#! /usr/bin/python3 # Last edited on 2026-10-12 09:37:17 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_072_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_072_double_hist_rules(pre, mak, tit) + \ add_all_072_coin_image_rules(pre, mak, tit) return targets # ---------------------------------------------------------------------- def add_all_072_double_hist_rules(pre, mak, tit): targets = [] for book0, bsub0, utype0, book1, bsub1, utype1 in ( \ ("bencao", "fu", "ch", "starps", "fu", "wp", ), # Compare SBJ with SPS "fu-wp". ("bencao", "fu", "ch", "starps", "fu", "wc", ), # Compare SBJ with SPS "fu-wc". ("bencao", "fu", "ch", "starps", "fu", "ec", ), # Compare SBJ with SPS "fu-ec". \ ("bencao", "fu", "ch", "starps", "gd", "wp", ), # Compare SBJ with SPS "gd-wp". ("bencao", "fu", "ch", "starps", "gd", "wc", ), # Compare SBJ with SPS "gd-wc". ("bencao", "fu", "ch", "starps", "gd", "ec", ), # Compare SBJ with SPS "gd-ec". ): del_qo = True exp_irm = True ivt_name0 = f"{book0}-{bsub0}-{utype0}-par" usize0 = 1 if utype0 == "ch" else 1/spf.expected_eva_letters_per_hanzi(utype0, del_qo, exp_irm) color0 = makef.hist_color(book0, bsub0, utype0) ivt_name1 = f"{book1}-{bsub1}-{utype1}-par" usize1 = 1 if utyp1 == "ch" else 1/spf.expected_eva_letters_per_hanzi(utype1, del_qo, exp_irm) color1 = makef.hist_color(book1, bsub1, utype1) bin_size = 5 if utype0 == "ec" else 1 target = add_072_rules_for_double_parag_size_hist_plot \ ( pre, mak, tit, ivt_name0, utype0, usize0, color0, ivt_name1, utype1, usize1, color1, bin_size ) targets.append(target) return targets # ---------------------------------------------------------------------- def add_all_072_coin_image_rules(pre, mak, tit): # Adds rules and commands to create files # "res/{upp_name0}-{upp_name1}-coin-map.png" that shows coincidences of sizes # etc between the sizes of the parags of the SBJ "../071/{upp_name0}-par.upp" and those of the # SPS "res/{upp_name1}-par.upp". Returns the upp_names of the targets (sans "res/"). # The starps file should be first because it is the shortest one. utype1 = "ch" upp_name1 = f"bencao-fu-{utype1}-par" usize1 = 1.000 targets = [] for bsub0, utype0 in ("gd", "wc"), ("gd", "wp"), ("gd", "ec"): upp_name0 = f"starps-{bsub0}-{utype0}-par" del_qo = True exp_irm = True usize0 = 1/spf.expected_eva_letters_per_hanzi(utype0, del_qo, exp_irm) target = add_072_rules_for_parag_parag_coin_image \ ( pre, mak, tit, upp_name0, utype0, usize0, upp_name1, utype1, usize1 ) targets.append(target) return targets # ---------------------------------------------------------------------- def add_072_rules_for_units_per_line_file(pre, mak, tit, ivt_name, utype): assert utype == "ch" or utype == "ec" or utype == "wc" or utype == "wp" # The ".upp" files are created other Notes: source_dir = "../071" if utype == "ch" else "../070" source = f"{source_dir}/res/{ivt_name}.ivt" pfn_py_lib = "work/process_funcs.py" cfn_py_lib = "work/chinese_funcs.py" target = f"{ivt_name}.upp" # The size counting script and its imported modules: tit[target] = f"making parag size file {source_dir}/res{target} from {source}" pre[target] = [ source_dir ] mak[target] = ( f"( cd {source_dir} && make TARGETS='{target}' ) ", ) return target # ---------------------------------------------------------------------- def add_072_rules_for_double_parag_size_hist_plot \ ( pre, mak, tit, ivt_name0, utype0, usize0, color0, ivt_name1, utype1, usize1, color1, bin_size ): # Rules to create the plot file "{ivt_name0}-{ivt_name1}-upp-hist.png" # that compare the histograms of parag sizes listed in files # "{dir0}/res/{ivt_name0}.upp" and "{dir1}/res/es/{ivt_name1}.upp", # where {dir0} and {dir1} are "../070" for unit types "ec", "wp", and "wc", # "or "../071" for unit type "ch". # # The unit types {utype0,utype1} must be consistent with {ivt_name0,ivt_name1} dir0 = "../071" if utype0 == "ch" else "../070" dir1 = "../071" if utype1 == "ch" else "../070" # Add rules to create the prerequisite files: for ivt_name, utype in (ivt_name0, utype0), (ivt_name1, utype1): pre_target = add_072_rules_for_units_per_line_file(pre, mak, tit, ivt_name, utype) assert pre_target == f"{ivt_name}.upp" target = f"{ivt_name0}-{ivt_name1}-upp-hist.png" # Hstogram plotting script and its imported modules: script = "work/plot_double_parag_size_histograms.sh" mkwdhist_script = "work/make_hist_of_units_per_parag.sh" mkhist_script = "work/make_histogram.gawk" h2poly_script = "work/turn_histogram_into_polygonal_line.gawk" efn_gawk_lib = "work/error_funcs.gawk" pre[target] = [ script, mkwdhist_script, efn_gawk_lib, mkhist_script, h2poly_script ] pre[target].append(f"{dir0}/res/{ivt_name0}.upp") pre[target].append(f"{dir1}/res/{ivt_name1}.upp") tit[target] = f"plotting parag size histograms for {ivt_name0} and {ivt_name1}" mak[target] = ( f"{script} {dir0} {ivt_name0} {usize0:.3f} '{color0}' {dir1} {ivt_name1} {usize1:.3f} '{color1}' {bin_size}", ) return target # ---------------------------------------------------------------------- def add_072_rules_for_parag_parag_coin_image \ ( pre, mak, tit, ivt_name0, utype0, usize0, ivt_name1, utype1, usize1 ): # Adds rules and commands to create a target file # "res/{ivt_name0}-{ivt_name1}-coin-map.png" that shows coincidences # of sizes between the SPS recipes and SBJ # parags. These sizes are # obtained from # "{dir0}/res/{ivt_name0}.upp" and "{dir1}/res/es/{ivt_name1}.upp", # where {dir0} and {dir1} are "../070" for unit types "ec", "wp", and "wc", # "or "../071" for unit type "ch". dir0 = "../071" if utype0 == "ch" else "../070" dir1 = "../071" if utype1 == "ch" else "../070" # Add rules to create the prerequisite files: for ivt_name, utype in (ivt_name0, utype0), (ivt_name1, utype1): pre_target = add_072_rules_for_units_per_line_file(pre, mak, tit, ivt_name, utype) assert pre_target == f"{ivt_name}.upp" target = f"{ivt_name0}-{ivt_name1}-coin-map.png" # Hstogram plotting script and its imported modules: script = "create_parag_size_similarity_image.py" pre[target] = [ script, ] pre[target].append(f"{dir0}/res/{ivt_name0}.upp") pre[target].append(f"{dir1}/res/{ivt_name1}.upp") tit[target] = f"plotting parag size histograms for {ivt_name0} and {ivt_name1}" mak[target] = ( f"{script} {dir0} {ivt_name0} {utype0} {usize0} {dir1} {ivt_name1} {utype1} {usize1}", ) return target # ----------------------------------------------------------------------