/
combine_ltr_res.py
64 lines (56 loc) · 1.84 KB
/
combine_ltr_res.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
import re
from total_search_eff_res import *
def main():
for paths in ALL:
totalContent = []
for path in paths[:4]:
content = readAndParseResFile(path)
filename = path.rsplit("/", 1)[1]
year = filename.split("_")[0]
for each in content:
for element in each:
element["topic"] = "{}_{}".format(year, element["topic"])
totalContent.append(content)
writeResFile(totalContent, paths[4])
def writeResFile(content, path):
f = open(path, "w+")
for each in content:
for item in each:
for element in item:
line = "{topic} 0 {uid} {rank} {score} {desc}\n".format(topic=element["topic"], uid=element["uid"], rank=element["rank"], score=element["score"], desc=element["desc"])
f.write(line)
def readAndParseResFile(path):
with open(path, "r") as f:
content = f.readlines()
content = [x.strip() for x in content]
allItems = []
for each in content:
splitedLine = re.split(r' +', each)
oneObj = {
"topic": splitedLine[0],
"uid": splitedLine[2],
"rank": splitedLine[3],
"score": splitedLine[4],
"desc": splitedLine[5]
}
allItems.append(oneObj)
topics = []
for item in allItems:
topics.append(item["topic"])
uniqueTopics = list(dict.fromkeys(topics))
uniqueTopics.sort()
groupedAll = []
for topic in uniqueTopics:
grouped = []
for t in allItems:
if t["topic"] == topic:
grouped.append(t)
else:
continue
if len(grouped) > 0:
groupedAll.append(grouped)
for g in groupedAll:
g.sort(key=lambda x: x["uid"])
return groupedAll
if __name__ == '__main__':
main()