forked from starcroce/nlp_iob
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hw3.py
268 lines (214 loc) · 7.06 KB
/
hw3.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
import re
class FilePreProcess():
word_dict = {}
word_list = []
def __init__(self, filename):
self.filein = open(filename)
def file_close(self):
self.filein.close()
def get_word_count(self):
for line in self.filein:
line_list = line.split('\t')
self.word_list.append(line_list[0])
for each in self.word_list:
if not self.word_dict.has_key(each):
self.word_dict[each] = 1
else:
self.word_dict[each] += 1
def replace_train_file(self, filein, fileout):
filein = open(filein, 'r')
fileout = open(fileout, 'w')
pattern_num = re.compile(r'.*\d+.*')
pattern_mix = re.compile(r'(?:^[a-z]+[A-Z]+[a-zA-Z]*|^[A-Z]+[a-z]+[a-zA-Z]*)')
for line in filein:
line_list = line.split('\t')
word = line_list[0]
if self.word_dict[word] == 1:
if len(word) >= 8:
line_list[0] = 'UNKOWN_long'
elif pattern_num.match(word) != None:
line_list[0] = 'UNKOWN_num'
elif word.isupper():
line_list[0] = 'UNKOWN_upper'
elif pattern_mix.match(word) != None:
line_list[0] = 'UNKOWN_mix'
else:
line_list[0] = 'UNKOWN_other'
line = '\t'.join(line_list)
fileout.write(line)
filein.close()
fileout.close()
def replace_test_file(self, filein, fileout, word_set):
filein = open(filein, 'r')
fileout = open(fileout, 'w')
pattern_num = re.compile(r'.*\d+.*')
pattern_mix = re.compile(r'(?:^[a-z]+[A-Z]+[a-zA-Z]*|^[A-Z]+[a-z]+[a-zA-Z]*)')
for word in filein:
if word != '\n':
word = word.rstrip('\n')
if not word in word_set:
if len(word) >= 8:
word = 'UNKOWN_long'
elif pattern_num.match(word) != None:
word = 'UNKOWN_num'
elif word.isupper():
word = 'UNKOWN_upper'
elif pattern_mix.match(word) != None:
word = 'UNKOWN_mix'
else:
word = 'UNKOWN_other'
fileout.write(word + '\n')
else:
fileout.write(word)
filein.close()
fileout.close()
class FileProcess():
unique_word_list = []
def __init__(self, filename):
self.filein = open(filename)
def file_close(self):
self.filein.close()
def get_start_prob_dict(self):
self.filein.seek(0)
start_prob_dict = {}
sentence_num = 0
for line in self.filein:
if line == '\n':
sentence_num += 1
start_prob_dict['B\n'] = 1.0 / (sentence_num + 2)
start_prob_dict['I\n'] = 1.0 / (sentence_num + 2)
start_prob_dict['O\n'] = 1 - start_prob_dict['B\n'] - start_prob_dict['I\n']
return start_prob_dict
def get_tag_trans_prob_dict(self):
self.filein.seek(0)
tag_list = []
for line in self.filein:
if line != '\n':
line_list = line.split('\t')
tag_list.append(line_list[1])
trans_item_list = []
for i in range(0, len(tag_list)-1):
item = [tag_list[i], tag_list[i + 1]]
trans_item_list.append(item)
B_trans_prob_dict = {}
B_trans_prob_dict['B\n'] = (trans_item_list.count(['B\n', 'B\n']) + 1) / (16637.0 + 3.0)
B_trans_prob_dict['I\n'] = (trans_item_list.count(['B\n', 'I\n']) + 1) / (16637.0 + 3.0)
B_trans_prob_dict['O\n'] = (trans_item_list.count(['B\n', 'O\n']) + 1) / (16637.0 + 3.0)
I_trans_prob_dict = {}
I_trans_prob_dict['B\n'] = (trans_item_list.count(['I\n', 'B\n']) + 1) / (24435.0 + 3.0)
I_trans_prob_dict['I\n'] = (trans_item_list.count(['I\n', 'I\n']) + 1) / (24435.0 + 3.0)
I_trans_prob_dict['O\n'] = (trans_item_list.count(['I\n', 'O\n']) + 1) / (24435.0 + 3.0)
O_trans_prob_dict = {}
O_trans_prob_dict['B\n'] = (trans_item_list.count(['O\n', 'B\n']) + 1) / (345128.0 + 3.0)
O_trans_prob_dict['I\n'] = (trans_item_list.count(['O\n', 'I\n']) + 1) / (345128.0 + 3.0)
O_trans_prob_dict['O\n'] = (trans_item_list.count(['O\n', 'O\n']) + 1) / (345128.0 + 3.0)
tag_trans_prob_dict = {}
tag_trans_prob_dict['B\n'] = B_trans_prob_dict
tag_trans_prob_dict['I\n'] = I_trans_prob_dict
tag_trans_prob_dict['O\n'] = O_trans_prob_dict
return tag_trans_prob_dict
def get_tag_word_prob_dict(self):
self.filein.seek(0)
B_list = []
I_list = []
O_list = []
word_list = []
for line in self.filein:
if line != '\n':
line_list = line.split('\t')
word_list.append(line_list[0])
if line_list[1] == 'B\n':
B_list.append(line_list[0])
elif line_list[1] == 'I\n':
I_list.append(line_list[0])
else:
O_list.append(line_list[0])
self.unique_word_list = list(set(word_list))
B_dict = {}
I_dict = {}
O_dict = {}
for word in self.unique_word_list:
B_dict[word] = 0
I_dict[word] = 0
O_dict[word] = 0
for word in B_list:
B_dict[word] += 1
for word in I_list:
I_dict[word] += 1
for word in O_list:
O_dict[word] += 1
for word in self.unique_word_list:
B_dict[word] = B_dict[word] / 16637.0
I_dict[word] = I_dict[word] / 24435.0
O_dict[word] = O_dict[word] / 345128.0
tag_word_prob_dict = {}
tag_word_prob_dict['B\n'] = B_dict
tag_word_prob_dict['I\n'] = I_dict
tag_word_prob_dict['O\n'] = O_dict
return tag_word_prob_dict
def viterbi(obs, states, start_p, trans_p, emit_p):
if len(obs) < 2:
return ['O\n']
V = [{y:(start_p[y] * emit_p[y][obs[0]]) for y in states}]
path = {y:[y] for y in states}
for y in states:
V[0][y] = start_p[y] * emit_p[y][obs[0]]
path[y] = [y]
for t in range(1, len(obs)):
V.append({})
newpath = {}
for y in states:
(prob, state) = max((V[t-1][y0] * trans_p[y0][y] * emit_p[y][obs[t]], y0) for y0 in states)
V[t][y] = prob
newpath[y] = path[state] + [y]
path = newpath
(prob, state) = max((V[t][y], y) for y in states)
return path[state]
def read_test_file(filename):
filein = open(filename, 'r')
total_line = ''
for line in filein:
total_line += line
sentence_list = total_line.split('\n\n')
test_list = []
for each in sentence_list:
words = each.split('\n')
if '' in words:
words.remove('')
test_list.append(tuple(words))
return test_list
def output_result(word_list, tag_list, output_filename):
fileout = open(output_filename, 'w')
for i in range(0, len(word_list)):
for j in range(0, len(word_list[i])):
line = word_list[i][j] + '\t' + tag_list[i][j]
fileout.write(line)
fileout.write('\n')
fileout.close()
def main():
filename = 'gene.train.txt'
train_pre_process = FilePreProcess(filename)
train_pre_process.get_word_count()
new_filename = 'modified.train.txt'
train_pre_process.replace_train_file(filename, new_filename)
train_pre_process.file_close()
train_process = FileProcess(new_filename)
start_prob_dict = train_process.get_start_prob_dict()
tag_trans_prob_dict = train_process.get_tag_trans_prob_dict()
tag_word_prob_dict = train_process.get_tag_word_prob_dict()
train_process.file_close()
testname = 'gene.dev'
test_pre_process = FilePreProcess(testname)
new_testname = 'modified.gene.dev'
word_set = set(train_process.unique_word_list)
test_pre_process.replace_test_file(testname, new_testname, word_set)
tags_tuple = ('B\n', 'I\n', 'O\n')
test_word_list = read_test_file(new_testname)
res_list = []
for words_tuple in test_word_list:
path = viterbi(words_tuple, tags_tuple, start_prob_dict, tag_trans_prob_dict, tag_word_prob_dict)
res_list.append(path)
raw_word_list = read_test_file(testname)
output_result(raw_word_list, res_list, 'output.gene.txt')
if __name__ == '__main__':
main()