-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpreprocess.py
216 lines (179 loc) · 10.1 KB
/
preprocess.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
# https://github.com/nguyenvanhieuvn/text-classification-tutorial/blob/master/text_classification_tutorial.ipynb
# https://nguyenvanhieu.vn/phan-loai-van-ban-tieng-viet
import regex as re
import string
import emoji
#from vncorenlp import VnCoreNLP
import underthesea
from nltk import flatten
# Remove HTML code
def remove_HTML(text):
return re.sub(r'<[^>]*>', '', text)
# Standardize unicode
def convert_unicode(text):
char1252 = 'à|á|ả|ã|ạ|ầ|ấ|ẩ|ẫ|ậ|ằ|ắ|ẳ|ẵ|ặ|è|é|ẻ|ẽ|ẹ|ề|ế|ể|ễ|ệ|ì|í|ỉ|ĩ|ị|ò|ó|ỏ|õ|ọ|ồ|ố|ổ|ỗ|ộ|ờ|ớ|ở|ỡ|ợ|ù|ú|ủ|ũ|ụ|ừ|ứ|ử|ữ|ự|ỳ|ý|ỷ|ỹ|ỵ|À|Á|Ả|Ã|Ạ|Ầ|Ấ|Ẩ|Ẫ|Ậ|Ằ|Ắ|Ẳ|Ẵ|Ặ|È|É|Ẻ|Ẽ|Ẹ|Ề|Ế|Ể|Ễ|Ệ|Ì|Í|Ỉ|Ĩ|Ị|Ò|Ó|Ỏ|Õ|Ọ|Ồ|Ố|Ổ|Ỗ|Ộ|Ờ|Ớ|Ở|Ỡ|Ợ|Ù|Ú|Ủ|Ũ|Ụ|Ừ|Ứ|Ử|Ữ|Ự|Ỳ|Ý|Ỷ|Ỹ|Ỵ'
charutf8 = 'à|á|ả|ã|ạ|ầ|ấ|ẩ|ẫ|ậ|ằ|ắ|ẳ|ẵ|ặ|è|é|ẻ|ẽ|ẹ|ề|ế|ể|ễ|ệ|ì|í|ỉ|ĩ|ị|ò|ó|ỏ|õ|ọ|ồ|ố|ổ|ỗ|ộ|ờ|ớ|ở|ỡ|ợ|ù|ú|ủ|ũ|ụ|ừ|ứ|ử|ữ|ự|ỳ|ý|ỷ|ỹ|ỵ|À|Á|Ả|Ã|Ạ|Ầ|Ấ|Ẩ|Ẫ|Ậ|Ằ|Ắ|Ẳ|Ẵ|Ặ|È|É|Ẻ|Ẽ|Ẹ|Ề|Ế|Ể|Ễ|Ệ|Ì|Í|Ỉ|Ĩ|Ị|Ò|Ó|Ỏ|Õ|Ọ|Ồ|Ố|Ổ|Ỗ|Ộ|Ờ|Ớ|Ở|Ỡ|Ợ|Ù|Ú|Ủ|Ũ|Ụ|Ừ|Ứ|Ử|Ữ|Ự|Ỳ|Ý|Ỷ|Ỹ|Ỵ'
char1252 = char1252.split('|')
charutf8 = charutf8.split('|')
dic = {}
for i in range(len(char1252)): dic[char1252[i]] = charutf8[i]
return re.sub(
r'à|á|ả|ã|ạ|ầ|ấ|ẩ|ẫ|ậ|ằ|ắ|ẳ|ẵ|ặ|è|é|ẻ|ẽ|ẹ|ề|ế|ể|ễ|ệ|ì|í|ỉ|ĩ|ị|ò|ó|ỏ|õ|ọ|ồ|ố|ổ|ỗ|ộ|ờ|ớ|ở|ỡ|ợ|ù|ú|ủ|ũ|ụ|ừ|ứ|ử|ữ|ự|ỳ|ý|ỷ|ỹ|ỵ|À|Á|Ả|Ã|Ạ|Ầ|Ấ|Ẩ|Ẫ|Ậ|Ằ|Ắ|Ẳ|Ẵ|Ặ|È|É|Ẻ|Ẽ|Ẹ|Ề|Ế|Ể|Ễ|Ệ|Ì|Í|Ỉ|Ĩ|Ị|Ò|Ó|Ỏ|Õ|Ọ|Ồ|Ố|Ổ|Ỗ|Ộ|Ờ|Ớ|Ở|Ỡ|Ợ|Ù|Ú|Ủ|Ũ|Ụ|Ừ|Ứ|Ử|Ữ|Ự|Ỳ|Ý|Ỷ|Ỹ|Ỵ',
lambda x: dic[x.group()], text
)
# Standardize accent typing
vowels_to_ids = {}
vowels_table = [
['a', 'à', 'á', 'ả', 'ã', 'ạ', 'a' ],
['ă', 'ằ', 'ắ', 'ẳ', 'ẵ', 'ặ', 'aw'],
['â', 'ầ', 'ấ', 'ẩ', 'ẫ', 'ậ', 'aa'],
['e', 'è', 'é', 'ẻ', 'ẽ', 'ẹ', 'e' ],
['ê', 'ề', 'ế', 'ể', 'ễ', 'ệ', 'ee'],
['i', 'ì', 'í', 'ỉ', 'ĩ', 'ị', 'i' ],
['o', 'ò', 'ó', 'ỏ', 'õ', 'ọ', 'o' ],
['ô', 'ồ', 'ố', 'ổ', 'ỗ', 'ộ', 'oo'],
['ơ', 'ờ', 'ớ', 'ở', 'ỡ', 'ợ', 'ow'],
['u', 'ù', 'ú', 'ủ', 'ũ', 'ụ', 'u' ],
['ư', 'ừ', 'ứ', 'ử', 'ữ', 'ự', 'uw'],
['y', 'ỳ', 'ý', 'ỷ', 'ỹ', 'ỵ', 'y' ]
]
for i in range(len(vowels_table)):
for j in range(len(vowels_table[i]) - 1):
vowels_to_ids[vowels_table[i][j]] = (i, j)
def is_valid_vietnamese_word(word):
chars = list(word)
vowel_indexes = -1
for index, char in enumerate(chars):
x, y = vowels_to_ids.get(char, (-1, -1))
if x != -1:
if vowel_indexes == -1: vowel_indexes = index
else:
if index - vowel_indexes != 1: return False
vowel_indexes = index
return True
def standardize_word_typing(word):
if not is_valid_vietnamese_word(word): return word
chars = list(word)
dau_cau = 0
vowel_indexes = []
qu_or_gi = False
for index, char in enumerate(chars):
x, y = vowels_to_ids.get(char, (-1, -1))
if x == -1: continue
elif x == 9: # check qu
if index != 0 and chars[index - 1] == 'q':
chars[index] = 'u'
qu_or_gi = True
elif x == 5: # check gi
if index != 0 and chars[index - 1] == 'g':
chars[index] = 'i'
qu_or_gi = True
if y != 0:
dau_cau = y
chars[index] = vowels_table[x][0]
if not qu_or_gi or index != 1:
vowel_indexes.append(index)
if len(vowel_indexes) < 2:
if qu_or_gi:
if len(chars) == 2:
x, y = vowels_to_ids.get(chars[1])
chars[1] = vowels_table[x][dau_cau]
else:
x, y = vowels_to_ids.get(chars[2], (-1, -1))
if x != -1: chars[2] = vowels_table[x][dau_cau]
else: chars[1] = vowels_table[5][dau_cau] if chars[1] == 'i' else vowels_table[9][dau_cau]
return ''.join(chars)
return word
for index in vowel_indexes:
x, y = vowels_to_ids[chars[index]]
if x == 4 or x == 8: # ê, ơ
chars[index] = vowels_table[x][dau_cau]
return ''.join(chars)
if len(vowel_indexes) == 2:
if vowel_indexes[-1] == len(chars) - 1:
x, y = vowels_to_ids[chars[vowel_indexes[0]]]
chars[vowel_indexes[0]] = vowels_table[x][dau_cau]
else:
x, y = vowels_to_ids[chars[vowel_indexes[1]]]
chars[vowel_indexes[1]] = vowels_table[x][dau_cau]
else:
x, y = vowels_to_ids[chars[vowel_indexes[1]]]
chars[vowel_indexes[1]] = vowels_table[x][dau_cau]
return ''.join(chars)
def standardize_sentence_typing(text):
words = text.lower().split()
for index, word in enumerate(words):
cw = re.sub(r'(^\p{P}*)([p{L}.]*\p{L}+)(\p{P}*$)', r'\1/\2/\3', word).split('/')
if len(cw) == 3: cw[1] = standardize_word_typing(cw[1])
words[index] = ''.join(cw)
return ' '.join(words)
# Normalize acronyms
# !wget https://gist.githubusercontent.com/nguyenvanhieuvn/7d9441c10b3c2739499fc5a4d9ea06fb/raw/df939245b3e841b62af115be4dcb3516dadc9fc5/teencode.txt
replace_list = {
'ô kêi': 'ok', 'okie': 'ok', 'o kê': 'ok', 'okey': 'ok', 'ôkê': 'ok', 'oki': 'ok', 'oke': 'ok', 'okay': 'ok', 'okê': 'ok',
'tks': 'cảm ơn', 'thks': 'cảm ơn', 'thanks': 'cảm ơn', 'ths': 'cảm ơn', 'thank': 'cảm ơn',
'kg': 'không', 'not': 'không', 'k': 'không', 'kh': 'không', 'kô': 'không', 'hok': 'không', 'ko': 'không', 'khong': 'không', 'kp': 'không phải',
'he he': 'tích cực', 'hehe': 'tích cực', 'hihi': 'tích cực', 'haha': 'tích cực', 'hjhj': 'tích cực', 'thick': 'tích cực',
'cc': 'tiêu cực', 'huhu': 'tiêu cực', 'cute': 'dễ thương',
'sz': 'cỡ', 'size': 'cỡ',
'wa': 'quá', 'wá': 'quá', 'qá': 'quá',
'đx': 'được', 'dk': 'được', 'dc': 'được', 'đk': 'được', 'đc': 'được',
'vs': 'với', 'j': 'gì', '“': ' ', 'time': 'thời gian', 'm': 'mình', 'mik': 'mình', 'r': 'rồi', 'bjo': 'bao giờ', 'very': 'rất',
'authentic': 'chuẩn chính hãng', 'aut': 'chuẩn chính hãng', 'auth': 'chuẩn chính hãng', 'date': 'hạn sử dụng', 'hsd': 'hạn sử dụng',
'store': 'cửa hàng', 'sop': 'cửa hàng', 'shopE': 'cửa hàng', 'shop': 'cửa hàng',
'sp': 'sản phẩm', 'product': 'sản phẩm', 'hàg': 'hàng',
'ship': 'giao hàng', 'delivery': 'giao hàng', 'síp': 'giao hàng', 'order': 'đặt hàng',
'gud': 'tốt', 'wel done': 'tốt', 'good': 'tốt', 'gút': 'tốt', 'tot': 'tốt', 'nice': 'tốt', 'perfect': 'rất tốt',
'quality': 'chất lượng', 'chất lg': 'chất lượng', 'chat': 'chất', 'excelent': 'hoàn hảo', 'bt': 'bình thường',
'sad': 'tệ', 'por': 'tệ', 'poor': 'tệ', 'bad': 'tệ',
'beautiful': 'đẹp tuyệt vời', 'dep': 'đẹp',
'xau': 'xấu', 'sấu': 'xấu',
'thik': 'thích', 'iu': 'yêu', 'fake': 'giả mạo',
'quickly': 'nhanh', 'quick': 'nhanh', 'fast': 'nhanh',
'fresh': 'tươi', 'delicious': 'ngon',
'dt': 'điện thoại', 'fb': 'facebook', 'face': 'facebook', 'ks': 'khách sạn', 'nv': 'nhân viên',
'nt': 'nhắn tin', 'ib': 'nhắn tin', 'tl': 'trả lời', 'trl': 'trả lời', 'rep': 'trả lời',
'fback': 'feedback', 'fedback': 'feedback',
'sd': 'sử dụng', 'sài': 'xài',
'^_^': 'tích cực', ':)': 'tích cực', ':(': 'tiêu cực',
'❤️': 'tích cực', '👍': 'tích cực', '🎉': 'tích cực', '😀': 'tích cực', '😍': 'tích cực', '😂': 'tích cực', '🤗': 'tích cực', '😙': 'tích cực', '🙂': 'tích cực',
'😔': 'tiêu cực', '😓': 'tiêu cực',
'⭐': 'star', '*': 'star', '🌟': 'star',
}
with open('teencode.txt', encoding='utf-8') as f:
for pair in f.readlines():
key, value = pair.split('\t')
replace_list[key] = value.strip()
def normalize_acronyms(text):
words = []
for word in text.strip().split():
# word = word.strip(string.punctuation)
if word.lower() not in replace_list.keys(): words.append(word)
else: words.append(replace_list[word.lower()])
return emoji.demojize(' '.join(words)) # Remove Emojis
# Remove unnecessary characters
def remove_unnecessary_characters(text):
text = re.sub(r'[^\s\wáàảãạăắằẳẵặâấầẩẫậéèẻẽẹêếềểễệóòỏõọôốồổỗộơớờởỡợíìỉĩịúùủũụưứừửữựýỳỷỹỵđÁÀẢÃẠĂẮẰẲẴẶÂẤẦẨẪẬÉÈẺẼẸÊẾỀỂỄỆÓÒỎÕỌÔỐỒỔỖỘƠỚỜỞỠỢÍÌỈĨỊÚÙỦŨỤƯỨỪỬỮỰÝỲỶỸỴĐ_]', ' ', text)
text = re.sub(r'\s+', ' ', text).strip() # Remove extra whitespace
return text
def preprocess_fn(text, word_tokenize = None):
text = text.strip()
#tokens = sent.split()
#for token in tokens:
#for t in token:
#if t.isnumeric() or t.isdigit():
#tokens.remove(token)
#break
text = remove_HTML(text)
text = convert_unicode(text)
text = standardize_sentence_typing(text)
text = normalize_acronyms(text)
tokens = text.split()
if word_tokenize is None:
text= underthesea.word_tokenize(" ".join(tokens), format="text")
else:
#for using VnCoreNLP
text = " ".join(word_tokenize.tokenize(" ".join(tokens))[0])
text = remove_unnecessary_characters(text)
# return text.lower()
return text