"""Refer to hit_syn.py. https://github.com/RonenNess/grepfunc from grepfunc import grep """ # pylint: disable=invalid-name from pathlib import Path import re # from colorama import init, Fore, Back from gradio_ttw_api.detect_chinese import detect_chinese DIRPATH = Path(__file__).parent # rsync -uvaz acone3a:misc/reference-it-terms/HIT-同义词词林0.txt gradio_ttw_api FILE1 = r"HIT-同义词词林0.txt" FILE2 = r"HIT-IRLab-同义词词林detailed_table_of_contents.txt" dict_file = Path(DIRPATH) / Path(FILE1) assert dict_file.is_file(), f" {dict_file} does not exit." # init() # colorama # def hit_syno(query): def hit(query: str): """Grep synonyms from HIT synonyms file. >>> query = '同义' >>> '均等' in hit(query) True >>> hit('') '' """ if not detect_chinese(query): return "" result = [] with dict_file.open(encoding="utf-8") as fhandle: for line in fhandle: if query in line: result.append( re.sub( rf"({query})", # Fore.RED + r'\1' + Fore.RESET, line r"\1", line, ).strip() ) return "\n".join(result)