-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdgsa_eval.py
68 lines (64 loc) · 2.31 KB
/
dgsa_eval.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
def tag2ot(ote_tag_sequence):
"""
transform ote tag sequence to a sequence of opinion target
:param ote_tag_sequence: tag sequence for ote task
:return:
"""
n_tags = len(ote_tag_sequence)
ot_sequence = []
beg, end = -1, -1
for i in range(n_tags):
tag = ote_tag_sequence[i].split("-")[0]
if len(ote_tag_sequence[i].split("-")) > 1:
polarity = ote_tag_sequence[i].split("-")[-1]
else:
polarity = "NEU"
if tag == 'S':
ot_sequence.append(tuple([i, i, polarity]))
elif tag == 'B':
beg = i
elif tag == 'E':
end = i
if end > beg > -1:
ot_sequence.append(tuple([beg, end, polarity]))
beg, end = -1, -1
return ot_sequence
def match_ot(gold_ote_sequence, pred_ote_sequence):
"""
calculate the number of correctly predicted opinion target
:param gold_ote_sequence: gold standard opinion target sequence
:param pred_ote_sequence: predicted opinion target sequence
:return: matched number
"""
n_hit = 0
for t in pred_ote_sequence:
if t in gold_ote_sequence:
n_hit += 1
return n_hit
def evaluate_ote(gold_ot, pred_ot):
"""
evaluate the model performce for the ote task
:param gold_ot: gold standard ote tags
:param pred_ot: predicted ote tags
:return:
"""
assert len(gold_ot) == len(pred_ot)
n_samples = len(gold_ot)
# number of true positive, gold standard, predicted opinion targets
n_tp_ot, n_gold_ot, n_pred_ot = 0, 0, 0
for i in range(n_samples):
g_ot = gold_ot[i]
p_ot = pred_ot[i]
g_ot_sequence, p_ot_sequence = tag2ot(ote_tag_sequence=g_ot), tag2ot(ote_tag_sequence=p_ot)
# hit number
n_hit_ot = match_ot(gold_ote_sequence=g_ot_sequence, pred_ote_sequence=p_ot_sequence)
n_tp_ot += n_hit_ot
n_gold_ot += len(g_ot_sequence)
n_pred_ot += len(p_ot_sequence)
# add 0.001 for smoothing
# calculate precision, recall and f1 for ote task
ot_precision = float(n_tp_ot) / float(n_pred_ot + 0.001)
ot_recall = float(n_tp_ot) / float(n_gold_ot + 0.001)
ot_f1 = 2 * ot_precision * ot_recall / (ot_precision + ot_recall + 0.001)
ote_scores = (ot_precision, ot_recall, ot_f1)
return ote_scores