#! /usr/bin/python3 # Last edited on 2026-09-13 16:53:08 by stolfi import sys, os, re; from sys import stderr as err from error_funcs import arg_error, prog_error from process_funcs import bash import bencao_starps_make_funcs as makef import bencao_starps_size_position_funcs as spf def add_all_071_rules(pre, mak, tit): # Adds rules to build all derived files that # depend only on the SPS sources. # # Returns the list of ultimate targets. targets = \ add_all_071_bencao_par_ivt_rules(pre, mak, tit, "wh") + \ add_all_071_bencao_par_ivt_rules(pre, mak, tit, "ch") + \ add_all_071_upp_rules(pre, mak, tit) + \ add_all_071_word_tuple_rules(pre, mak, tit) + \ add_all_071_lines_with_pattern_rules(pre, mak, tit) + \ add_all_071_single_hist_rules(pre, mak, tit) + \ add_all_071_double_hist_rules(pre, mak, tit) + \ add_all_071_bencao_word_pos_plot_rules(pre, mak, tit) return targets # ---------------------------------------------------------------------- def add_all_071_bencao_par_ivt_rules(pre, mak, tit, utype): # Rules to create the "bencao-fu-{utype}-par.ivt" file, # where {utype} is "ch" and "wh", # from the raw transcription files in the "clean/ch" folder, # falling back to "ctp/bencao.uts" for entries not found there. targets = [] source_master = "ctp/bencao.utf" clean_dir = "clean/ch" ivt_par_name = f"bencao-fu-{utype}-par" target = f"{ivt_par_name}.ivt" script = "create_bencao_fu_par_ivt.py" tit[target] = f"making res/{target} from {source_master}, {clean_dir}/*.utf" pre[target] = [ source_master, clean_dir, ] mak[target] = ( f"./{script} {utype} {clean_dir} {source_master} res/{target}", ) targets.append(target) return targets # ---------------------------------------------------------------------- def add_all_071_bencao_ch_par_ivt_rules(pre, mak, tit): # Rules to create "bencao-fu-ch-par.ivt" # from "bencao-fu-wh-par.ivt". targets = [] wh_ivt_par_name = f"bencao-fu-wh-par" ch_ivt_par_name = f"bencao-fu-ch-par" source_wh = f"res/{wh_ivt_par_name}.ivt" target = f"{ch_ivt_par_name}.ivt" script = "remove_chinese_punctuation.py" tit[target] = f"making res/{target} from {source_wh}" pre[target] = [ source_wh, ] mak[target] = ( f"./{script} < {source_wh} > res/{target}", ) targets.append(target) return targets # ---------------------------------------------------------------------- def add_all_071_upp_rules(pre, mak, tit): # Rules to create the "{book}-{bsub}-{utype}.upp" files, with the size of # each recipe. The {utype} specifies the unit of size, namely "ch" for hanzi # chars. targets = [] for book, bsub, utype, ltype in ( \ ( "bencao", "fu", "ch", "par", ), ): ivt_name = f"{book}-{bsub}-{utype}-{ltype}" target = makef.add_rules_for_units_per_line_file(pre, mak, tit, ivt_name, utype) targets.append(target) return targets # ---------------------------------------------------------------------- def add_all_071_single_hist_rules(pre, mak, tit): targets = [] for book, bsub, utype in ( \ ( "bencao", "fu", "ch", ), ): ivt_name = f"{book}-{bsub}-{utype}-par" usize = 1.000 # Plot sizes in raw units. color = makef.hist_color(book, bsub, utype) bin_size = 5 if utype == "ec" else 1 target = makef.add_rules_for_single_parag_size_hist_plot(pre, mak, tit, ivt_name, usize, color, bin_size) targets.append(target) return targets # ---------------------------------------------------------------------- def add_all_071_double_hist_rules(pre, mak, tit): targets = [] for book0, bsub0, utype0, book1, bsub1, utype1, raw_size in ( ): assert utype0 == "ch" usize0 = 1 ivt_name0 = f"{book0}-{bsub0}-{utype0}-par" color0 = makef.hist_color(book0, bsub0, utype0) assert utype1 == "ch" usize1 = 1 ivt_name1 = f"{book1}-{bsub1}-{utype1}-par" color1 = makef.hist_color(book1, bsub1, utype1) bin_size = 1 target = makef.add_rules_for_double_parag_size_hist_plot \ ( pre, mak, tit, ivt_name0, usize0, color0, ivt_name1, usize1, color1, bin_size ) targets.append(target) return targets # ---------------------------------------------------------------------- def add_all_071_bencao_word_pos_plot_rules(pre, mak, tit): # Word position plots for various words and various parags in the SBJ. book = "bencao" bsub = "fu" ltype = "par" utype = "ch" usize = 1.0 ivt_name = f"{book}-{bsub}-{utype}-{ltype}" color = makef.hist_color(book, bsub, utype) kword_en_list = ( "USES", ) kword_en_list_not = ( "MAKES", "QI", ) slocs_maybe = ( \ "b1.4.100", # 3 hits. "b2.5.118", # 3 hits?. "b2.5.518", # 3 hits?. "b3.3.080", # 3 hits. "b2.1.013", # 3 hits. ) slocs_yes = ( \ "b1.4.096", # 7 hits - red rooster. "b2.4.094", # 3 hits. "b3.3.088", # 3 hits. ) slocs_not = ( \ "b1.1.014", # 1 hit. "b1.2.061", # 1 hit. "b1.1.007", # 1 hit. "b1.1.014", # 1 hit. "b1.2.061", # 1 hit. "b1.4.090", # 2 hits. "b1.5.106", # 1 hit. "b1.5.109", # 2 hits. "b2.1.001", # 1 hit. "b2.2.066", # 1 hit. "b2.3.090", # 2 hits. "b3.3.071", # 1 hit. "b3.5.119", # 2 hits. ) sloc_list = slocs_maybe + slocs_yes targets = makef.add_rules_for_many_word_pos_plots \ ( pre, mak, tit, ivt_name, utype, usize, color, kword_en_list, sloc_list ) return targets # ---------------------------------------------------------------------- def add_all_071_lines_with_pattern_rules(pre, mak, tit): # Creates ".opa" files for SBJ recipes that contain certain patterns. debug = False targets = [] for book, kword_en in ( \ ('bencao', 'USE1', ), ): assert book == "bencao" utype_raw = "ch"; utype_match = "ch"; ltype = "par"; bsub = "fu" ivt_name = f"{book}-{bsub}-{utype_raw}-{ltype}" target = makef.add_rules_for_lines_with_pattern_files \ ( pre, mak, tit, ivt_name, utype_match, kword_en ) if debug: err.write(f"!= {target = }\n") targets.append(target) return targets # ---------------------------------------------------------------------- def add_all_071_word_tuple_rules(pre, mak, tit): # Word tuples targets = [] for ivt_name, utype, tsize, kword_en in ( ( "bencao-fu-ch-par", "ch", 1, None, ), ( "bencao-fu-ch-par", "ch", 2, None, ), ( "bencao-fu-ch-par", "ch", 3, None, ), ( "bencao-fu-ch-par", "ch", 4, 'USES', ), ): target = makef.add_rules_for_word_tuple_file \ ( pre, mak, tit, ivt_name, utype, tsize, kword_en ) targets.append(target) return targets # ----------------------------------------------------------------------