-
Notifications
You must be signed in to change notification settings - Fork 10
/
evaluator.py
45 lines (39 loc) · 1.62 KB
/
evaluator.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
"""
Compares two text files line by line
(c) 2022 Paola Ortega Saborio and Fabian M. Suchanek
"""
def compare(output_file, gold_file=None):
""" Compares 2 text files line by line and prints precision and recall
Parameters: the file to be tested and (optionally) the gold file
if the gold file is not given, defaults to XXX-gold.tsv
Returns: 0 if comparison is successful,
-1 if there were problems reading the files"""
print("Evaluating "+output_file)
gold_standard = f"{output_file[0:-4]}-gold.tsv" if gold_file == None else gold_file # standard file path
with open(gold_standard, encoding='utf8') as gold:
goldContent=set(gold)
with open(output_file, encoding='utf8') as out:
outContent = set(out)
not_in_output = goldContent.difference(outContent)
not_in_gold = outContent.difference(goldContent)
print(f" Lines in the gold standard that are not in the output file: {len(not_in_output)}")
for line in not_in_output:
try:
print(f" {str(line)}", end="")
except:
# too idiotic to print
pass
print(f" Lines in output file that are not in the gold standard: {len(not_in_gold)}")
for line in not_in_gold:
try:
print(f" {str(line)}", end="")
except:
# too idiotic to print
pass
if len(not_in_output)==0 and len(not_in_gold)==0:
print("OK!")
else:
print("FAILED!")
# quick test
if __name__ == '__main__':
compare("test-data/02-make-taxonomy/02-yago-taxonomy.tsv")