-
Notifications
You must be signed in to change notification settings - Fork 13
/
ComputePRF.py
147 lines (120 loc) · 4.11 KB
/
ComputePRF.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
from collections import defaultdict
import operator
def compute_prf(tp, fp, fn):
if tp > 0:
p = tp / float(tp + fp)
r = tp / float(tp + fn)
f = 2 * r * p / float(r + p)
else:
p = 0
r = 0
f = 0
return p, r, f
def evaluate(evalfile, goldfile, separator=",", columnsEG=[1, 1], get_confusion=True):
if isinstance(separator, str):
separator = [separator, separator]
gm = defaultdict(list)
langs = []
gold_data = defaultdict(list)
with open(goldfile, mode="r") as gold:
for line in gold:
x = line.strip().split(separator[1])
gold_data[x[0]].append(x[columnsEG[1]])
gm[x[columnsEG[1]]].append(x[0])
if x[columnsEG[1]] not in langs:
langs.append(x[columnsEG[1]])
em = defaultdict(list)
eval_data = defaultdict(list)
with open(evalfile, mode="r") as eval:
for line in eval:
x = line.strip().split(separator[0])
em[x[columnsEG[0]]].append(x[0])
if x[columnsEG[0]] not in langs:
langs.append(x[columnsEG[0]])
eval_data[x[0]].append(x[columnsEG[0]])
if get_confusion:
print("CONFUSION WORKS PROPERLY ONLY IF EACH QUERY CAN HAVE ONLY ONE ANSWER")
confusions = defaultdict(list)
total_confusions = defaultdict(int)
for g in gold_data:
gold_set = []
for l in gold_data[g]:
same = False
if g in eval_data:
for l2 in eval_data[g]:
if l == l2:
same = True
break
if not same:
gold_set.append(l)
eval_set = []
for l in eval_data[g]:
same = False
if g in eval_data:
for l2 in gold_data[g]:
if l == l2:
same = True
break
if not same:
eval_set.append(l)
total_confusions[str(sorted(gold_data[g]))] += 1
if len(gold_set) == 0 or len(eval_set) == 0:
continue
confusions[str(sorted(gold_set))].append(str(sorted(eval_set)))
for l in sorted(confusions):
conf = defaultdict(int)
for x in confusions[l]:
conf[x] += 1
print("Correct {0}, Incorrectly: {1} out of {2}".format(l, len(confusions[l]), total_confusions[l]))
for x in sorted(conf.items(), key=operator.itemgetter(1), reverse=True):
if x[1] <= 0:
break
print("... confused with {0} {1}x".format(x[0], x[1]))
scores = defaultdict(lambda: defaultdict(int))
for lang in langs:
for doc in em[lang]:
if doc in gm[lang]:
scores[lang]["tp"] += 1
else:
scores[lang]["fp"] += 1
for doc in gm[lang]:
if doc not in em[lang]:
scores[lang]["fn"] += 1
tp = 0
fp = 0
fn = 0
for lang in langs:
tp += scores[lang]["tp"]
fp += scores[lang]["fp"]
fn += scores[lang]["fn"]
p, r, f = compute_prf(tp, fp, fn)
tpm = defaultdict(int)
fpm = defaultdict(int)
fnm = defaultdict(int)
for lang in langs:
for doc in em[lang]:
if doc in gm[lang]:
tpm[lang] += 1
else:
fpm[lang] += 1
for lang in langs:
for doc in gm[lang]:
if doc not in em[lang]:
fnm[lang] += 1
pm = 0
rm = 0
fm = 0
for lang in langs:
ptmp, rtmp, ftmp = compute_prf(tpm[lang], fpm[lang], fnm[lang])
pm += ptmp
rm += rtmp
fm += ftmp
pm /= float(len(langs))
rm /= float(len(langs))
fm /= float(len(langs))
if rm + pm > 0:
fms = 2 * rm * pm / float(rm + pm)
else:
fms = 0
print("PM {0:.3f}, RM {1:.3f}, FM {2:.3f}, (f(PM,RM)={3:.3f}), Pm {4:.3f}, Rm {5:.3f}, Fm {6:.3f} ".format(pm, rm, fm, fms, p, r, f))
return f