forked from ahmad-Wattar/Websuche-und-Information-Retrieval
-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluation.py
72 lines (55 loc) · 1.77 KB
/
evaluation.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
#!/usr/bin/env python
# coding: utf-8
# In[15]:
import pandas as pd
import pytrec_eval
import json
import sys
import re
def evaluate_run(run_path, qrel_path):
run = pd.read_csv(run_path, sep = ' ', names = ['topic','Q0','id','rank','score','team'])
#run['topic_mustbe'] = 0
#i = 1
#t = 0
#for idx, row in run.iterrows():
#if row['rank']>1:
#run.at[idx,'topic_mustbe']=t
#else:
#t+=1
#run.at[idx,'topic_mustbe']=t
#'touche2020-task2-relevance-withbaseline(1).qrels'
qrel = pd.read_csv(qrel_path, sep = ' ', names = ['topic','Q0','id','relevance'])
qrels = {}
for i in range(1,51):
qrels[str(i)] = {}
for idx, row in qrel.iterrows():
qrels[str(row['topic'])][row['id']] = row['relevance']
runs = {}
for i in range(1,51):
runs[str(i)] = {}
for idx, row in run.iterrows():
runs[str(row['topic'])][row['id']] = row['score']
evaluator = pytrec_eval.RelevanceEvaluator(
qrels,{'map_cut', 'ndcg_cut', 'recall', 'P'})
#{'map_cut', 'ndcg_cut', 'recall'}
res = evaluator.evaluate(runs)
results = {}
recall = {}
pres = {}
for key in res:
results[key] = res[key]['ndcg_cut_10']
recall[key] = res[key]['recall_10']
pres[key] = res[key]['P_10']
print(res[key])
filename = re.split('\.', sys.argv[1])
with open((filename[0]+'_evaluated.json'), 'w') as f:
json.dump(results, f)
s = sum(results.values())
r = sum(recall.values())/50
p = sum(pres.values())/50
f1 = (2*(r*p)/(r+p))
print("Average ndcg_cut_10: ", s/50)
print("Average recall_10: ", r)
print("Av. precision: ", p)
print("F1-score: ",f1)
evaluate_run(sys.argv[1], sys.argv[2])