forked from v-mipeng/LexiconNER
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dict_match.py
191 lines (168 loc) · 5.85 KB
/
dict_match.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
# -*- coding: utf-8 -*-
# @Time : 2018/8/6 12:49
# @Author : Xiaoyu Xing
# @File : dict_match.py
import utils.data_utils, utils.dict_utils
import numpy as np
def compute_precision_recall_f1(labels, preds, flag, pflag):
"""
Word level
:param labels:
:param preds:
:param flag:
:param pflag:
:return:
"""
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, tp, np_, pp
def compute_precision_recall_f1_2(labels, preds, flag, pflag):
"""
character level
:param labels:
:param preds:
:param flag:
:param pflag:
:return:
"""
tp = 0
np_ = 0
pp = 0
for i in range(len(labels)):
item1 = np.array(preds[i])
item2 = np.array(labels[i])
if item1 == pflag:
pp += 1
if item2 == flag:
np_ += 1
if item1 == pflag:
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, tp, np_, pp
def getLabelsAndPreds(sentences):
labels = []
preds = []
for sent in sentences:
for word, label, pred in sent:
if len(label.split('-')) > 1:
label = label.split('-')[-1]
else:
label = label
labels.append(label)
preds.append(pred)
return labels, preds
def dict_match_word(dp, dutils, fileName, dictName, flag, mode, dataset):
sentences = dp.read_origin_file(fileName)
length = [len(i) for i in sentences]
maxLen = 10
sentences, num, count = dutils.lookup_in_Dic(dictName, sentences, flag, maxLen)
print(count)
if mode == "TRAIN":
dp.writeFile("data/" + dataset + "/train." + flag + ".txt", mode, flag, sentences)
ss = []
for sentence in sentences:
s = []
for i, (word, label, pred) in enumerate(sentence):
pred = np.array(pred)
if pred[dutils.tag2Idx[flag]] == 1:
s.append([word, label, 1])
else:
s.append([word, label, 0])
ss.append(s)
sentences = ss
labels, preds = getLabelsAndPreds(sentences)
p, r, f1, tp, np_, pp = compute_precision_recall_f1_2(labels, preds, flag, 1)
return p, r, f1, tp, np_, pp
def dict_match_result(dp, dutils, fileName, dictName, flag, mode, dataset, percent=1.0):
sentences = dp.read_origin_file(fileName)
size = int(len(sentences) * percent)
sentences = sentences[:size]
length = [len(i) for i in sentences]
maxLen = 10
sentences, num, _ = dutils.lookup_in_Dic(dictName, sentences, flag, maxLen)
print(num)
if mode == "TRAIN":
dp.writeFile("data/" + dataset + "/valid." + flag + ".txt", mode, flag, sentences)
ss = []
for sentence in sentences:
s = []
for i, (word, label, pred) in enumerate(sentence):
pred = np.array(pred)
if pred[dutils.tag2Idx[flag]] == 1:
s.append([word, label, 1])
else:
s.append([word, label, 0])
ss.append(s)
sentences = ss
newSentences, newLabels, newPreds = dp.wordLevelGeneration(sentences)
p, r, f1, tp, np_, pp = compute_precision_recall_f1(newLabels, newPreds, flag, 1)
return p, r, f1, tp, np_, pp
def count_entity(dataset, type):
filename = "dictionary/" + dataset + "/" + type + ".txt"
s = set()
with open(filename, "r") as fw:
for line in fw:
line = line.strip()
s.add(line)
print(type, len(s))
if __name__ == "__main__":
dp = utils.data_utils.DataPrepare("conll2003")
dutils = utils.dict_utils.DictUtils()
# num = 3
p1, r1, f11, tp, np_1, pp = dict_match_word(dp, dutils,
"data/conll2003/valid.txt",
"dictionary/conll2003/person.txt",
"PER",
"Train", "conll2003")
print("%.4f" % p1, "%.4f" % r1, "%.4f" % f11, tp, np_1, pp)
p2, r2, f12, tp, np_2, pp = dict_match_word(dp, dutils, "data/conll2003/test.txt",
"dictionary/conll2003/location.txt",
"LOC",
"Train", "conll2003")
print("%.4f" % p2, "%.4f" % r2, "%.4f" % f12, tp, np_2, pp)
#
p3, r3, f13, tp, np_3, pp = dict_match_word(dp, dutils, "data/conll2003/test.txt",
"dictionary/conll2003/organization.txt",
"ORG", "Train", "conll2003")
print("%.4f" % p3, "%.4f" % r3, "%.4f" % f13, tp, np_3, pp)
p4, r4, f14, tp, np_4, pp = dict_match_word(dp, dutils, "data/conll2003/test.txt",
"dictionary/conll2003/misc.txt",
"MISC", "Train",
"conll2003")
print("%.4f" % p4, "%.4f" % r4, "%.4f" % f14, tp, np_4, pp)