-
Notifications
You must be signed in to change notification settings - Fork 32
/
trec_eval.py
54 lines (39 loc) · 1.46 KB
/
trec_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
"""Approximately simulates trec_eval using pytrec_eval."""
import argparse
import os
import sys
import pytrec_eval
def main():
parser = argparse.ArgumentParser()
parser.add_argument('qrel')
parser.add_argument('run')
args = parser.parse_args()
assert os.path.exists(args.qrel)
assert os.path.exists(args.run)
with open(args.qrel, 'r') as f_qrel:
qrel = pytrec_eval.parse_qrel(f_qrel)
with open(args.run, 'r') as f_run:
run = pytrec_eval.parse_run(f_run)
evaluator = pytrec_eval.RelevanceEvaluator(
qrel, pytrec_eval.supported_measures)
results = evaluator.evaluate(run)
def print_line(measure, scope, value):
print('{:25s}{:8s}{:.4f}'.format(measure, scope, value))
for query_id, query_measures in sorted(results.items()):
for measure, value in sorted(query_measures.items()):
print_line(measure, query_id, value)
# Scope hack: use query_measures of last item in previous loop to
# figure out all unique measure names.
#
# TODO(cvangysel): add member to RelevanceEvaluator
# with a list of measure names.
for measure in sorted(query_measures.keys()):
print_line(
measure,
'all',
pytrec_eval.compute_aggregated_measure(
measure,
[query_measures[measure]
for query_measures in results.values()]))
if __name__ == "__main__":
sys.exit(main())