File size: 3,824 Bytes
faadabf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
from fireredtts.modules.text_normalizer.regex_common import *
from sentencex import segment
import re


symbol_reduction = {
    "「": '"',
    "」": '"',
    "`": '"',
    "〝": '"',
    "〞": '"',
    "‟": '"',
    "„": '"',
    "{": "(",
    "}": ")",
    "【": "(",
    "】": ")",
    "〖": "(",
    "〗": ")",
    "〔": "(",
    "〕": ")",
    "〘": "(",
    "〙": ")",
    "《": "(",
    "》": ")",
    "⦅": "(",
    "⦆": ")",
    "〚": "(",
    "〛": ")",
    "『": '"',
    "』": '"',
    "「": '"',
    "」": '"',
    "{": "(",
    "}": ")",
    "〈": "(",
    "〉": ")",
    "•": "·",
    "‧": "·",
    "〰": "…",
    "﹏": "…",
    "〜": "~",
    "~": "~",
    "+": "+",
    "、": "、",
    "。": "。",
    "︐": ",",
    "﹐": ",",
    "︑": "、",
    "﹑": "、",
    "︒": "。",
    "︓": ":",
    "﹕": ":",
    "︔": ";",
    "﹔": ";",
    "︕": "!",
    "﹗": "!",
    "︖": "?",
    "﹖": "?",
    "﹙": "(",
    "﹚": ")",
    "﹪": "%",
    "﹠": "&",
    ">": ">",
    "|": "、",
    "=": "=",
    "‐": "-",
    "‑": "-",
    "‒": "-",
    "–": "-",
    "—": "-",
    "―": "-",
    "%": "%",
    "μ": "u",
}


strong_break = re.compile("([。”;;!!:…??)\)\]』】」}~\r\n]| \.)", re.UNICODE)
weak_break = re.compile(
    "["
    "\U00002702-\U000027b0\U0001f926-\U0001f937\U00010000-\U0001fbff\U00030000-\U0010ffff"
    "\u2640-\u2642\u2600-\u2b55\u23cf\u23e9\u231a\ufe0f\u3030"
    "\t,,. ]",
    re.UNICODE,
)


def contains_chinese(text):
    return bool(chinese_regex.search(text))


def strip_kaomoji(text):
    return kaomoji_regex.sub(" ", text)


def is_chinese(char):
    return chinese_char_regex.match(char)


def is_eng_and_digit(char):
    return eng_and_digit_char_regex.match(char)


def is_upper_eng_and_digit(text):
    return upper_eng_and_digit_regex.match(text)


def is_valid_char(char):
    return valid_char_regex.match(char)


def is_digit(text):
    return digit_regex.match(text)


def f2b(ustr, exemption="。,:"):
    half = []
    for u in ustr:
        num = ord(u)
        if num == 0x3000:
            half.append(" ")
        elif u in exemption:  # exemption
            half.append(u)
        elif 0xFF01 <= num <= 0xFF5E:
            num -= 0xFEE0
            half.append(chr(num))
        else:
            half.append(u)
    return "".join(half)


def zh_text_split(text, length=80):
    if length == 0:
        return []
    if length == 1:
        return [c for c in length]
    if len(text) <= length:
        return [text]

    match_strong = re.search(strong_break, text[:length][::-1])
    match_weak = re.search(weak_break, text[:length][::-1])
    end_ind_strong = length - match_strong.start() if match_strong else 0
    end_ind_weak = length - match_weak.start() if match_weak else 0

    if end_ind_strong < length // 3:
        if end_ind_weak < length // 3:
            valid_max = max(end_ind_strong, end_ind_weak)
            if valid_max >= 3:
                return [text[:valid_max]] + zh_text_split(text[valid_max:])
            else:
                return [text[:length]] + zh_text_split(text[length:])
        else:
            return [text[:end_ind_weak]] + zh_text_split(text[end_ind_weak:])
    else:
        return [text[:end_ind_strong]] + zh_text_split(text[end_ind_strong:])


def text_split(text):
    if contains_chinese(text):
        substrings = list(segment("zh", text))
        new_substrings = []
        for s in substrings:
            if len(s) > 50:
                new_substrings += zh_text_split(s, length=50)
            else:
                new_substrings.append(s)
        substrings = new_substrings
    else:
        substrings = list(segment("en", text))

    return substrings