#! /usr/bin/python3 # Last edited on 2026-10-07 09:06:31 by stolfi # Functions for removing punctuation from SBJ (hanzi) and SPS (EVA) text, import sys, os, re from sys import stdout as out, stderr as err, stdin as inp from error_funcs import arg_error, file_line_error, prog_error from process_funcs import bash, basic_line_loop from chinese_funcs import read_chinese_char_set from compute_stats_funcs import compute_and_print_stats from math import sqrt, hypot, exp, log, pi, inf, nan, floor, ceil def remove_punctuation(text, utype, data_error): # Removes the punctuation of a file of the given {utype}. # The result may be of a different {utype}. # # See {remove_bencao_punctuation} for more details when {utype} is "ch" # of "wh". # # See {remove_starps_punctuation} for more details when the {utype} # is "ec", "wp", or "wc". # if utype == "ch" or utype == "wh": text = remove_bencao_punctuation(text, utype, data_error) elif utype == "ec" or utype == "wc" or utype == "wp": text = remove_starps_punctuation(text, utype, data_error) return text # ---------------------------------------------------------------------- def remove_starps_punctuation(text, utype, data_error): # Removes punctuation from a {text} from an SPS text (line, # parag or page) of the given {utype}, which must be # "ec", "wc", or "wp". # # If {utype} is "wc" or "wp", deletes all commas, dashes, and periods, # and the alignment markers [«=»]. The text then becomes of type "ec". # # If {utype} is "ec", bombs out if the {text} contains any dashes, commas, # or periods; otherwise it just deletes any alignment markers [«=»]. text = text.strip() if utype == "ec" or utype == "wc" or utype == "wp": # Remove alignment markers: text = re.sub(r"^[«=»]", "", text) text = re.sub(r"[«=»]$", "", text) # Replace punctuation: text = re.sub(r"-", ".", text) if utype == "ec": if re.search(r"[-,.]", text) is not None: data_error("punctuation in 'ec' text") elif utype == "wc" or utype == "wp": text = re.sub(r"[-,.]", "", text) else: assert False # Should not happen. else: arg_error(f"invalid {utype = !r}") return text # ---------------------------------------------------------------------- def remove_bencao_punctuation(text, utype, data_error): # Removes the punctuation from an SBJ (hanzi) text of type {utype}. # Returns the cleaned {text}. # # If the {utype} is "wh", the function removes all ideographic # punctuation, including ideographic spaces ' '. The result is then # of type "ch". # # If the {utype} is "ch", the functon fails if the {text} contains any # ideographic punctuation, including spaces; otherwise it is a no-op. # punct = r"[][;:、,。[]《》 ]" if utype == "wh": # Remove all punctuation: text = re.sub(punct, "", text) elif utype == "ch": if re,search(punct, text) is not None: data_error("punctuation in 'ch' text") else: arg_error(f"invalid text unit type {utype = !r}") return text # ---------------------------------------------------------------------- def test_stuff(): def data_error(msg): err.write(f"** {msg}\n"); assert False # .................................................................... return # ---------------------------------------------------------------------- if len(sys.argv) > 1 and sys.argv[1] == "BSRF.TEST": test_stuff()