-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvicleaners.py
276 lines (232 loc) · 6.55 KB
/
vicleaners.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# -*- coding: utf-8 -*-
# Cleaners for Vietnamese
# A Module in TTS Synthesis
# Dev by R.
# Date : 09/2018
from pyvi import ViTokenizer
from rules import _spec_char, _currency, _d_unit, _w_unit, _number, short_dict
import re
flatten = lambda *n: (e for a in n
for e in (flatten(*a) if isinstance(a, (tuple, list)) else (a,)))
# Regular expression matching whitespace:
_whitespace_re = re.compile(r'\s+')
_string_ascii_re = re.compile(r'[a-z]+')
_number_re = re.compile(r'[0-9]+')
_fnumber_re = re.compile(r'[0-9]+(\.|\,)[0-9]+')
_numbers_re = re.compile(r'[0-9]+(\,|\.)[0-9]+((\.|\,)[0-9]*)+')
_date_re = re.compile(r'([0-9]{2}\/)?[0-9]{2}(\/[0-9]{4})?')
_fraction_re = re.compile(r'[0-9]+(\/[0-9]+)+')
_unit_re = re.compile(r'([0-9]+)?[a-z]{5}')
def num_to_text(text, flag):
try:
num = int(text)
if num <= 10:
if flag == 0:
return _number[num]
return u"linh " + _number[num]
if num//1000000000 > 0:
if num%1000000000 == 0:
return num_to_text(num//1000000000, 0) + u" tỷ"
if num%1000000000 != 0:
return num_to_text(num//1000000000, 0) + u" tỷ " + num_to_text(num%1000000000, 1)
if num//1000000 > 0:
if num%1000000 == 0:
return num_to_text(num//1000000, 0) + u" triệu"
if num%1000000 != 0:
return num_to_text(num//1000000, 0) + u" triệu " + num_to_text(num%1000000, 2)
else:
if flag == 1:
return num_to_text(num//1000000, 0) + u" triệu " + num_to_text(num%1000000, 2)
if num//1000 > 0:
if num%1000 == 0:
return num_to_text(num//1000, 0) + u" nghìn"
if num%1000 != 0:
return num_to_text(num//1000, 0) + u" nghìn " + num_to_text(num%1000, 3)
else:
if flag == 2:
return num_to_text(num//1000, 0) + u" nghìn " + num_to_text(num%1000, 3)
if num//100 > 0:
if num%100 == 0:
return num_to_text(num//100, 0) + u" trăm"
if num%100 != 0:
return num_to_text(num//100, 0) + u" trăm " + num_to_text(num%100, 4)
else:
if flag == 3:
return num_to_text(num//100, 0) + u" trăm " + num_to_text(num%100, 4)
if num//10 > 0:
if num >= 20:
if num%10 != 0:
if num%10 == 1:
return num_to_text(num//10, 0) + u" mươi mốt"
if num%10 == 5:
return num_to_text(num//10, 0) + u" mươi lăm"
return num_to_text(num//10, 0) + u" mươi " + num_to_text(num%10, 0)
else:
return num_to_text(num//10, 0) + u" mươi"
else:
if num == 15:
return u"mười lăm"
return u"mười " + num_to_text(num%10, 0)
except:
return text
def normalize_numbers(text):
try:
if len(text) > 1:
output = u''
splitChar = ''
res = [text]
if ',' in text:
res = text.split(',')
splitChar =','
if '.' in text:
res = text.split('.')
splitChar = '.'
if '/' in text:
res = text.split('/')
splitChar = '/'
if len(res) > 1:
if splitChar in ['.', ',']:
for i, map in enumerate(res):
if i < len(res) - 1:
output += num_to_text(map, 0) + u" phẩy "
else:
output += num_to_text(map, 0)
if splitChar in ['/']:
if len(res) == 3:
if int(res[0]) <=31 and int(res[1]) <=12 and len(res[2]) == 4:
return u"ngày " + num_to_text(res[0], 0) + u" tháng " + num_to_text(res[1], 0) + u" năm " + num_to_text(res[2], 0)
elif len(res) == 2:
if int(res[0]) <= 31 and int(res[1]) <= 12:
return u"ngày " + num_to_text(res[0], 0) + u" tháng " + num_to_text(res[1], 0)
elif int(res[0]) <= 12 and len(res[1]) == 4:
return u"tháng " + num_to_text(res[0], 0) + u" năm " + num_to_text(res[1], 0)
for i, map in enumerate(res):
if i < len(res) - 1:
output += num_to_text(map, 0) + u" phần "
else:
output += num_to_text(map, 0)
if str(text).isdigit():
return num_to_text(text, 0).split()
return output
return text
except:
return text
def c_unit(text):
try:
return _currency[text]
except:
return text
def d_unit(text):
try:
return _d_unit[text]
except:
return text
def w_unit(text):
try:
return _w_unit[text]
except:
return text
def _remove_commmas(m):
text = m.group(0).replace(',', '')
text = text.replace('.', '')
return text
def _fnumber_(m):
text = m.group(0)
text = normalize_numbers(text)
return text
def _number_(m):
text = m.group(0)
text = num_to_text(text, 0)
return " "+ text +" "
def _fraction_(m):
text = m.group(0)
text = normalize_numbers(text)
return text
def __unit(text):
text = c_unit(text)
text = w_unit(text)
text= d_unit(text)
def _unit_(m):
text = m.group(1)
text = re.sub(_number, _number_, text)
text = re.sub(_string_ascii_re, __unit, text)
return text
def _short_dict(text):
d = short_dict()
try:
return d[text]
except:
return text
def _normalize_numbers(text):
text = re.sub(_numbers_re, _remove_commmas, text)
text = re.sub(_fraction_re, _fraction_, text)
text = re.sub(_fnumber_re, _fnumber_, text)
text = re.sub(_number_re, _number_, text)
return text
def _specChar(text):
try:
if len(text) == 1:
return _spec_char[text]
else:
for char in _spec_char:
if char in text:
text = text.replace(char,u" " + _spec_char[char] + u" ")
result = []
for char in text.split():
char = _normalize_numbers(char)
char = c_unit(char)
char = d_unit(char)
char = w_unit(char)
result.append(char)
return cleaners.join_str(result)
except:
return text
class cleaners(object):
def __init__(self, text=None):
if text == None:
print("Text not None!")
return
self.str = text
self.raw = text
self.word_sent = []
self.result = []
def split_word_sent(self, text):
self.word_sent = ViTokenizer.tokenize(text).split()
return self.word_sent
@staticmethod
def collapse_whitespace(text):
return re.sub(_whitespace_re, ' ', text)
@staticmethod
def lower(text):
return str(text).lower()
def do(self):
text = cleaners.collapse_whitespace(self.str)
text = cleaners.lower(text)
ws = self.split_word_sent(text)
# print(ws)
for chars in ws:
if "_" in chars:
chars = chars.split("_")
else:
chars = [chars]
for char in chars:
char = _short_dict(char)
char = _normalize_numbers(char)
char = _specChar(char)
char = c_unit(char)
char = d_unit(char)
char = w_unit(char)
self.result.append(char)
return cleaners.join_str(flatten(self.result))
def strip(self):
self.str = self.str.strip()
return self.str
@staticmethod
def join_str(list_str):
return " ".join(list_str)
if __name__ == "__main__":
with open("input.txt", mode="r", encoding="utf-8") as f:
for line in f:
print("Input:\n[%s]" % (line))
ret = cleaners(line).do()
print("Output:\n[%s]" % ret)