-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocessing.py
More file actions
185 lines (121 loc) · 4.6 KB
/
preprocessing.py
File metadata and controls
185 lines (121 loc) · 4.6 KB
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
#!/usr/bin/env python
# coding: utf-8
# In[6]:
'''Implement a preprocessing script (preprocess.py) that reads the original file in
the training or gold folder and produce an input file with no spaces and a label
file in the BIES format.'''
# In[41]:
import os
import re
#from sklearn.preprocessing import OneHotEncoder, LabelEncoder
from sklearn import preprocessing
# In[48]:
def bies_tag(input_file, output_file):
with open(input_file) as input_data, open(output_file, 'w+') as output_data:
for line in input_data:
word_list = line.strip().split()
for word in word_list:
if len(word) == 1 or (len(word) > 2 and word[0] == '<' and word[-1] == '>'):
output_data.write("S")
else:
output_data.write("B")
for w in word[1:len(word) - 1]:
output_data.write("I")
output_data.write("E")
output_data.write("\n")
def normalize(ustring):
rstring = ""
for uchar in ustring:
inside_code = ord(uchar)
if inside_code == 12288:
inside_code = 32
elif 65281 <= inside_code <= 65374:
inside_code -= 65248
rstring += chr(inside_code)
return rstring
def filter_before_tag(text):
#remove only n
text = normalize(text)
rNUM = u'(-|\+)?\d+((\.|·)\d+)?%?'
rENG = u'[A-Za-z_.]+'
non_eng = re.sub(rENG, '', text)
non_num = re.sub(rNUM, '', non_eng)
s = re.sub("[\u3000\t\ax03]", '', non_num)
return s
def filter_after_tag(text):
s = text.replace(" ", "")
#s = s.replace("\n", "")
return s
def preprocess_before_tag(src, des, split_long_sentence=False):
with open(src) as src, open(des, 'w+') as des:
for line in src:
sent = filter_before_tag(line)
des.write(sent)
# if len(''.join(sent)) > 200:
# print(' '.join(sent))
def preprocess_after_tag(src, des, split_long_sentence=False):
with open(src) as src, open(des, 'w+') as des:
for line in src:
sent = filter_after_tag(line)
des.write(sent)
def getres_from_folder(filename, folder):
abs = os.getcwd()
res = abs + '/../resources'
train = res + folder
sample_f = train + filename
return sample_f
def encode_bies_labels(bies_file, bies_encoded_file):
with open(bies_file) as bies, open(bies_encoded_file, 'w+') as encoded:
#use LabelEncoder class
token_list = []
le = preprocessing.LabelEncoder()
for line in bies:
bies_line = line.split()
for single in bies_line:
token_list.append(single)
le.fit(token_list)
encoded_list = le.transform(token_list)
for encoded_label in encoded_list:
encoded.write(str(encoded_label))
encoded.write("\n")
# In[49]:
def cleaning(data, folder):
appendix = data.split(".")[0]
labelname = appendix + "_" + "label.txt"
train = getres_from_folder(data, folder)
sample_train = getres_from_folder('temp.utf8', folder)
L_train = getres_from_folder(labelname, folder + 'clean/')
TRAIN = getres_from_folder(data, folder + 'clean/')
enc_name = appendix + '_enc_label'
encoded_bies = getres_from_folder(enc_name, folder + 'clean/')
preprocess_before_tag(train, sample_train)
bies_tag(sample_train, L_train)
preprocess_after_tag(sample_train, TRAIN)
encode_bies_labels(L_train, encoded_bies)
# In[ ]:
# In[50]:
#PREPROCESSING OF TRAINING DATA
#datasets = ["as_simpl.utf8"]
datasets = ["as_simpl.utf8", "cityu_simpl.utf8", "msr.utf8", "pku.utf8"]
folders = ["/training/", "/gold/", "/testing/"]
#folders = ["/training/"]
for folder in folders:
for data in datasets:
cleaning(data, folder)
'''for data in datasets:
appendix = data.split("_")[0]
labelname = appendix + "_" + "label.txt"
#TRAINING DATA
train = getres_from_folder(data, '/training/')
sample_train = getres_from_folder('temp.utf8', '/training/')
L_train = getres_from_folder(labelname, '/training/clean/')
TRAIN = getres_from_folder(data, '/training/clean/')
enc_name = appendix + '_enc_label'
encoded_bies = getres_from_folder(enc_name, '/training/clean/')
preprocess_before_tag(train, sample_train)
bies_tag(sample_train, L_train)
preprocess_after_tag(sample_train, TRAIN)
encode_bies_labels(L_train, encoded_bies)'''
#EMBEDDINGS
# In[ ]:
# In[ ]: