-
Notifications
You must be signed in to change notification settings - Fork 0
/
QueryProcessor.py
218 lines (181 loc) · 7.65 KB
/
QueryProcessor.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
from Indexer import *
import numpy as np
import pickle
import json
import math
from string import ascii_lowercase
from nltk.stem import PorterStemmer
from operator import itemgetter
import sys
TOTAL_UNIQUE_DOC = 55393
class QueryProcessor():
""" a query processor
take the npy file and read it
take the query words and process the query using the file
"""
def __init__(self):
""" format of dictionaries:
self.doc_id: "doc_id": "url"
self.index: "word": [Posting Objects]
"""
self.fp = [open("split_index_file/split_index_%s.txt"%x, 'r') for x in ascii_lowercase] #Open all the split_index_files simulataneously
self.all_results = {}
self.urlid = []
self.query_dict = {}
self.query_score = {}
self.doc_score = {}
self.doc_length = {}
def search(self,words):
""" the search component
print out the results for this query
"""
try:
words = words.split()
if len(words) == 2:
word = PorterStemmer().stem(words[0]) + ' ' + PorterStemmer().stem(words[1])
fp_num = ord(word[0]) - 97
self.search_biword(word, fp_num)
for doc in sorted(self.all_results.items(), key = lambda kv:kv[1], reverse=True):
self.urlid.append(doc[0])
if len(words) == 3:
word = PorterStemmer().stem(words[0]) + ' ' + PorterStemmer().stem(words[1]) + ' ' + PorterStemmer().stem(words[2])
fp_num = ord(word[0]) - 97
self.search_triword(word, fp_num)
for doc in sorted(self.all_results.items(), key = lambda kv:kv[1], reverse=True):
self.urlid.append(doc[0])
if len(words) > 3 or (len(self.urlid) < 20 and len(words) == 2) or (len(self.urlid) < 20 and len(words) == 3):
self.clear_results()
self.query_tf_idf(words)
for word in words:
word = PorterStemmer().stem(word)
fp_num = ord(word[0]) - 97
self._process(word, fp_num)
self.cosine_score()
for doc in sorted(self.doc_score.items(), key = lambda kv:kv[1], reverse=True):
if doc[0] not in self.urlid:
self.urlid.append(doc[0])
elif len(words) == 1:
fp_num = ord(words[0][0]) - 97
self.rank_single_word(PorterStemmer().stem(words[0]), fp_num)
for doc in sorted(self.all_results.items(), key = lambda kv:kv[1], reverse=True):
self.urlid.append(doc[0])
return self.urlid
except:
return []
def search_biword(self, word, fp_num):
"""
Search for the website contain the biwords
"""
fb = open("split_biword_file/split_biword_index_%s.txt"%ascii_lowercase[fp_num], 'r')
while True:
word_dict = {}
line = fb.readline() #read the correct splited_index file
if word in line:
word_dict = json.loads(line)
if (list(word_dict.keys())[0]) != word:
continue
for i in (sorted(word_dict[word], key=itemgetter(1), reverse=True)[:75]):
if i[0] in self.all_results:
self.all_results[i[0]] += i[1]
else:
self.all_results[i[0]] = i[1]
if not line:
break
def search_triword(self, word, fp_num):
"""
Search for the website contain the triwords
"""
fb = open("split_triword_file/split_triword_index_%s.txt"%ascii_lowercase[fp_num], 'r')
while True:
word_dict = {}
line = fb.readline() #read the correct splited_index file
if word in line:
word_dict = json.loads(line)
if (list(word_dict.keys())[0]) != word:
continue
for i in (sorted(word_dict[word], key=itemgetter(1), reverse=True)[:75]):
if i[0] in self.all_results:
self.all_results[i[0]] += i[1]
else:
self.all_results[i[0]] = i[1]
if not line:
break
def rank_single_word(self, word, fp_num):
while True:
word_dict = {}
line = self.fp[fp_num].readline() #read the correct splited_index file
if word in line:
word_dict = json.loads(line)
if (list(word_dict.keys())[0]) != word:
continue
for i in (sorted(word_dict[word], key=itemgetter(1), reverse=True)):
if i[0] in self.all_results:
self.all_results[i[0]] += i[1]
else:
self.all_results[i[0]] = i[1]
break
if not line:
break
def cosine_score(self):
"""
Compute the cosine similarity
between query and the document
"""
for i in self.all_results:
length = 0
for j in self.all_results[i]:
length += self.all_results[i][j] ** 2
length = math.sqrt(length)
for j in self.all_results[i]:
self.all_results[i][j] = self.all_results[i][j]/length
for doc in self.all_results:
score = 0
for query_word in self.query_score:
if query_word in self.all_results[doc]:
score += self.all_results[doc][query_word] * self.query_score[query_word]
self.doc_score[doc] = score
def query_tf_idf(self, words):
"""
Compute the Normalized tf_idf Score
words for Query
"""
for word in words:
word = PorterStemmer().stem(word)
if word in self.query_dict:
self.query_dict[word] += 1
else:
self.query_dict[word] = 1
for token in self.query_dict:
df = self.query_dict[token]
idf = math.log(TOTAL_UNIQUE_DOC/df)
tf = self.query_dict[token]
tf_idf = (1 + math.log(tf))* idf
self.query_score[token] = tf_idf
length = 0
for word in self.query_score:
length += self.query_score[word] ** 2
length = math.sqrt(length)
for word in self.query_score:
self.query_score[word] = self.query_score[word]/length
def _process(self,word, fp_num):
""" get the query words as a list
process the query words, add result to all_result list
return this single result as a result object
"""
while True:
word_dict = {}
line = self.fp[fp_num].readline() #read the correct splited_index file
if word in line:
word_dict = json.loads(line)
if (list(word_dict.keys())[0]) != word:
continue
for i in (sorted(word_dict[word], key=itemgetter(1), reverse=True)[:75]):
if i[0] in self.all_results:
self.all_results[i[0]].update({word:i[1]})
else:
self.all_results[i[0]] = {word:i[1]}
break
if not line:
break
def clear_results(self):
self.all_results = {}