-
Notifications
You must be signed in to change notification settings - Fork 9
/
extraction_tri.py
executable file
·276 lines (232 loc) · 8.7 KB
/
extraction_tri.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
"""Chinese Event Extraction.
"""
# modules
import codecs
from collections import defaultdict
import jieba.posseg as pseg
class DataLoader():
def __init__(self):
self.__trigger_train = self._load_data('trigger_train')
self.__argument_train = self._load_data('argument_train')
self.__trigger_test = self._load_data('trigger_test')
self.__argument_test = self._load_data('argument_test')
def _load_data(self, filename):
"""A template for loading train and test set."""
with codecs.open(filename+'.txt', 'r', 'UTF-8') as f:
file_raw = f.read().split('\n')
data_set = list()
tmp = list()
for i in file_raw:
if i:
tmp.append(tuple(i.split('\t')))
else:
if not tmp:
continue
data_set.append(tmp)
tmp = list()
return data_set
def get_trigger_train(self):
return self.__trigger_train
def get_argument_train(self):
return self.__argument_train
def get_trigger_test(self):
return self.__trigger_test
def get_argument_test(self):
return self.__argument_test
class DataLoader_POS(DataLoader):
def __init__(self):
super().__init__()
def _load_data(self, filename):
"""A template for loading train and test set, including POS data."""
try:
with codecs.open(filename+'_pos.txt', 'r', 'UTF-8') as f:
file_raw = f.read().split('\n')
data_set = list()
tmp = list()
for i in file_raw:
if i:
tmp.append(tuple(i.split('\t')))
else:
if not tmp:
continue
data_set.append(tmp)
tmp = list()
return data_set
except FileNotFoundError:
with codecs.open(filename+'.txt', 'r', 'UTF-8') as f:
file_raw = f.read().split('\n')
data_set_words = list()
data_set_tags = list()
tmp = list()
for i in file_raw:
if i:
tmp.append(tuple(i.split('\t')))
else:
if not tmp:
continue
data_set_words.append([i[0] for i in tmp])
data_set_tags.append([i[1] for i in tmp])
tmp = list()
sentences = [''.join(i) for i in data_set_words]
pos_ed = str()
tmp = str()
for i in range(len(sentences)):
s = list(pseg.cut(sentences[i]))
for k in range(len(s)):
word, flag = s[k]
tmp += '%s\t%s\t%s\n' % (word, flag, data_set_tags[i][k])
pos_ed += tmp + '\n'
tmp = str()
with codecs.open(filename+'_pos.txt', 'w', 'UTF-8') as f:
f.write(pos_ed)
self._load_data(filename)
class HMM():
def __init__(self, train_set):
# vocab and tags of the train_set
self.__vocab, self.__tags = self.__train_set_meta(train_set)
# transition probability matrix and observation likelihoods
self.__tpm, self.__obl = self.__train(train_set)
def __train_set_meta(self, train_set):
vocab = set()
tags = set()
for i in train_set:
for j in i:
vocab.update([j[0]])
tags.update([j[1]])
return vocab, tags
def __train(self, train_set):
# Trigram MLE parameter estimation
vocab, tags = self.__vocab, self.__tags
tags.update(['*', 'STOP'])
count = defaultdict(lambda: 0.0)
tpm = defaultdict(lambda: 0.0)
obl = defaultdict(lambda: 0.1)
count_tags = 0
# count
for s in train_set:
count_tags += len(s)
count[('*')] += 1
count[('*', '*')] += 1
count[(s[0][1])] += 1
count[('*', s[0][1])] += 1
count[('*', '*', s[0][1])] += 1
count[(s[1][1])] += 1
count[(s[0][1], s[1][1])] += 1
count[('*', s[0][1], s[1][1])] += 1
count[(s[0][0], s[0][1])] += 1
count[(s[1][0], s[1][1])] += 1
for i in range(2, len(s)):
count[(s[i][1])] += 1
count[(s[i-1][1], s[i][1])] += 1
count[(s[i-2][1], s[i-1][1], s[i][1])] += 1
count[(s[i][0], s[i][1])] += 1
count[('STOP')] += 1
count[(s[-1][1], 'STOP')] += 1
count[(s[-2][1], s[-1][1], 'STOP')] += 1
# smoothing coefficients
l1, l2, l3 = 0, 0, 1
# MLE
for x in vocab:
for s in tags:
obl[(x, s)] = count[(x, s)]/count[(s)]
for s in tags:
if s == '*':
continue
q1 = count[(s)]/count_tags
for v in tags:
if v == 'STOP':
continue
q2 = count[(v, s)]/count[(v)]
for u in tags:
if count[(u, v)] == 0 or u == 'STOP':
continue
q3 = count[(u, v, s)]/count[(u, v)]
tpm[(u, v, s)] += l1*q3 + l2*q2 + l3*q1
return tpm, obl
def decode(self, seq):
# Viterbi Algorithm. seq for a observations sequence (Trigram)
tags = self.__tags
tpm, obl = self.__tpm, self.__obl
l = len(seq)
vit = {}
bp = {}
# definitions
K = {}
K[-2] = {'*'}
K[-1] = {'*'}
for i in range(l):
K[i] = tags
# initialization
vit[(-1, '*', '*')] = 1
# viterbi
for k in range(l):
for u in K[k-1]:
for v in K[k]:
vit[(k, u, v)] = max([vit[(k-1, w, u)] * tpm[(w, u, v)] * obl[(seq[k], v)] for w in K[k-2]])
bp[(k, u, v)] = max(K[k-2], key=lambda w: vit[(k-1, w, u)] * tpm[(w, u, v)] * obl[(seq[k], v)])
tagseq = [0] * l
tagseq[-2], tagseq[-1] = max([(u, v) for u in K[l-2] for v in K[l-1]], \
key=lambda p: vit[(l-1, p[0], p[1])] * tpm[(p[0], p[1], 'STOP')])
for k in range(l-3, -1, -1):
tagseq[k] = bp[(k+2, tagseq[k+1], tagseq[k+2])]
return tagseq
class CRF():
pass
class Processor():
@staticmethod
def process(train, test):
# using HMM to process trigger or argument.
model = HMM(train)
test_seqs = [[p[0] for p in s] for s in test]
result_str = str()
for i in range(len(test_seqs)):
tmp = str()
tagged = model.decode(test_seqs[i])
for j in range(len(tagged)):
tmp += '\t'.join((test[i][j][0], test[i][j][1], tagged[j])) + '\n'
result_str += tmp + '\n'
return result_str
@staticmethod
def eval(which, result_str):
"""Integrated from Zoe's 'eval.py'."""
result = result_str.split('\n')
TP, FP, TN, FN, type_correct, sum = 0, 0, 0, 0, 0, 0
for word in result:
if word.strip():
sum += 1
li = word.strip().split()
if li[1] != 'O' and li[2] != 'O':
TP += 1
if li[1] == li[2]:
type_correct += 1
if li[1] != 'O' and li[2] == 'O':
FN += 1
if li[1] == 'O' and li[2] != 'O':
FP += 1
if li[1] == 'O' and li[2] == 'O':
TN += 1
recall = TP/(TP+FN)
precision = TP/(TP+FP)
accuracy = (TP+TN)/sum
F1 = 2 * precision * recall/(precision+recall)
print('===== ' + which + ' labeling result =====')
print('accuracy: %.4f' % accuracy )
print('type_correct: %.4f' % (type_correct/TP))
print('precision: %.4f' % precision )
print('recall: %.4f' % recall )
print('F1: %.4f' % F1 )
if __name__ == '__main__':
Data = DataLoader()
t_train = Data.get_trigger_train()
a_train = Data.get_argument_train()
t_test = Data.get_trigger_test()
a_test = Data.get_argument_test()
# print(a_test)
t_res_str = Processor.process(t_train, t_test)
with codecs.open('trigger-HMM_result.txt', 'w', 'UTF-8') as t_result_file:
t_result_file.write(t_res_str)
a_res_str = Processor.process(a_train, a_test)
with codecs.open('argument-HMM_result.txt', 'w', 'UTF-8') as a_result_file:
a_result_file.write(a_res_str)
Processor.eval('trigger', t_res_str)
Processor.eval('argument', a_res_str)