-
Notifications
You must be signed in to change notification settings - Fork 0
/
retrieveDocuments.py
101 lines (84 loc) · 2.78 KB
/
retrieveDocuments.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import pickle
import os
with open("index2.pkl", "rb") as f:
auxIndex, mainIndex, snip_doc= pickle.load(f)
# print(auxIndex.keys())
with open("idf_scores.pkl", "rb") as f:
idf_score = pickle.load(f)
def convertKeyToPath(key):
document, row = key.split(":::")
document = auxIndex[int(document)]
return os.path.join(document, row)
def retrieveDocumentsWIDF(query):
# print(query)
tokens = query.split(" ")
tmpDocs = []
token_list= []
final_tokens=[]
for token in tokens:
if(idf_score.get(token)==None):
token_list.append((token,0))
else :
token_list.append((token,idf_score[token]))
token_list.sort(key= lambda x : x[1], reverse=True)
for token in token_list:
if(mainIndex.get(token[0])!=None):
tmpDocs.append(list(mainIndex[token[0]].keys()))
final_tokens.append(token)
# print(mainIndex.values())
if(len(tmpDocs)==0):
return []
resultSet = set(tmpDocs[0])
for i in range(1, len(tokens)):
try:
# resultSet = resultSet.intersection(tmpDocs[i])
# print(len(bigd))
resultSet = resultSet | set(tmpDocs[i])
except:
continue
snippet = []
# print(resultSet)
resultList = list(map(convertKeyToPath, list(resultSet)))
final_res = [resultSet,final_tokens,resultList]
# return resultSet,final_tokens,resultList
return final_res
def retrieveDocuments(query):
# print(query)
tokens = query.split(" ")
tmpDocs = []
token_list= []
final_tokens=[]
for token in tokens:
if(idf_score.get(token)==None):
token_list.append((token,0))
else :
token_list.append((token,idf_score[token]))
token_list.sort(key= lambda x : x[1], reverse=True)
for token in token_list:
if(mainIndex.get(token[0])!=None):
tmpDocs.append(list(mainIndex[token[0]].keys()))
final_tokens.append(token)
# print(mainIndex.values())
if(len(tmpDocs)==0):
return []
resultSet = set(tmpDocs[0])
for i in range(1, len(tokens)):
try:
temp = resultSet
resultSet = resultSet.intersection(tmpDocs[i])
# print(len(bigd))
# resultSet = resultSet | set(tmpDocs[i])
except:
continue
if(len(resultSet)<50):
resultSet = temp | set(tmpDocs[i-1]) | set(tmpDocs[i])
# resultSet = resultSet.intersection(tmpDocs[i])
break
snippet = []
# print(resultSet)
resultList = list(map(convertKeyToPath, list(resultSet)))
final_res = [resultSet,final_tokens,resultList]
# return resultSet,final_tokens,resultList
return final_res
# query = "industry airline"
# retrieveDocuments(query)