File size: 1,280 Bytes
94e5748
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"""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"<em>\1</em>",
                        line,
                    ).strip()
                )

    return "\n".join(result)