-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocess_category.py
126 lines (100 loc) · 4.09 KB
/
preprocess_category.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
import preprocess as my_preproc
import os
def add_spaces_category():
src_dir = 'category'
dst_dir = 'category_add_spaces'
filenames = []
for (dirpath, dirnames, filenames1) in os.walk(src_dir):
filenames = filenames1
break
for filename in filenames:
with open(f'{dst_dir}/{filename}', 'w') as fp1:
pass
with open(f'{src_dir}/{filename}', 'r') as fp:
for line in fp:
try:
title, q = line.split('\t')
except Exception as e:
print(e)
print(line.split('\t'))
print(filename, title, q)
dst_title = my_preproc.add_space_in_sentence(title)
dst_q = my_preproc.add_space_in_sentence(q)
with open(f'{dst_dir}/{filename}', 'a') as fp1:
fp1.write(f'{dst_title}\t{dst_q}\n')
def remove_non_alphabet_unicode():
src_dir = 'category_add_spaces'
dst_dir = 'category_only_alphabet'
filenames = []
for (dirpath, dirnames, filenames1) in os.walk(src_dir):
filenames = filenames1
break
for filename in filenames:
with open(f'{dst_dir}/{filename}', 'w') as fp1:
pass
with open(f'{src_dir}/{filename}', 'r') as fp:
for line in fp:
try:
title, q = line.split('\t')
except Exception as e:
print(e)
print(line.split('\t'))
print(filename, title, q)
dst_title = my_preproc.remove_special_character_number(title)
dst_q = my_preproc.remove_special_character_number(q)
with open(f'{dst_dir}/{filename}', 'a') as fp1:
fp1.write(f'{dst_title}\t{dst_q}')
def tokenize_word():
"""
using CocCoc tokenizer
"""
src_dir = 'category_only_alphabet'
dst_dir = 'category_tokenizer'
filenames = []
for (dirpath, dirnames, filenames1) in os.walk(src_dir):
filenames = filenames1
break
for filename in filenames:
with open(f'{dst_dir}/{filename}', 'w') as fp1:
pass
with open(f'{src_dir}/{filename}', 'r') as fp:
for line in fp:
try:
title, q = line.split('\t')
except Exception as e:
print(e)
print(line.split('\t'))
print(filename, title, q)
dst_title = my_preproc.tokenizer(title)
dst_q = my_preproc.tokenizer(q)
with open(f'{dst_dir}/{filename}', 'a') as fp1:
fp1.write(f'{dst_title}\t{dst_q}')
def remove_stop_words():
src_dir = 'category_tokenizer'
dst_dir = 'category_non_stop_words'
stop_multiple_words = ['bác sỹ']
stop_words = ['có', 'em', 'bị', 'và', 'bác_sĩ', 'tôi', 'thì', 'bệnh_viện', 'medlatec', 'cám_ơn', 'cảm_ơn', 'nguyên_nhân', 'hỏi',
'e', 'ạ', 'vâng', 'bs', 'bv', 'nhưng', 'chào', 'là']
filenames = []
for (dirpath, dirnames, filenames1) in os.walk(src_dir):
filenames = filenames1
break
for filename in filenames:
with open(f'{dst_dir}/{filename}', 'w') as fp1:
pass
with open(f'{src_dir}/{filename}', 'r') as fp:
for line in fp:
try:
title, q = line.split('\t')
except Exception as e:
print(e)
print(line.split('\t'))
print(filename, title, q)
dst_title = my_preproc.remove_stop_words(title, stop_multiple_words, stop_words)
dst_q = my_preproc.remove_stop_words(q, stop_multiple_words, stop_words)
with open(f'{dst_dir}/{filename}', 'a') as fp1:
fp1.write(f'{dst_title}\t{dst_q}\n')
if __name__ == '__main__':
#remove_only_alphabet_unicode()
remove_stop_words()
#tokenize_word()