#! /usr/bin/python3 # Last edited on 2026-10-07 07:02:09 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_070_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_070_starps_lin_ivt_rules(pre, mak, tit) + \ add_all_070_starps_par_ivt_rules(pre, mak, tit) + \ add_all_070_starps_pag_ivt_rules(pre, mak, tit) + \ add_all_070_upp_rules(pre, mak, tit) + \ add_all_070_word_tuple_rules(pre, mak, tit) + \ add_all_070_lines_with_pattern_rules(pre, mak, tit) + \ add_all_070_single_hist_rules(pre, mak, tit) + \ add_all_070_double_hist_rules(pre, mak, tit) + \ add_all_070_starps_word_pos_plot_rules(pre, mak, tit) return targets # ---------------------------------------------------------------------- def add_all_070_starps_lin_ivt_rules(pre, mak, tit): # Rules to create the "-lin.ivt" files of the SPS from the raw ".ivt" # transcription files, without comments, weirdos, ligatures, etc. targets = [] source_raw = "../074/st_files/str-parags.ivt" # The join script and its imported modules: raw_to_lin_script = f"convert_starps_raw_to_lin_ivt.py" pfn_py_lib = "work/process_funcs.py" ivt_py_lib = "work/ivt_format.py" nfn_py_lib = "note_070_funcs.py" filter_script = "remove_bad_lines_from_starps_ivt.gawk" efn_gawk_lib = "work/error_funcs.gawk" for bsub in ("fu", "gd", ): for utype in ( "ec", "wp", "wc", ): target = f"starps-{bsub}-{utype}-lin.ivt" tit[target] = f"creating {bsub}-{utype} SPS transcripton file {target} from {source_raw}" pre[target] = [ source_raw, raw_to_lin_script, pfn_py_lib, ivt_py_lib, nfn_py_lib ] mak[target] = [ f"cat {source_raw} \\", f" | egrep -v -e '^]' \\", ] if bsub == "gd": # Subset with good parags only: pre[target] += [ filter_script, efn_gawk_lib, ] mak[target] += [ f" | {filter_script} -i {efn_gawk_lib} \\", ] mak[target] += [ f" | {raw_to_lin_script} {utype} \\", f" > res/{target}", ] targets.append(target) return targets # ---------------------------------------------------------------------- def add_all_070_starps_par_ivt_rules(pre, mak, tit): # Rules to create the "-par.ivt" files of the SPS from the "-lin.ivt" # files, with each parag joined into a single line, without # comments, weirdos, ligatures, etc. targets = [] # The join script and its imported modules: lin_to_par_script = f"convert_lin_ivt_to_par_ivt.py" pfn_py_lib = "work/process_funcs.py" ivt_py_lib = "work/ivt_format.py" nfn_py_lib = "note_070_funcs.py" for bsub in ( "fu", "gd", ): for utype in ( "ec", "wp", "wc", ): target = f"starps-{bsub}-{utype}-par.ivt" source = f"res/starps-{bsub}-{utype}-lin.ivt" tit[target] = f"creating '{bsub}' SPS line-per-parag file {target} from {source}" pre[target] = [ source, lin_to_par_script, pfn_py_lib, ivt_py_lib, nfn_py_lib ] mak[target] = [ f"cat {source} \\", f" | {lin_to_par_script} {utype} \\", f" > res/{target}", ] targets.append(target) return targets # ---------------------------------------------------------------------- def add_all_070_starps_pag_ivt_rules(pre, mak, tit): # Rules to create the "-pag.ivt" files for the SPS, like "-par.ivt" # but with all the parags on the same page joined into a single big # parag. # The join script and its imported modules: par_to_pag_script = f"convert_starps_par_ivt_to_pag_ivt.py" pfn_py_lib = "work/process_funcs.py" ivt_py_lib = "work/ivt_format.py" nfn_py_lib = "note_070_funcs.py" targets = [] # No sense making the "gd" version. for bsub in ( "fu", ): for utype in ( "ec", "wp", "wc", ): target = f"starps-{bsub}-{utype}-pag.ivt" source = f"res/starps-{bsub}-{utype}-par.ivt" tit[target] = f"creating '{bsub}' SPS line-per-page file {target} from {source}" pre[target] = [ source, par_to_pag_script, pfn_py_lib, ivt_py_lib, nfn_py_lib ] mak[target] = [ f"cat {source} \\", f" | {par_to_pag_script} {utype} \\", f" > res/{target}", ] targets.append(target) return targets # ---------------------------------------------------------------------- def add_all_070_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, either "wp", "wc", or "ec". targets = [] for book, bsub, utype, ltype in ( \ ( "starps", "fu", "wp", "par", ), ( "starps", "fu", "wc", "par", ), ( "starps", "fu", "ec", "par", ), \ ( "starps", "fu", "wp", "pag", ), ( "starps", "fu", "wc", "pag", ), ( "starps", "fu", "ec", "pag", ), \ ( "starps", "gd", "wp", "par", ), ( "starps", "gd", "wc", "par", ), ( "starps", "gd", "ec", "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_070_single_hist_rules(pre, mak, tit): targets = [] for book, bsub, utype in ( \ ( "starps", "fu", "wp", ), ( "starps", "fu", "wc", ), ( "starps", "fu", "ec", ), ( "starps", "gd", "wp", ), ( "starps", "gd", "wc", ), ( "starps", "gd", "ec", ), ): 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_070_double_hist_rules(pre, mak, tit): targets = [] for book0, bsub0, utype0, book1, bsub1, utype1, raw_size in ( \ ("starps", "fu", "ec", "starps", "gd", "ec", True, ), # Compare full vs good SPS in "ec" units. ("starps", "fu", "wc", "starps", "gd", "wc", True, ), # Compare full vs good SPS in "wc" units. ("starps", "gd", "wc", "starps", "gd", "wp", True, ), # Compare "wc" vs "wp" SPS in good subset. ("starps", "gd", "wc", "starps", "gd", "ec", False, ), # Compare "wc" vs "ec" SPS in good subset. ): usize0 = 1 usize1 = 1 if not raw_size and utype0 != utype1: # Assume that each word counts as 5 hanzi: if utype0 == "ec": usize1 = 5 elif utype1 == "ec": usize0 = 5 assert utype0 == "ec" or utype0 == "wc" or utype0 == "wp" ivt_name0 = f"{book0}-{bsub0}-{utype0}-par" color0 = makef.hist_color(book0, bsub0, utype0) assert utype1 == "ec" or utype1 == "wc" or utype1 == "wp" ivt_name1 = f"{book1}-{bsub1}-{utype1}-par" color1 = makef.hist_color(book1, bsub1, utype1) bin_size = 5 if utype0 == "ec" or utype1 == "ec" else 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_070_starps_word_pos_plot_rules(pre, mak, tit): # Word position plots for various words and parags in the SPS. book = "starps" bsub = "fu" ltype = "par" utype = "ec" del_qo = False # !!! Make parameter, pass on !!! exp_irm = False # !!! Make parameter, pass on !!! usize = 1/spf.expected_eva_letters_per_hanzi(del_qo, exp_irm) ivt_name = f"{book}-{bsub}-{utype}-{ltype}" color = makef.hist_color(book, bsub, utype) kword_en_list = ( "USE1", "USES", ) kword_en_lis_not = ( ) slocs_maybe = ( \ "f105v.1", # 4 hits. "f105v.14", # 4 hits. "f106r.42", # 4 hits. "f104v.22", # 6 hits. "f114r.4", # 6 hits. "f114r.8", # 6 hits. "f114r.19", # 6 hits. "f114r.24", # 6 hits. ) slocs_yes = ( \ "f112v.11", # 3 hits. "f105v.20", # 4 hits. "f105v.32", # 5 hits. ) slocs_later = ( \ "f104v.1", # 7 hits. ) slocs_no_match = ( \ "f105r.42", # 0 hits. "f105v.8", # 2 hits. "f106v.42", # 2 hits. "f115r.13", # 2 hits. "f114r.14", # 4 hits. "f104r.1", # 3 hits. "f104r.12", # 3 hits. "f105r.1", # 3 hits. "f105v.5", # 3 hits. "f106v.1", # 3 hits. "f107r.21", # 3 hits. "f113r.10", # 3 hits. ) slocs_messy = ( \ "f105r.17", # 5 hits. "f108v.20", # 5 hits. "f108v.23", # 4 hits. "f108v.23", # 5 hits. "f111r.10", # 3 hits. "f111r.25", # 4 hits. "f114r.32", # 4 hits. "f114v.36", # 4 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_070_lines_with_pattern_rules(pre, mak, tit): # Creates ".opa" files with SPS lines that contain certain patterns. debug = False targets = [] for book, kword_en in ( \ ('starps', 'USE1', ), ): assert book == "starps" utype_raw = "wp"; utype_match = "ec"; 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_070_word_tuple_rules(pre, mak, tit): # Word tuples targets = [] for ivt_name, utype, tsize, kword_en in ( ( "starps-fu-ec-pag", "ec", 5, None, ), ( "starps-fu-ec-pag", "ec", 11, None, ), ( "starps-fu-ec-par", "ec", 6, 'USES', ), ( "starps-fu-wc-par", "wc", 1, None, ), ( "starps-fu-wc-par", "wc", 2, None, ), ( "starps-gd-wc-par", "wc", 1, None, ), ( "starps-gd-wc-par", "wc", 2, None, ), ( "starps-fu-wp-par", "wp", 1, None, ), ( "starps-fu-wp-par", "wp", 2, None, ), ( "starps-fu-wp-pag", "wp", 2, 'USES' , ), ): target = makef.add_rules_for_word_tuple_file \ ( pre, mak, tit, ivt_name, utype, tsize, kword_en ) targets.append(target) return targets # ----------------------------------------------------------------------