-
Notifications
You must be signed in to change notification settings - Fork 0
/
metric.py
55 lines (48 loc) · 1.49 KB
/
metric.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
def compute_sent_metrics(results):
err_sent = [pair[0] for pair in results]
cor_sent = [pair[1] for pair in results]
predict = [pair[2] for pair in results]
assert len(err_sent) == len(cor_sent) == len(predict)
c_tp, c_fp, c_tn, c_fn = 0, 0, 0, 0
d_tp, d_fp, d_tn, d_fn = 0, 0, 0, 0
for es, cs, ps in zip(err_sent, cor_sent, predict):
cs_d = [e == c for e, c in zip(es, cs)]
ps_d = [e == p for e, p in zip(es, ps)]
if es != cs:
if cs == ps:
c_tp += 1
else:
c_fn += 1
if cs_d == ps_d:
d_tp += 1
else:
d_fn += 1
if es == cs:
if cs == ps:
c_tn += 1
else:
c_fp += 1
if cs_d == ps_d:
d_tn += 1
else:
d_fp += 1
d_acc = (d_tp + d_tn) / (d_tp + d_tn + d_fp + d_fn)
d_pre = d_tp / (d_tp + d_fp)
d_rec = d_tp / (d_tp + d_fn)
d_f1 = 2 * d_pre * d_rec / (d_pre + d_rec)
c_acc = (c_tp + c_tn) / (c_tp + c_tn + c_fp + c_fn)
c_pre = c_tp / (c_tp + c_fp)
c_rec = c_tp / (c_tp + c_fn)
c_f1 = 2 * c_pre * c_rec / (c_pre + c_rec)
return {
"sent_d_acc": d_acc,
"sent_d_pre": d_pre,
"sent_d_rec": d_rec,
"sent_d_f1": d_f1,
"sent_c_acc": c_acc,
"sent_c_pre": c_pre,
"sent_c_rec": c_rec,
"sent_c_f1": c_f1
}
def compute_char_metrics(results):
pass