forked from Executedone/Chinese-FastSpeech2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
text_normalization.py
226 lines (182 loc) · 5.83 KB
/
text_normalization.py
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
'''
_*_ coding: utf-8 _*_
Date: 2021/11/22
Author:
Intent:
'''
import re
POLY_PHRASE = {
'我行': [['wǒ'], ['háng']],
'开户行': [['kāi'], ['hù'], ['háng']],
'还款': [['huán'], ['kuǎn']],
'行货': [['háng'], ['huò']],
'发卡行': [['fā'], ['kǎ'], ['háng']],
'茧行': [['jiǎn'], ['háng']],
'还呗': [['huán'], ['bei']],
'人行': [['rén'], ['háng']],
'各行': [['gè'], ['háng']],
'行号': [['háng'], ['hào']],
'素朴': [['sù'], ['pǔ']]
}
NUM_DICT = {
'0': '零',
'1': '一',
'2': '二',
'3': '三',
'4': '四',
'5': '五',
'6': '六',
'7': '七',
'8': '八',
'9': '九',
'10': '十',
'100': '百',
'1000': '千',
'-': ','
}
TIME = {
':': '点',
'30': '半',
'-': '至',
'~': '至'
}
SPECIAL_SYMBOLS = {
'.': '点',
'%': '百分之'
}
PUNCTRUATION = [',', ';', '"', '!', '!', ',', '。', ';', ':', '’', '“', '?',
'、', '?', '(', ')', '《', '》', '(', ')', '、', '<', '>', '_',
'——', '+', '[', ']']
SINGLE_NUM_PATTERN = re.compile(r'(\d{3,20}-*)+\d{2,10}')
PHRASE_NUM_PATTERN = re.compile(r'(\d{1,2}-\d{1,2})|(\d{1,4}\.\d{1,4})|(\d{1,4})|(—55)')
TIME_PATTERN = re.compile(r'(\d{1,2}:\d{2}[-~]\d{1,2}:\d{2})|(\d{1,2}:30)|(\d{1,2}:00)|(\d{1,2}:\d{2})')
PERCENTAGE_PATTERN = re.compile(r'\d+(\.\d+)*%')
def get_num_phones(num):
phones = []
num_len = len(num)
if num_len == 2 and num[0] == '1':
phones.append(NUM_DICT['10'])
if num[1] != '0':
phones.append(NUM_DICT[num[1]])
else:
for i, n in enumerate(num):
if num[i:] == '0' * (num_len - i) or num[:(i + 1)] == '0' * (i + 1):
continue
else:
if len(phones) > 0 and phones[-1] == '零' and n == '0':
continue
else:
phones.append(NUM_DICT[n])
if n == '0':
continue
if num_len - i == 4:
phones.append(NUM_DICT['1000'])
elif num_len - i == 3:
phones.append(NUM_DICT['100'])
elif num_len - i == 2:
phones.append(NUM_DICT['10'])
else:
pass
return phones
def get_time_phones(time):
phones = []
assert ':' in time
h, m = time.split(':')
h_phone = get_num_phones(h)
if m == '30':
m_phone = [TIME['30']]
elif m == '00':
m_phone = None
else:
m_phone = get_num_phones(m)
phones.extend(h_phone)
phones.append(TIME[':'])
if m_phone is not None:
phones.extend(m_phone)
return phones
def get_percent_phones(percentage):
phones = SPECIAL_SYMBOLS['%'].split(' ')
s = percentage.replace('%', '')
if '.' in s:
pre, post = s.split('.')
comma = SPECIAL_SYMBOLS['.']
else:
pre, post = s, ''
comma = None
if pre == '0':
phones.append(NUM_DICT['0'])
else:
phones.extend(get_num_phones(pre))
if comma is not None:
phones.append(comma)
phones.extend([NUM_DICT[n] for n in post if n in NUM_DICT])
return phones
def get_special_phones(phrase):
phones = []
if phrase == '2':
return ['两']
if phrase == '—55':
return ['至', '五', '十', '五']
if PERCENTAGE_PATTERN.match(phrase):
return get_percent_phones(phrase)
if TIME_PATTERN.match(phrase):
if '-' in phrase or '~' in phrase:
b = '-' if '-' in phrase else '~'
temp = phrase.split(b)
pre_t, post_t = temp[0], temp[1]
pre_t_phones = get_time_phones(pre_t)
post_t_phones = get_time_phones(post_t)
phones.extend(pre_t_phones)
phones.append(TIME[b])
phones.extend(post_t_phones)
else:
t_phones = get_time_phones(phrase)
phones.extend(t_phones)
return phones
if SINGLE_NUM_PATTERN.match(phrase):
phones.extend([NUM_DICT[num] for num in phrase if num in NUM_DICT])
return phones
if PHRASE_NUM_PATTERN.match(phrase):
if '-' in phrase:
nums = phrase.split('-')
for i, num in enumerate(nums):
if len(num) > 0:
phones.extend(get_num_phones(num))
if i < len(nums) - 1:
phones.append(TIME['-'])
elif '.' in phrase:
temp = phrase.split('.')
phrase_num = temp[0]
single_num = temp[1]
phones.extend(get_num_phones(phrase_num))
phones.append(SPECIAL_SYMBOLS['.'])
phones.extend([NUM_DICT[n] for n in single_num if n in NUM_DICT])
else:
num = re.match(r'\d+', phrase).group()
phones.extend(get_num_phones(num))
return phones
return phones
def pattern_normalize(text, pattern):
normalized_text = []
s, e = 0, 0
for x in pattern.finditer(text):
phrase, start, end = x.group(), x.start(), x.end()
e = start
normalized_text.append(text[s:e])
s = end
normalized_text.append(''.join(get_special_phones(phrase)))
normalized_text.append(text[s:len(text)])
text = ''.join(normalized_text)
return text
def eliminate_duplicate_punctuation(text):
text = re.sub(r'\W+', ',', text)
return text
def text_normalization(text):
text = pattern_normalize(text, PERCENTAGE_PATTERN)
text = pattern_normalize(text, TIME_PATTERN)
text = pattern_normalize(text, SINGLE_NUM_PATTERN)
text = pattern_normalize(text, PHRASE_NUM_PATTERN)
text = eliminate_duplicate_punctuation(text)
return text
if __name__ == "__main__":
pass