-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.py
183 lines (148 loc) · 5.58 KB
/
metrics.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
import os
import config
import logging
def get_entities(seq):
"""
Gets entities from sequence.
Args:
seq (list): sequence of labels.
Returns:
list: list of (chunk_type, chunk_start, chunk_end).
Example:
seq = ['B-PER', 'I-PER', 'O', 'B-LOC']
get_entities(seq)
[('PER', 0, 1), ('LOC', 3, 3)]
"""
# for nested list
if any(isinstance(s, list) for s in seq):
seq = [item for sublist in seq for item in sublist + ['O']]
prev_tag = 'O'
prev_type = ''
begin_offset = 0
chunks = []
for i, chunk in enumerate(seq + ['O']):
tag = chunk[0]
type_ = chunk.split('-')[-1]
if end_of_chunk(prev_tag, tag, prev_type, type_):
chunks.append((prev_type, begin_offset, i - 1))
if start_of_chunk(prev_tag, tag, prev_type, type_):
begin_offset = i
prev_tag = tag
prev_type = type_
return chunks
def end_of_chunk(prev_tag, tag, prev_type, type_):
"""Checks if a chunk ended between the previous and current word.
Args:
prev_tag: previous chunk tag.
tag: current chunk tag.
prev_type: previous type.
type_: current type.
Returns:
chunk_end: boolean.
"""
chunk_end = False
if prev_tag == 'S':
chunk_end = True
# pred_label中可能出现这种情形
if prev_tag == 'B' and tag == 'B':
chunk_end = True
if prev_tag == 'B' and tag == 'S':
chunk_end = True
if prev_tag == 'B' and tag == 'O':
chunk_end = True
if prev_tag == 'I' and tag == 'B':
chunk_end = True
if prev_tag == 'I' and tag == 'S':
chunk_end = True
if prev_tag == 'I' and tag == 'O':
chunk_end = True
if prev_tag != 'O' and prev_tag != '.' and prev_type != type_:
chunk_end = True
return chunk_end
def start_of_chunk(prev_tag, tag, prev_type, type_):
"""Checks if a chunk started between the previous and current word.
Args:
prev_tag: previous chunk tag.
tag: current chunk tag.
prev_type: previous type.
type_: current type.
Returns:
chunk_start: boolean.
"""
chunk_start = False
if tag == 'B':
chunk_start = True
if tag == 'S':
chunk_start = True
if prev_tag == 'S' and tag == 'I':
chunk_start = True
if prev_tag == 'O' and tag == 'I':
chunk_start = True
if tag != 'O' and tag != '.' and prev_type != type_:
chunk_start = True
return chunk_start
def f1_score(y_true, y_pred, mode='dev'):
"""Compute the F1 score.
The F1 score can be interpreted as a weighted average of the precision and
recall, where an F1 score reaches its best value at 1 and worst score at 0.
The relative contribution of precision and recall to the F1 score are
equal. The formula for the F1 score is::
F1 = 2 * (precision * recall) / (precision + recall)
Args:
y_true : 2d array. Ground truth (correct) target values.
y_pred : 2d array. Estimated targets as returned by a tagger.
Returns:
score : float.
Example:
y_true = [['O', 'O', 'O', 'B-MISC', 'I-MISC', 'I-MISC', 'O'], ['B-PER', 'I-PER', 'O']]
y_pred = [['O', 'O', 'B-MISC', 'I-MISC', 'I-MISC', 'I-MISC', 'O'], ['B-PER', 'I-PER', 'O']]
f1_score(y_true, y_pred)
0.50
"""
true_entities = set(get_entities(y_true))
pred_entities = set(get_entities(y_pred))
nb_correct = len(true_entities & pred_entities)
nb_pred = len(pred_entities)
nb_true = len(true_entities)
p = nb_correct / nb_pred if nb_pred > 0 else 0
r = nb_correct / nb_true if nb_true > 0 else 0
score = 2 * p * r / (p + r) if p + r > 0 else 0
if mode == 'dev':
return score
else:
f_score = {}
for label in config.labels:
true_entities_label = set()
pred_entities_label = set()
for t in true_entities:
if t[0] == label:
true_entities_label.add(t)
for p in pred_entities:
if p[0] == label:
pred_entities_label.add(p)
nb_correct_label = len(true_entities_label & pred_entities_label)
nb_pred_label = len(pred_entities_label)
nb_true_label = len(true_entities_label)
p_label = nb_correct_label / nb_pred_label if nb_pred_label > 0 else 0
r_label = nb_correct_label / nb_true_label if nb_true_label > 0 else 0
score_label = 2 * p_label * r_label / (p_label + r_label) if p_label + r_label > 0 else 0
f_score[label] = score_label
return f_score, score
def bad_case(y_true, y_pred, data):
if not os.path.exists(config.case_dir):
os.system(r"touch {}".format(config.case_dir)) # 调用系统命令行来创建文件
output = open(config.case_dir, 'w')
for idx, (t, p) in enumerate(zip(y_true, y_pred)):
if t == p:
continue
else:
output.write("bad case " + str(idx) + ": \n")
output.write("sentence: " + str(data[idx]) + "\n")
output.write("golden label: " + str(t) + "\n")
output.write("model pred: " + str(p) + "\n")
logging.info("--------Bad Cases reserved !--------")
if __name__ == "__main__":
y_t = [['O', 'O', 'O', 'B-address', 'I-address', 'I-address', 'O'], ['B-name', 'I-name', 'O']]
y_p = [['O', 'O', 'B-address', 'I-address', 'I-address', 'I-address', 'O'], ['B-name', 'I-name', 'O']]
sent = [['十', '一', '月', '中', '山', '路', '电'], ['周', '静', '说']]
bad_case(y_t, y_p, sent)