-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathevaluate.py
218 lines (189 loc) · 7.14 KB
/
evaluate.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
import warnings
warnings.filterwarnings("ignore")
from sklearn import metrics
class ConllEvaluator(object):
START_TAGS = {
("B", "B"),
("I", "B"),
("O", "B"),
("O", "I"),
("E", "E"),
("E", "I"),
("O", "E"),
("O", "I")
}
END_TAGS = {
("B", "B"),
("B", "O"),
("I", "B"),
("I", "O"),
("E", "E"),
("E", "I"),
("E", "O"),
("I", "O")
}
def is_chunk_start(self, ptag, tag, ptag_type, tag_type):
return (ptag, tag) in self.START_TAGS or \
(tag != "O" and tag != "." and ptag_type != tag_type)
def is_chunk_end(self, ptag, tag, ptag_type, tag_type):
return (ptag, tag) in self.END_TAGS or \
ptag != 'O' and ptag != '.' and ptag_type != tag_type
@staticmethod
def split_tag(tag):
s = tag.split('-')
if len(s) > 2 or len(s) == 0:
raise ValueError('tag format wrong. it must be B-xxx.xxx')
if len(s) == 1:
tag = s[0]
tag_type = ""
else:
tag = s[0]
tag_type = s[1]
return tag, tag_type
def evaluate(self, golds, preds):
"""
https://github.com/MiuLab/SlotGated-SLU/blob/master/utils.py
:param golds:
:param preds:
:return:
"""
correct_chunk = {}
correct_chunk_cnt = 0
found_correct = {}
found_correct_cnt = 0
found_pred = {}
found_pred_cnt = 0
correct_tags = 0
token_count = 0
for correct_slot, pred_slot in zip(golds, preds):
in_correct = False
last_correct_tag = 'O'
last_correct_type = ''
last_pred_tag = 'O'
last_pred_type = ''
for c, p in zip(correct_slot, pred_slot):
correct_tag, correct_type = self.split_tag(c)
pred_tag, pred_type = self.split_tag(p)
if in_correct:
if self.is_chunk_end(last_correct_tag, correct_tag,
last_correct_type, correct_type) and \
self.is_chunk_end(last_pred_tag, pred_tag,
last_pred_type, pred_type) and \
last_correct_type == last_pred_type:
in_correct = False
correct_chunk_cnt += 1
if last_correct_type in correct_chunk:
correct_chunk[last_correct_type] += 1
else:
correct_chunk[last_correct_type] = 1
elif self.is_chunk_end(last_correct_tag, correct_tag,
last_correct_type, correct_type) != \
self.is_chunk_end(last_pred_tag, pred_tag,
last_pred_type, pred_type) or \
correct_type != pred_type:
in_correct = False
if self.is_chunk_start(last_correct_tag, correct_tag,
last_correct_type, correct_type) and \
self.is_chunk_start(last_pred_tag, pred_tag,
last_pred_type, pred_type) and \
correct_type == pred_type:
in_correct = True
if self.is_chunk_start(last_correct_tag, correct_tag,
last_correct_type, correct_type):
found_correct_cnt += 1
if correct_type in found_correct:
found_correct[correct_type] += 1
else:
found_correct[correct_type] = 1
if self.is_chunk_start(last_pred_tag, pred_tag,
last_pred_type, pred_type):
found_pred_cnt += 1
if pred_type in found_pred:
found_pred[pred_type] += 1
else:
found_pred[pred_type] = 1
if correct_tag == pred_tag and correct_type == pred_type:
correct_tags += 1
token_count += 1
last_correct_tag = correct_tag
last_correct_type = correct_type
last_pred_tag = pred_tag
last_pred_type = pred_type
if in_correct:
correct_chunk_cnt += 1
if last_correct_type in correct_chunk:
correct_chunk[last_correct_type] += 1
else:
correct_chunk[last_correct_type] = 1
if found_pred_cnt > 0:
precision = 100 * correct_chunk_cnt / found_pred_cnt
else:
precision = 0
if found_correct_cnt > 0:
recall = 100 * correct_chunk_cnt / found_correct_cnt
else:
recall = 0
if (precision + recall) > 0:
f1 = (2 * precision * recall) / (precision + recall)
else:
f1 = 0
return {
"overall": {
"f1": f1,
"prec": precision,
"rec": recall
}
}
def replace(items, x, y, z):
return [y if item == x else z for item in items]
def cast_float(d):
if isinstance(d, dict):
return {k: cast_float(v) for k, v in d.items()}
else:
return float(d)
def evaluate_intents(golds, preds):
vocab = set(golds) | set(preds)
measures = (
metrics.precision_score,
metrics.recall_score,
metrics.f1_score
)
ret = {
"overall": {
"acc": metrics.accuracy_score(golds, preds),
"prec": metrics.precision_score(golds, preds, average="micro"),
"rec": metrics.recall_score(golds, preds, average="micro"),
"f1": metrics.f1_score(golds, preds, average="micro"),
},
"intents": {
intent: {
name: measure(
y_true=replace(golds, intent, 1, 0),
y_pred=replace(preds, intent, 1, 0),
average="binary"
) for name, measure in zip(("prec", "rec", "f1"), measures)
} for intent in vocab
}
}
return cast_float(ret)
def strip_bi(sents):
def _strip_bi(w):
if w == "O":
return w
tokens = w.split("-")
if len(tokens) == 1:
return w
return tokens[1]
return [" ".join(_strip_bi(w) for w in sent.split()) for sent in sents]
def evaluate(gold_labels, gold_intents, pred_labels, pred_intents):
return {
"intent-classification": evaluate_intents(gold_intents, pred_intents),
"slot-labeling": ConllEvaluator().evaluate(
golds=[x.split() for x in gold_labels],
preds=[x.split() for x in pred_labels]
),
"sentence-understanding": float(metrics.accuracy_score(
y_true=list(map("/".join, zip(strip_bi(gold_labels), gold_intents))),
y_pred=list(map("/".join, zip(strip_bi(pred_labels), pred_intents)))
))
}