-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevalue.py
More file actions
161 lines (143 loc) · 5.01 KB
/
evalue.py
File metadata and controls
161 lines (143 loc) · 5.01 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
import json, tqdm, sys, copy
def getmetric(prednum,goldnum,tt,classprednum=None,classgoldnum=None,classtt=None):
p = 0
r = 0
f1 = 0
if prednum > 0:
p = tt/prednum
if goldnum > 0:
r = tt/goldnum
if p > 0 and r > 0:
f1 = 2*p * r / (p+r)
result = {'p':p,'r':r,'f1':f1}
if classprednum is not None:
result['typef1'] = {}
for label in classprednum:
p = 0
r = 0
f1 = 0
if classprednum[label] > 0:
p = classtt[label]/classprednum[label]
if classgoldnum[label] > 0:
r = classtt[label]/classgoldnum[label]
if p > 0 and r > 0:
f1 = 2*p * r / (p+r)
result['typef1'][label] = {'p':p,'r':r,'f1':f1}
return result
def macroupdate(f1,macrof1=None):
if macrof1 is None:
macrof1 = {
'p': [f1['p']],
'r': [f1['r']],
'f1': [f1['f1']],
}
if 'typef1' in f1:
macrof1['typef1'] = {}
for label in f1['typef1']:
macrof1['typef1'][label] = {}
macrof1['typef1'][label]['p'] = [f1['typef1'][label]['p']]
macrof1['typef1'][label]['r'] = [f1['typef1'][label]['r']]
macrof1['typef1'][label]['f1'] = [f1['typef1'][label]['f1']]
else:
macrof1['p'].append(f1['p'])
macrof1['r'].append(f1['r'])
macrof1['f1'].append(f1['f1'])
if 'typef1' in macrof1:
for label in f1['typef1']:
macrof1['typef1'][label]['p'].append(f1['typef1'][label]['p'])
macrof1['typef1'][label]['r'].append(f1['typef1'][label]['r'])
macrof1['typef1'][label]['f1'].append(f1['typef1'][label]['f1'])
return macrof1
def filterpred(preds):
textoffset = []
if len(preds) == 1:
return preds[0]
offsetnum = {}
for pred in preds:
offset = []
for entity in pred:
entitytype = entity['type']
if entitytype not in offsetnum:
offsetnum[entitytype] = {}
for i in range(entity['offset'][0],entity['offset'][1]):
offset.append(i)
if i not in offsetnum[entitytype]:
offsetnum[entitytype][i] = 1
else:
offsetnum[entitytype][i] += 1
newpred = []
for pred in preds:
for entity in pred:
entitytype = entity['type']
maxthisnum = 0
maxothernum = 0
for i in range(entity['offset'][0],entity['offset'][1]):
if i in textoffset:
maxothernum = 3
break
maxthisnum = max(maxthisnum,offsetnum[entitytype][i])
for j in offsetnum:
if j != entitytype and i in offsetnum[j]:
maxothernum = max(maxothernum,offsetnum[j][i])
if maxthisnum > maxothernum:
newpred.append(entity)
for i in range(entity['offset'][0],entity['offset'][1]):
textoffset.append(i)
return newpred
def writeresult(modelfile,f1):
filename = ['/f1','/error','/errorinfo','/wrongmap']
json.dump(f1,open(modelfile+filename[0]+'.json','w'))
def evaluefunc(results,targetlabel):
print('evalue')
f1 = []
lastpred = []
lastdata = 0
lastindex = 0
prednum = 0
goldnum = 0
tt = 0
classprednum = {}
classgoldnum = {}
classtt = {}
wrongmap = {}
for label in targetlabel:
classprednum[label] = 0
classgoldnum[label] = 0
classtt[label] = 0
wrongmap[label] = {}
results.append({'index':-1,'pred':[]})
for result in tqdm.tqdm(results):
index = result['index']
if index == lastindex:
lastdata = result
pred = result['pred']
lastpred.append(pred)
continue
else:
data = lastdata
gold = data['gold']
lastpred = filterpred(lastpred)
prednum += len(lastpred)
goldnum += len(gold)
for entity in gold:
classgoldnum[entity['type']] += 1
for entity in lastpred:
classprednum[entity['type']] += 1
predentitytext = [[j['text'],j['offset']] for j in lastpred]
for entityindex,entity in enumerate(gold):
if entity in lastpred:
tt += 1
classtt[entity['type']] += 1
else:
if [entity['text'],entity['offset']] in predentitytext:
newentity = copy.deepcopy(entity)
pred = result['pred']
lastpred = [pred]
lastindex = index
lastdata = result
f1 = getmetric(prednum,goldnum,tt,classprednum,classgoldnum,classtt)
sys.stdout.write('p:{0:.4f}, r:{1:.4f}, f1: {2:.4f}'.format(
f1['p'],f1['r'], f1['f1']) + '\r')
sys.stdout.write('\n')
print('\n')
return f1