forked from v-mipeng/LexiconNER
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_utils.py
369 lines (340 loc) · 14.9 KB
/
data_utils.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# -*- coding: utf-8 -*-
# @Time : 2018/8/6 12:43
# @Author : Xiaoyu Xing
# @File : dataUtils.py
import numpy as np
import os
class DataPrepare(object):
def __init__(self, dataset):
self.tag2Idx = {
"PER": 0, "LOC": 1, "ORG": 2, "MISC": 3
}
self.idx2tag = {
0: "PER", 1: "LOC", 2: "ORG", 3: "MISC"
}
self.case2Idx = {'numeric': 0, 'allLower': 1, 'allUpper': 2, 'initialUpper': 3, 'other': 4, 'mainly_numeric': 5,
'contains_digit': 6, 'PADDING_TOKEN': 7}
self.caseEmbeddings = np.identity(len(self.case2Idx), dtype='float32')
self.char2Idx = {"PADDING": 0, "UNKNOWN": 1}
for c in " 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.,-_()[]{}!?:;#'\"/\\%$`&=*+@^~|":
self.char2Idx[c] = len(self.char2Idx)
self.words = self.get_words(dataset)
self.word2Idx = {}
self.wordEmbeddings = []
with open("data/glove.6B.100d.txt", "r", encoding='utf-8') as fw:
for line in fw:
line = line.strip()
splits = line.split(" ")
if len(self.word2Idx) == 0:
self.word2Idx["PADDING_TOKEN"] = len(self.word2Idx)
vector = np.zeros(len(splits) - 1) # Zero vector vor 'PADDING' word
self.wordEmbeddings.append(vector)
self.word2Idx["UNKNOWN_TOKEN"] = len(self.word2Idx)
vector = np.random.uniform(-0.25, 0.25, len(splits) - 1)
self.wordEmbeddings.append(vector)
if splits[0].lower() in self.words:
vector = np.array([float(num) for num in splits[1:]])
self.wordEmbeddings.append(vector)
self.word2Idx[splits[0]] = len(self.word2Idx)
self.wordEmbeddings = np.array(self.wordEmbeddings)
self.dataset = dataset
def read_origin_file(self, filename):
with open(filename, "r", encoding='utf-8') as fw:
sentences = []
sentence = []
for line in fw:
if len(line) == 0 or line.startswith('-DOCSTART') or line[0] == '\n':
if len(sentence) > 0:
sentences.append(sentence)
sentence = []
continue
else:
splits = line.split(' ')
sentence.append([splits[0].strip(), splits[1].strip(), np.zeros(4)])
if len(sentence) > 0:
sentences.append(sentence)
sentence = []
return sentences
def read_processed_file(self, filename, flag):
"""
return data [[[word, isEntity, isLabeled],[],...],...]
:param filename:
:return:
"""
with open(filename, "r", encoding='utf-8') as fw:
sentences = []
sentence = []
# i = 0
for line in fw:
if len(line) == 0 or line.startswith('-DOCSTART') or line[0] == '\n':
if len(sentence) > 0:
sentences.append(sentence)
sentence = []
continue
else:
# word, trueEntityLabel, dicEntityLabel
splits = line.split(' ')
if len(splits[0].strip()) > 0:
if splits[1].strip() != "-1":
sentence.append([splits[0].strip(), int(
splits[1].strip() == "B-" + flag or splits[1].strip() == "I-" + flag), int(splits[2])])
else:
sentence.append([splits[0].strip(), -1, int(splits[2])])
else:
if splits[1].strip() != "-1":
sentence.append([" ", int(
splits[1].strip() == "B-" + flag or splits[1].strip() == "I-" + flag), int(splits[2])])
else:
sentence.append([splits[0].strip(), -1, int(splits[2])])
if len(sentence) > 0:
sentences.append(sentence)
sentence = []
# print(i)
return sentences
def writeFile(self, fileName, mode, dic, sentences):
if mode == "TRAIN":
with open(fileName, "w") as fw:
for sentence in sentences:
for word, label, tagIdxList in sentence:
if np.sum(tagIdxList == True) == 1 and tagIdxList[self.tag2Idx[dic]] == 1:
labeled = 1
fw.write(word + " " + label + " " + str(labeled) + "\n")
else:
labeled = 0
fw.write(word + " " + label + " " + str(labeled) + "\n")
fw.write("\n")
else:
with open(fileName, "w") as fw:
for sentence in sentences:
for word, label, tagIdxList in sentence:
labeled = 0
fw.write(word + " " + label + " " + str(labeled) + "\n")
fw.write("\n")
def wordLevelGeneration(self, sentences):
newSentences = []
newLabels = []
newPreds = []
for sentence in sentences:
words = []
labels = []
preds = []
for i, (word, label, pred) in enumerate(sentence):
phase = [word]
phase_label = [label]
phase_pred = [pred]
if label != 'O':
splits = label.split("-")
tag = splits[0]
entityLabel = splits[1]
if tag == 'B':
j = i + 1
while j < len(sentence):
if sentence[j][1] != 'O':
tag2 = sentence[j][1].split('-')[0]
entityLabel2 = sentence[j][1].split('-')[1]
if tag2 == 'I' and entityLabel2 == entityLabel:
phase = phase + [sentence[j][0]]
phase_label += [sentence[j][1]]
phase_pred += [sentence[j][2]]
j += 1
if j == len(sentence):
words.append(phase)
labels.append(phase_label)
preds.append(phase_pred)
break
else:
words.append(phase)
labels.append(phase_label)
preds.append(phase_pred)
break
else:
words.append(phase)
labels.append(phase_label)
preds.append(phase_pred)
break
if j - i == 1 and j == len(sentence):
words.append(phase)
labels.append(phase_label)
preds.append(phase_pred)
i += 1
break
assert len(phase) == len(phase_label) == len(phase_pred)
else:
words.append(phase)
labels.append(phase_label)
preds.append(phase_pred)
newSentences.append(words)
newLabels.append(labels)
newPreds.append(preds)
newLabels_ = []
for s in newLabels:
temp = []
for item in s:
if len(item) == 1 and item[0] != "O":
label = item[0].split("-")[-1].strip()
temp.append([label])
elif len(item) > 1:
temp2 = []
for j in item:
newJ = j.split("-")[-1].strip()
temp2.append(newJ)
temp.append(temp2)
elif len(item) == 1 and item[0] == "O":
temp.append([item[0]])
newLabels_.append(temp)
return newSentences, newLabels_, newPreds
def wordLevelGeneration2(self, sentences):
"""
has probablity used in eval
:param sentences:
:return:
"""
newSentences = []
newLabels = []
newPreds = []
newProbs = []
for sentence in sentences:
words = []
labels = []
preds = []
probs = []
for i, (word, label, pred, prob) in enumerate(sentence):
phase = [word]
phase_label = [label]
phase_pred = [pred]
phase_prob = [prob]
if label != 'O':
splits = label.split("-")
tag = splits[0]
entityLabel = splits[1]
if tag == 'B':
j = i + 1
while j < len(sentence):
if sentence[j][1] != 'O':
tag2 = sentence[j][1].split('-')[0]
entityLabel2 = sentence[j][1].split('-')[1]
if tag2 == 'I' and entityLabel2 == entityLabel:
phase = phase + [sentence[j][0]]
phase_label += [sentence[j][1]]
phase_pred += [sentence[j][2]]
phase_prob += [sentence[j][3]]
j += 1
if j == len(sentence):
words.append(phase)
labels.append(phase_label)
preds.append(phase_pred)
probs.append(phase_prob)
break
else:
words.append(phase)
labels.append(phase_label)
preds.append(phase_pred)
probs.append(phase_prob)
break
else:
words.append(phase)
labels.append(phase_label)
preds.append(phase_pred)
probs.append(phase_prob)
break
if j - i == 1 and j == len(sentence):
words.append(phase)
labels.append(phase_label)
preds.append(phase_pred)
probs.append(phase_prob)
i += 1
break
assert len(phase) == len(phase_label) == len(phase_pred) == len(phase_prob)
else:
words.append(phase)
labels.append(phase_label)
preds.append(phase_pred)
probs.append(phase_prob)
newSentences.append(words)
newLabels.append(labels)
newPreds.append(preds)
newProbs.append(probs)
newLabels_ = []
for s in newLabels:
temp = []
for item in s:
if len(item) == 1 and item[0] != "O":
label = item[0].split("-")[-1].strip()
temp.append([label])
elif len(item) > 1:
temp2 = []
for j in item:
newJ = j.split("-")[-1].strip()
temp2.append(newJ)
temp.append(temp2)
elif len(item) == 1 and item[0] == "O":
temp.append([item[0]])
newLabels_.append(temp)
return newSentences, newLabels_, newPreds, newProbs
def compute_precision_recall_f1(self, labels, preds, flag, pflag):
tp = 0
np_ = 0
pp = 0
for i in range(len(labels)):
sent_label = labels[i]
sent_pred = preds[i]
for j in range(len(sent_label)):
item1 = np.array(sent_pred[j])
item2 = np.array(sent_label[j])
if (item1 == pflag).all() == True:
pp += 1
if (item2 == flag).all() == True:
np_ += 1
if (item1 == pflag).all() == True:
tp += 1
if pp == 0:
p = 0
else:
p = float(tp) / float(pp)
if np_ == 0:
r = 0
else:
r = float(tp) / float(np_)
if p == 0 or r == 0:
f1 = 0
else:
f1 = float(2 * p * r) / float((p + r))
return p, r, f1
def get_words(self, dataset):
words = {}
if dataset == "conll2003" or dataset == 'conll2002':
trainSentences = self.read_origin_file("data/" + dataset + "/train.txt")
validSentences = self.read_origin_file("data/" + dataset + "/valid.txt")
testSentences = self.read_origin_file("data/" + dataset + "/test.txt")
for sentences in [trainSentences, validSentences, testSentences]:
for sentence in sentences:
for token, label, flag in sentence:
words[token.lower()] = True
return words
elif dataset == "muc" or dataset == "wikigold" or dataset == "twitter":
trainSentences = self.read_origin_file("data/" + dataset + "/train.txt")
testSentences = self.read_origin_file("data/" + dataset + "/test.txt")
for sentences in [trainSentences, testSentences]:
for sentence in sentences:
for token, label, flag in sentence:
words[token.lower()] = True
return words
else:
raise ValueError("dataset name is wrong")
def read_unlabeled_data(self, filename):
with open(filename, "r") as fw:
sentences = []
sentence = []
for line in fw:
if len(line) == 0 or line.startswith('-DOCSTART') or line[0] == '\n':
if len(sentence) > 0:
sentences.append(sentence)
sentence = []
continue
else:
splits = line.split(' ')
sentence.append([splits[0].strip(), -1, np.zeros(4)])
if len(sentence) > 0:
sentences.append(sentence)
sentence = []
return sentences