-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.py
382 lines (339 loc) · 12.5 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
#!/usr/bin/python3
import re
import nltk
import os
import sys
import getopt
import pickle
from nltk.stem import PorterStemmer
from nltk.tokenize import word_tokenize
stemmer = PorterStemmer()
OPT = ['NOT','AND','OR']
ORDER_OPT = {'NOT':2,'AND':1,'OR':0}
def usage():
print("usage: " + sys.argv[0] + " -d dictionary-file -p postings-file -q file-of-queries -o output-file-of-results")
def run_search(dict_file, postings_file, queries_file, results_file):
"""
using the given dictionary file and postings file,
perform searching on the given queries file and output the results to a file
"""
print('running search on the queries...')
# This is an empty method
# Pls implement your code in below
f_dictionary = open(os.path.join(os.path.dirname(__file__),dict_file),'rb')
dictionary = pickle.load(f_dictionary)
name_doc_ids = 'doc_ids'
f_doc_ids = open(os.path.join(os.path.dirname(__file__),name_doc_ids),'rb')
doc_ids = pickle.load(f_doc_ids)
f_queries = open(os.path.join(os.path.dirname(__file__),queries_file),'rb')
queries = [query.decode() for query in f_queries.read().splitlines()]
f_postings = open(os.path.join(os.path.dirname(__file__),postings_file),'rb')
results = []
for query in queries:
parsed_query = parser(query)
if parsed_query==[]:
results.append(parsed_query)
continue
term_stack = []
for token in parsed_query:
if token not in OPT:
term_stack.append(token)
continue
if token =='NOT':
term1 = term_stack.pop()
if not isinstance(term1,list):
term1 = get_postings(term1,dictionary,f_postings)
term_stack.append(NOT(term1,doc_ids))
else:
term1 = term_stack.pop()
term2 = term_stack.pop()
if not isinstance(term1,list):
term1 = get_postings(term1,dictionary,f_postings)
if not isinstance(term2,list):
term2 = get_postings(term2,dictionary,f_postings)
if token == 'AND':
term_stack.append(AND(term1,term2))
elif token == 'OR':
term_stack.append(OR(term1,term2))
if len(term_stack) > 1:
print(
"ERROR - phrasal queries not supported. Please do not give multiple isolated words without operators separating them"
)
term_stack = ["Error"]
else:
term1 = term_stack.pop()
if not isinstance(term1,list):
term1 = get_postings(term1,dictionary,f_postings)
for value in term1:
if not isinstance(value,str):
term_stack.append(value)
results.append(term_stack)
f_results = open(os.path.join(os.path.dirname(__file__),results_file),'w')
for result in results:
if len(result) == 0:
f_results.write('\n')
else:
str_res = ' '.join([str(doc_id) for doc_id in result])
str_res = str_res.rstrip()
f_results.write(str_res + '\n')
f_dictionary.close()
f_results.close()
f_postings.close()
f_queries.close()
def parser(query):
if query == '':
return []
tokens = query.rstrip().split(' ')
processed_tokens = []
for token in tokens:
ed_count = 0
cur_token = token
if token[0]=='(':
processed_tokens.append('(')
cur_token = cur_token[1:]
if token[-1]==')':
ed_count+=1
cur_token = cur_token[:-1]
if ed_count>0:
processed_tokens.append(cur_token)
processed_tokens.append(')')
else:
processed_tokens.append(cur_token)
# below is the code for shunting yard algorithm
output_queue = []
opt_stack = []
while processed_tokens:
processed_token = processed_tokens.pop(0)
if (processed_token not in OPT) and processed_token != '(' and processed_token != ')':
output_queue.append(stemmer.stem(processed_token.lower()))
elif processed_token in OPT:
while (opt_stack
and opt_stack[-1] != "("
and (
ORDER_OPT[opt_stack[-1]] > ORDER_OPT[processed_token]
or (
ORDER_OPT[opt_stack[-1]]
== ORDER_OPT[processed_token]
and processed_token != "NOT"
)
)
):
output_queue.append(opt_stack.pop())
opt_stack.append(processed_token)
elif processed_token == '(':
opt_stack.append('(')
elif processed_token == ')':
try:
while(opt_stack[-1]!='('):
output_queue.append(opt_stack.pop())
if opt_stack[-1]=='(':
opt_stack.pop()
except:
print("Mismatched parenthesis detected for query '{}'".format(query))
return []
if not processed_tokens:
while opt_stack:
opt = opt_stack.pop()
if opt=='(':
print("Mismatched parenthesis detected for query '{}'".format(query))
return []
output_queue.append(opt)
return output_queue
def get_postings(term,dictionary,f_postings):
try:
ptr = dictionary[term]['pointer']
f_postings.seek(ptr,0)
postings_list = pickle.load(f_postings)
except:
postings_list = []
return postings_list
def skip_ptr_check(value):
if isinstance(value,str):
return int(value[1:])
return None
def NOT(plist1,plist2):
result = []
cur_idx_1 = cur_idx_2 = 0
skip_ptr1 = skip_ptr2 = 0
while True:
if cur_idx_2 >= len(plist2):
return result
elif cur_idx_1 >= len(plist1):
for value in plist2[cur_idx_2:]:
if skip_ptr_check(value) == None:
result.append(value)
return result
cur_value_1 = plist1[cur_idx_1]
cur_value_2 = plist2[cur_idx_2]
skip_ptr1 = skip_ptr_check(cur_value_1)
skip_ptr2 = skip_ptr_check(cur_value_2)
if skip_ptr1 != None :
cur_idx_1 += 1
cur_value_1 = plist1[cur_idx_1]
if skip_ptr2 != None:
cur_idx_2 += 1
cur_value_2 = plist2[cur_idx_2]
if cur_value_1 == cur_value_2:
cur_idx_1+=1
cur_idx_2+=1
elif cur_value_2 < cur_value_1:
result.append(cur_value_2)
if skip_ptr2 != None:
if skip_ptr2 == len(plist2)-1:
skip_value2 = plist2[skip_ptr2]
else:
skip_value2 = plist2[skip_ptr2+1]
if skip_value2 <= cur_value_1:
for doc_id in plist2[cur_idx_2+1:skip_ptr2]:
result.append(doc_id)
cur_idx_2 = skip_ptr2+1
skip_ptr2 = skip_ptr_check(plist2[skip_ptr2])
else:
cur_idx_2 += 1
else:
cur_idx_2 += 1
elif cur_value_2 > cur_value_1:
if skip_ptr1 != None:
if skip_ptr1 == len(plist1) - 1:
skip_value1 = plist1[skip_ptr1]
else:
skip_value1 = plist1[skip_ptr1+1]
if skip_value1 <= cur_value_2:
cur_idx_1 = skip_ptr1+1
skip_ptr1 = skip_ptr_check(plist1[skip_ptr1])
else:
cur_idx_1 += 1
else:
cur_idx_1 += 1
return result
def AND(plist1,plist2):
result = []
cur_idx1 = cur_idx2 = 0
skip_ptr1 = skip_ptr2 = 0
while True:
if cur_idx2 >= len(plist2) or cur_idx1>=len(plist1):
return result
cur_value1 = plist1[cur_idx1]
cur_value2 = plist2[cur_idx2]
skip_ptr1 = skip_ptr_check(cur_value1)
skip_ptr2 = skip_ptr_check(cur_value2)
if skip_ptr1 != None:
cur_idx1 += 1
cur_value1 = plist1[cur_idx1]
if skip_ptr2 != None:
cur_idx2 += 1
cur_value2 = plist2[cur_idx2]
if cur_value1 == cur_value2:
cur_idx1 += 1
cur_idx2 += 1
result.append(cur_value1)
elif cur_value2<cur_value1:
if skip_ptr2 != None:
if skip_ptr2 ==len(plist2)-1:
skip_value2 = plist2[skip_ptr2]
else:
skip_value2 = plist2[skip_ptr2+1]
if skip_value2 <= cur_value1:
cur_idx2 = skip_ptr2 + 1
skip_ptr2 = skip_ptr_check(plist2[skip_ptr2])
else:
cur_idx2 += 1
else:
cur_idx2 += 1
else:
if skip_ptr1 != None:
if skip_ptr1 == len(plist1)-1:
skip_value1 = plist1[skip_ptr1]
else:
skip_value1 = plist1[skip_ptr1+1]
if skip_value1 <= cur_value2:
cur_idx1 = skip_ptr1+1
skip_ptr1 = skip_ptr_check(plist1[skip_ptr1])
else:
cur_idx1 +=1
else:
cur_idx1+=1
return result
def OR(plist1,plist2):
result = []
cur_idx1 = cur_idx2 = 0
skip_ptr1 = skip_ptr2 = 0
while True:
if cur_idx2>=len(plist2):
for value in plist1[cur_idx1:]:
if skip_ptr_check(value)==None:
result.append(value)
return result
elif cur_idx1 >= len(plist1):
for value in plist2[cur_idx2:]:
if skip_ptr_check(value)==None:
result.append(value)
return result
cur_value1 = plist1[cur_idx1]
cur_value2 = plist2[cur_idx2]
skip_ptr1 = skip_ptr_check(cur_value1)
skip_ptr2 = skip_ptr_check(cur_value2)
if skip_ptr1 != None:
cur_idx1 += 1
cur_value1 = plist1[cur_idx1]
if skip_ptr2 != None:
cur_idx2 += 1
cur_value2 = plist2[cur_idx2]
if cur_value1==cur_value2:
result.append(cur_value1)
cur_idx1 += 1
cur_idx2 += 1
elif cur_value1 > cur_value2:
result.append(cur_value2)
if skip_ptr2 != None:
if skip_ptr2 == len(plist2)-1:
skip_value2 = plist2[skip_ptr2]
else:
skip_value2 =plist2[skip_ptr2+1]
if skip_value2 <= cur_value1:
for document in plist2[cur_idx2+1 : skip_ptr2]:
result.append(document)
cur_idx2 = skip_ptr2 + 1
skip_ptr2 = skip_ptr_check(plist2[skip_ptr2])
else:
cur_idx2 += 1
else:
cur_idx2 += 1
else:
result.append(cur_value1)
if skip_ptr1 != None:
if skip_ptr1 == len(plist1)-1:
skip_value1 = plist1[skip_ptr1]
else:
skip_value1 = plist1[skip_ptr1+1]
if skip_value1 <= cur_value2:
for document in plist1[cur_idx1+1:skip_ptr1]:
result.append(document)
cur_idx1 = skip_ptr1+1
skip_ptr1 = skip_ptr_check(plist1[skip_ptr1])
else:
cur_idx1 += 1
else:
cur_idx1 += 1
return result
dictionary_file = postings_file = file_of_queries = output_file_of_results = None
try:
opts, args = getopt.getopt(sys.argv[1:], 'd:p:q:o:')
except getopt.GetoptError:
usage()
sys.exit(2)
for o, a in opts:
if o == '-d':
dictionary_file = a
elif o == '-p':
postings_file = a
elif o == '-q':
file_of_queries = a
elif o == '-o':
file_of_output = a
else:
assert False, "unhandled option"
if dictionary_file == None or postings_file == None or file_of_queries == None or file_of_output == None :
usage()
sys.exit(2)
run_search(dictionary_file, postings_file, file_of_queries, file_of_output)