-
Notifications
You must be signed in to change notification settings - Fork 0
/
getrels.py
37 lines (29 loc) · 1007 Bytes
/
getrels.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
## For generating qels file
## One time Run just
def generator(infile, outfile):
with open(infile, 'r') as f:
lines = f.readlines()
dictionary = {}
for line in lines:
words = line.split()
relevance_label = int(words[0])
qid = int(words[1].split(":")[1])
i = 2
while True:
if(words[i][:1] == "#"):
docid = words[i+2]
break
else:
i+=1
if qid in dictionary:
dictionary[qid].append((docid, relevance_label))
else:
dictionary[qid] = [(docid, relevance_label)]
print("Input Parsed, Writing Qrels")
sorted_keys = sorted(dictionary.keys())
with open(outfile, "w") as wf:
for qid in sorted_keys:
for did, rlabel in dictionary[qid]:
wf.write(str(qid) + " Q0 " + str(did) + " " + str(rlabel) + " \n")
return
generator("MQ2008-agg/agg.txt", "trec_eval-9.0.7/qrels.txt")