-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader_file.py
75 lines (70 loc) · 2.4 KB
/
reader_file.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
## For reading [collection-file]
# returns a dictionary with query ID as the key
# dict[queryID] -> list of (Document_id, relevance_label, rankings)
# where, rankings is a dictionary (RankingMechanism -> RankGiven) with RankGiven = -1
# if the RankingMechanism does not enlist this document
def reader(filename):
with open(filename, 'r') as f:
lines = f.readlines()
dictionary = {}
print("parsing numlines = ", len(lines))
for line in lines:
words = line.split()
relevance_label = int(words[0])
qid = int(words[1].split(":")[1])
i = 2
rankings_doc_query = {}
while True:
ind_rank_pair = words[i]
if(ind_rank_pair[:1] == "#"):
break
i+=1
ind, rank = ind_rank_pair.split(":")
if rank != "NULL":
rankings_doc_query[int(ind)] = int(rank)
else:
rankings_doc_query[int(ind)] = -1
while True:
if(words[i][:1] == "#"):
docid = words[i+2]
break
else:
i+=1
if qid in dictionary:
dictionary[qid].append((docid, relevance_label, rankings_doc_query))
else:
dictionary[qid] = [(docid, relevance_label, rankings_doc_query)]
return dictionary
def prob_rank_r_given_relevant(filename):
with open(filename, 'r') as f:
lines = f.readlines()
dictionary = {}
print("parsing numlines = ", len(lines))
for line in lines:
words = line.split()
relevance_label = int(words[0])
qid = int(words[1].split(":")[1])
i = 2
rankings_doc_query = {}
while True:
ind_rank_pair = words[i]
if(ind_rank_pair[:1] == "#"):
break
i+=1
ind, rank = ind_rank_pair.split(":")
if rank != "NULL":
rankings_doc_query[int(ind)] = int(rank)
else:
rankings_doc_query[int(ind)] = -1
while True:
if(words[i][:1] == "#"):
docid = words[i+2]
break
else:
i+=1
if qid in dictionary:
dictionary[qid].append((docid, relevance_label, rankings_doc_query))
else:
dictionary[qid] = [(docid, relevance_label, rankings_doc_query)]
return dictionary
#reader("MQ2008-agg/agg.txt")