-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.py
386 lines (327 loc) · 11.2 KB
/
search.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
from cmath import log10
import time
import math
import json
import sys
from unittest import result
from xxlimited import new
import preprocess
from nltk.stem import PorterStemmer
t1 = time.time()
number_of_title_files = 223
queries= sys.argv[1]
output_file = sys.argv[2]
pre = preprocess.Preprocess()
ps = PorterStemmer()
# def term_frequency_penalty():
# term_frequency_penalty = {}
# with open("document_frequency.txt", 'r') as f:
# while(True):
# line = f.readline()
# line = line.strip("\n")
# if len(line) == 0:
# break
# data = line.split("~")
# term_frequency_penalty[data[0]] = data[1]
# return term_frequency_penalty
def load_secondary_index():
secondary_index = []
with open("tmp/secondary_index.txt", 'r') as fp:
lines = fp.readlines()
for line in lines:
temp = []
line = line.strip("\n")
data = line.split("~")
temp.append(data[0])
temp.append(data[1])
secondary_index.append(temp)
return secondary_index
secondary_index = load_secondary_index()
# term_frequency_pen = term_frequency_penalty()
def binary_search(word):
low = 0
high = len(secondary_index)-1
while(low <= high):
mid = (low + high)//2
if(word >= secondary_index[mid][0] and word <= secondary_index[mid][1]):
return mid
if word > secondary_index[mid][1]:
low = mid+1;
else:
high = mid-1;
return "xx"
title_files = []
for i in range(0, number_of_title_files):
title_files.append(i)
category_index = { "b":0, "i":1, "c":2, "r":3, "t":4, "e":5}
section_weights = {0:10, 1:50, 2:30, 3:30, 4:100, 5:20}
stopwords = pre.getStopWords()
def read_file(file_name, word):
temp = {}
with open(file_name) as fp:
Lines = fp.readlines()
for line in Lines:
line = line.strip("\n")
data = line.split("~")
# try:
# temp[data[0]] = data[1]
# except IndexError:
# pass
if data[0] == word:
temp[data[0]] = data[1]
return temp
def get_posting_list(query):
posting_lists= {}
for word in query.split():
index = binary_search(word)
if(index == "xx"):
print(word, "NOT FOUND")
else:
file_name = "tmp/final_index{}.txt".format(index)
# print(file_name)
# print(file_name, word)
all_words = read_file(file_name, word)
try:
posting_lists[word] = all_words[word]
except KeyError:
print(word, "NOT FOUND")
# print(posting_lists)
del all_words
return posting_lists
def intersect(d):
res = set.intersection(*map(set,d))
return res
def page_rank(all_docs, extracted_info):
tf_idf = {}
N = 22200000
for data in extracted_info:
document_freq = data[8]
term_frequency = sum(data[1:7])
score = (term_frequency) * math.log10(N/document_freq)
if data[0] in tf_idf.keys():
tf_idf[data[0]] += score
else:
tf_idf[data[0]] = score
ranks = sorted(tf_idf.items(), key = lambda x: x[1], reverse = True)
docs = []
for key, value in ranks:
docs.append(key)
del ranks
return docs
def merge_posting_list(posting_lists):
title_posting_dict = []
all_titles_ind = []
key_list = []
for key in posting_lists.keys():
key_list.append(key)
posting_list = posting_lists[key]
all_docs = posting_list.split("|")
d = {}
title_ind = set()
for val in all_docs:
arr = val.split("-")
try:
d[int(arr[0][1:])] = val
title_ind.add(int(arr[0][1:]))
except:
pass
title_posting_dict.append(d)
all_titles_ind.append(title_ind)
l=0
for arr in all_titles_ind:
l+=len(arr)
# print("all titles: ", l)
all_titles_ind = intersect(all_titles_ind)
# print("Afetr intersection: ", len(all_titles_ind))
all_titles_ind = list(all_titles_ind)
# print(all_titles_ind)
# print(all_titles_ind)
new_posting_lists = {}
for title in all_titles_ind:
ind = 0
for key in key_list:
if key in new_posting_lists:
new_posting_lists[key] += title_posting_dict[ind][title] + "|"
else:
# print(ind, title)
# print("printing this: ", title_posting_dict[ind][title])
new_posting_lists[key] = title_posting_dict[ind][title] + "|"
ind+=1
with open("temp1.txt", 'w') as f:
for key in new_posting_lists.keys():
f.write("%s~%s\n"%(key, new_posting_lists[key]))
return new_posting_lists
def process_posting_list(posting_lists, documet_frequency, count=10, special = False):
extracted_info = []
all_docs = []
for key in posting_lists.keys():
data = posting_lists[key]
data = data.split("|")
docs = []
for word in data:
word = word.split("-")
current_data = [0]*9
for token in word:
try:
if len(token) == 0:
continue
if token[0] == 'd':
current_data[0] = int(token[1:])
if token[0] == 'b':
current_data[1] = int(token[1:])*section_weights[category_index["b"]]
if token[0] == 'i':
current_data[2] = int(token[1:])*section_weights[category_index["i"]]
if token[0] == 'c':
current_data[3] = int(token[1:])*section_weights[category_index["c"]]
if token[0] == 'r':
current_data[4] = int(token[1:])*section_weights[category_index["r"]]
if token[0] == 't':
current_data[5] = int(token[1:])*section_weights[category_index["t"]]
if token[0] == 'e':
current_data[6] = int(token[1:])*section_weights[category_index["e"]]
except ValueError:
pass
current_data[7] = key
current_data[8] = documet_frequency[key]
docs.append(current_data[0])
extracted_info.append(current_data)
all_docs.append(docs)
page_ranks = page_rank(all_docs, extracted_info)
del all_docs
k = min(len(page_ranks), count)
titles = []
for i in range(len(page_ranks)):
titles.append(page_ranks[i])
if(special):
return titles, extracted_info
return titles
def writetofile(ans):
with open(output_file, 'a') as f:
for i in ans:
f.write(i)
f.write("\n")
f.write("\n")
def get_title(title):
ind = title
title_file_ind = ind//100000
# print(ind, title_file_ind)
value = title_files[title_file_ind]
all_titles = []
# if(value == "B"):
# with open("tmp/title_list.txt") as f:
# all_titles = json.load(f)
# else:
with open("tmp/title_list{}.txt".format(value)) as f:
while True:
data = f.readline().strip("\n")
if(len(data) == 0):
break
else:
all_titles.append(data)
title_ind = ind - 100000*title_file_ind
# print(value, title_ind, len(all_titles))
# print(all_titles[title_ind],", ", all_titles[title_ind+1],", ", all_titles[title_ind+2])
# print(title_ind, title_file_ind, len(all_titles))
return all_titles[title_ind]
def process_special_query(query):
# print("in special query ")
query = query.split()
word_cat = {}
new_query = []
cat = "1"
for word in query:
if "i:" in word or "b:" in word or "c:" in word or "r:" in word or "e:" in word or "t:" in word:
cat = word[0:1]
tok = word[2:]
tok = ps.stem(tok)
if tok in stopwords:
continue
word_cat[tok] = cat
new_query.append(tok)
else:
tok = ps.stem(word)
if tok in stopwords:
continue
word_cat[tok] = cat
new_query.append(tok)
new_query = " ".join(new_query)
new_query = pre.remove_stopwords(new_query)
new_query = pre.stemmer(new_query)
# print(word_cat, new_query)
posting_lists = get_posting_list(new_query)
docuement_frequency = {}
for key in posting_lists.keys():
docuement_frequency[key] = len(posting_lists[key])
merged_posting_list = merge_posting_list(posting_lists)
# print(merged_posting_list)
titles, extracted_info = process_posting_list(merged_posting_list,docuement_frequency,count=10, special=True)
if( len(titles) < 11):
titles, extracted_info = process_posting_list(posting_lists, docuement_frequency,count=10, special=True)
del posting_lists
title_category = {}
for arr in extracted_info:
title_category[arr[0]] = arr[1:]
count = 0
# print(titles)
# print(extracted_info[:10])
# print()
ans = []
k = min(len(titles), 10)
# print("PRINTING TITLES: ", titles)
for title in titles:
if(title == 0):
continue
categories = title_category[title]
# print(categories)
word = categories[6]
word_c = word_cat[word]
ind = category_index[word_c]
# print(title)
# if categories[ind] > 0:
curr_title = get_title(title-1)
ans.append(curr_title)
count+=1
if count == k:
break
del titles, extracted_info
writetofile(ans)
with open(queries, 'r') as f:
ind = 0
while(True):
query = f.readline().strip("\n")
query = query.lower()
t3 = time.time()
ind+=1
if len(query) == 0:
break
query = query.lower()
if "i:" in query or "b:" in query or "c:" in query or "r:" in query or "e:" in query or "t:" in query:
# print(query)
output = process_special_query(query)
else:
new_query = pre.remove_stopwords(query)
new_query = pre.stemmer(new_query)
posting_lists = get_posting_list(new_query)
docuement_frequency = {}
for key in posting_lists.keys():
docuement_frequency[key] = len(posting_lists[key])
merged_list = merge_posting_list(posting_lists)
# print(merged_list)
output = process_posting_list(merged_list,docuement_frequency, 10)
del merged_list
del docuement_frequency
del posting_lists
count = 0
ans = []
k = min(10, len(output))
for title in output:
curr_title = get_title(title-1)
ans.append(curr_title)
count+=1
if count == 10:
break
writetofile(ans)
t4 = time.time()
print("query {} completed in: ".format(ind), t4-t3)
t2 = time.time()
print("TOTAL TIME TAKEN: ", t2-t1)