-
Notifications
You must be signed in to change notification settings - Fork 2
/
score.py
111 lines (98 loc) · 4.22 KB
/
score.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
#!/usr/bin/env python3
"""
Score the predictions with gold labels, using precision, recall and F1 metrics.
"""
import argparse
import sys
from collections import Counter
NO_RELATION = "no_relation"
def parse_arguments():
parser = argparse.ArgumentParser(description='Score a prediction file using the gold labels.')
parser.add_argument('gold_file', help='The gold relation file; one relation per line')
parser.add_argument('pred_file', help='A prediction file; one relation per line, in the same order as the gold file.')
args = parser.parse_args()
return args
def score(key, prediction, verbose=False):
correct_by_relation = Counter()
guessed_by_relation = Counter()
gold_by_relation = Counter()
# Loop over the data to compute a score
for row in range(len(key)):
gold = key[row]
guess = prediction[row]
if gold == NO_RELATION and guess == NO_RELATION:
pass
elif gold == NO_RELATION and guess != NO_RELATION:
guessed_by_relation[guess] += 1
elif gold != NO_RELATION and guess == NO_RELATION:
gold_by_relation[gold] += 1
elif gold != NO_RELATION and guess != NO_RELATION:
guessed_by_relation[guess] += 1
gold_by_relation[gold] += 1
if gold == guess:
correct_by_relation[guess] += 1
# Print verbose information
if verbose:
print("Per-relation statistics:")
relations = gold_by_relation.keys()
longest_relation = 0
for relation in sorted(relations):
longest_relation = max(len(relation), longest_relation)
for relation in sorted(relations):
# (compute the score)
correct = correct_by_relation[relation]
guessed = guessed_by_relation[relation]
gold = gold_by_relation[relation]
prec = 1.0
if guessed > 0:
prec = float(correct) / float(guessed)
recall = 0.0
if gold > 0:
recall = float(correct) / float(gold)
f1 = 0.0
if prec + recall > 0:
f1 = 2.0 * prec * recall / (prec + recall)
# (print the score)
sys.stdout.write(("{:<" + str(longest_relation) + "}").format(relation))
sys.stdout.write(" P: ")
if prec < 0.1: sys.stdout.write(' ')
if prec < 1.0: sys.stdout.write(' ')
sys.stdout.write("{:.2%}".format(prec))
sys.stdout.write(" R: ")
if recall < 0.1: sys.stdout.write(' ')
if recall < 1.0: sys.stdout.write(' ')
sys.stdout.write("{:.2%}".format(recall))
sys.stdout.write(" F1: ")
if f1 < 0.1: sys.stdout.write(' ')
if f1 < 1.0: sys.stdout.write(' ')
sys.stdout.write("{:.2%}".format(f1))
sys.stdout.write(" #: %d" % gold)
sys.stdout.write("\n")
print("")
# Print the aggregate score
if verbose:
print("Final Score:")
prec_micro = 1.0
if sum(guessed_by_relation.values()) > 0:
prec_micro = float(sum(correct_by_relation.values())) / float(sum(guessed_by_relation.values()))
recall_micro = 0.0
if sum(gold_by_relation.values()) > 0:
recall_micro = float(sum(correct_by_relation.values())) / float(sum(gold_by_relation.values()))
f1_micro = 0.0
if prec_micro + recall_micro > 0.0:
f1_micro = 2.0 * prec_micro * recall_micro / (prec_micro + recall_micro)
print("Precision (micro): {:.3%}".format(prec_micro))
print(" Recall (micro): {:.3%}".format(recall_micro))
print(" F1 (micro): {:.3%}".format(f1_micro))
return prec_micro, recall_micro, f1_micro
if __name__ == "__main__":
# Parse the arguments from stdin
args = parse_arguments()
key = [str(line).rstrip('\n') for line in open(str(args.gold_file))]
prediction = [str(line).rstrip('\n') for line in open(str(args.pred_file))]
# Check that the lengths match
if len(prediction) != len(key):
print("Gold and prediction file must have same number of elements: {} in gold vs {} in prediction".format(len(key), len(prediction)))
exit(1)
# Score the predictions
score(key, prediction, verbose=True)