-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.py
354 lines (300 loc) · 9.1 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
import os, sys
import timeit
from bisect import bisect
from math import log10
from nltk.stem.snowball import SnowballStemmer
import re
from operator import itemgetter
stopWords = set()
docTitleMap = {}
dict_appearance1 = {}
Total_docs = 0
secondaryIndex = []
indexFolder = ""
stemmer = SnowballStemmer("english")
regEx = re.compile(r'(\d+|\s+)')
outFile = open("queries_op.txt", 'w')
def myNum(word):
if (len(word) > 4):
return False
if '0' in word or '1' in word or '2' in word or '3' in word or '4' in word or '5' in word or '6' in word or '7' in word or '8' in word or '9' in word:
return True
return False
def wf(exp, weightedFrequency):
lis = re.compile(r'(\d+|\s+)').split(exp)
tagType, Frequency = lis[0], lis[1]
if tagType == "i":
freq = int(Frequency) * 50
weightedFrequency += freq
elif tagType == "t":
freq = int(Frequency) * 1000
weightedFrequency += freq
elif tagType == "c":
freq = int(Frequency) * 50
weightedFrequency += freq
elif tagType == "r":
freq = int(Frequency) * 50
weightedFrequency += freq
elif tagType == "e":
freq = int(Frequency) * 50
weightedFrequency += freq
elif tagType == "b":
freq = int(Frequency) * 5
weightedFrequency += freq
return weightedFrequency
def writeToFile(global_dict, K):
LandF_dict = {}
for k in global_dict:
try:
weightedFrequency = 0
n = len(global_dict[k])
for x in global_dict[k]:
x, idf = x.split("_")
x = x.split(",")
try:
for y in x:
try:
weightedFrequency = wf(y, weightedFrequency)
except:
continue
except:
continue
except:
continue
if n in LandF_dict:
LandF_dict[n][k] = float(log10(1 + weightedFrequency)) * float(idf)
else:
LandF_dict[n] = {k: float(log10(1 + weightedFrequency)) * float(idf)}
count = K
for k, v in sorted(LandF_dict.items(), reverse=True):
for k1, v1 in sorted(v.items(), key=itemgetter(1), reverse=True):
count -= 1
outFile.write(str(k1) + ", " + str(docTitleMap[k1]))
if count == 0:
break
if count == 0:
break
def Normal_query_words(queryWords, K):
wordsToSearch = []
for word in queryWords:
try:
word = word.lower().strip()
if word not in stopWords:
word = stemmer.stem(word)
if ((word.isalpha() and len(word) > 3 and len(word) < 16 and word not in stopWords) or (myNum(word))):
wordsToSearch.append(word)
except:
continue
global_dict = {}
for word in wordsToSearch:
position = bisect(secondaryIndex, word)
if position < 0:
continue
primaryFile = indexFolder + "index" + str(position) + ".txt"
file = open(primaryFile, "r")
data = file.read()
pos_start = data.find(word + ":")
if pos_start == -1:
continue
pos_end = data.find("\n", pos_start + 1)
required_text = data[pos_start:pos_end]
word, entry = required_text.split(":")
posting_list = entry.split("|")
No_Doc_word_found = len(posting_list)
idf = log10(Total_docs / No_Doc_word_found)
for i in posting_list:
docID, entry = i.split("-")
if docID in global_dict:
global_dict[docID].append(entry + "_" + str(idf))
else:
global_dict[docID] = [entry + "_" + str(idf)]
writeToFile(global_dict, K)
def stemming_words(query):
querywords = []
words_to_process = query.split()
field_dict = {}
for word in words_to_process:
word = word.lower().strip()
if word not in stopWords:
word = stemmer.stem(word)
if ((word.isalpha() and len(word) > 3 and len(word) < 16 and word not in stopWords) or (myNum(word))):
if word not in field_dict:
field_dict[word] = 1
querywords.append(word)
return querywords
def field_entry(field_dict, c, index_list, i):
required_text = query[index_list[i] + 2:index_list[i + 1]]
Extracted_query = stemming_words(required_text)
field_dict[c] = Extracted_query
def get_field_dict(query):
Extracted_query = []
field_dict = {}
index_list = []
indextotype = {}
index_category = query.find('c:')
index_ref = query.find('r:')
index_ext = query.find('e:')
index_title = query.find('t:')
index_body = query.find('b:')
index_infobox = query.find('i:')
indextotype[index_category] = 'c'
indextotype[index_ref] = 'r'
indextotype[index_ext] = 'e'
indextotype[index_title] = 't'
indextotype[index_body] = 'b'
indextotype[index_infobox] = 'i'
if index_ref != -1:
index_list.append(index_ref)
if index_infobox != -1:
index_list.append(index_infobox)
if index_ext != -1:
index_list.append(index_ext)
if index_title != -1:
index_list.append(index_title)
if index_body != -1:
index_list.append(index_body)
if index_category != -1:
index_list.append(index_category)
index_list.append(len(query))
index_list.sort()
for i in range(0, len(index_list) - 1):
if indextotype[index_list[i]] == 'c':
field_entry(field_dict, 'c', index_list, i)
if indextotype[index_list[i]] == 'r':
field_entry(field_dict, 'r', index_list, i)
if indextotype[index_list[i]] == 'e':
field_entry(field_dict, 'e', index_list, i)
if indextotype[index_list[i]] == 't':
field_entry(field_dict, 't', index_list, i)
if indextotype[index_list[i]] == 'b':
field_entry(field_dict, 'b', index_list, i)
if indextotype[index_list[i]] == 'i':
field_entry(field_dict, 'i', index_list, i)
return field_dict
def wf2(exp, weightedFrequency):
lis = re.compile(r'(\d+|\s+)').split(exp)
tagType, Frequency = lis[0], lis[1]
if tagType == "i" and tagType == key:
freq = int(Frequency) * 50
weightedFrequency += freq
elif tagType == "t" and tagType == key:
freq = int(Frequency) * 1000
weightedFrequency += freq
elif tagType == "c" and tagType == key:
freq = int(Frequency) * 50
weightedFrequency += freq
elif tagType == "r" and tagType == key:
freq = int(Frequency) * 50
weightedFrequency += freq
elif tagType == "e" and tagType == key:
freq = int(Frequency) * 50
weightedFrequency += freq
elif tagType == "b" and tagType == key:
freq = int(Frequency) * 5
weightedFrequency += freq
return weightedFrequency
def writeToFile2(global_dict, K):
LandF_dict = {}
for k in global_dict:
try:
weightedFrequency = 0
n = len(global_dict[k])
for x in global_dict[k]:
try:
x, idf, key = x.split("_")
x = x.split(",")
for y in x:
try:
weightedFrequency += wf2(y, weightedFrequency, key)
except:
continue
except:
continue
except:
continue
if n in LandF_dict:
LandF_dict[n][k] = float(log10(1 + weightedFrequency)) * float(idf)
else:
LandF_dict[n] = {k: float(log10(1 + weightedFrequency)) * float(idf)}
count = K
for k, v in sorted(LandF_dict.items(), reverse=True):
for k1, v1 in sorted(v.items(), key=itemgetter(1), reverse=True):
count -= 1
outFile.write(str(k1) + ", " + str(docTitleMap[k1]))
if count == 0:
break
if count == 0:
break
def Field_query_words(query, K):
query = query.lower()
docs = {}
global_dict = {}
fieldDict = get_field_dict(query)
for key in fieldDict.keys():
querywords = fieldDict[key]
for word in querywords:
position = bisect(secondaryIndex, word)
if position < 0:
continue
primaryFile = indexFolder + "index" + str(position) + ".txt"
try:
file = open(primaryFile, "r")
data = file.read()
except:
print("Primary Index file is not present:" + str(file))
pos_start = data.find(word + ":")
if pos_start == -1:
continue
pos_end = data.find("\n", pos_start + 1)
required_text = data[pos_start:pos_end]
word, entry = required_text.split(":")
posting_list = entry.split("|")
No_Doc_word_found = len(posting_list)
idf = log10(Total_docs / No_Doc_word_found)
for i in posting_list:
docID, entry = i.split("-")
if (key not in entry):
continue
if docID in global_dict:
global_dict[docID].append(entry + "_" + str(idf) + "_" + str(key))
else:
global_dict[docID] = [entry + "_" + str(idf) + "_" + str(key)]
writeToFile2(global_dict, K)
path_to_index = "INDEX"
indexFolder = str(path_to_index)
if (indexFolder[-1:] != '/'):
indexFolder += "/"
try:
f = open("stopwords.txt", "r")
for line in f:
stopWords.add(line.strip())
f = open(indexFolder + "secondaryIndex.txt", "r")
for line in f:
secondaryIndex.append(line.split()[0])
f = open(indexFolder + "docTitleMap.txt", "r")
for line in f:
docID, titleMap = line.split("#")
docTitleMap[docID] = titleMap
Total_docs += 1
except:
print(indexFolder + "Prerequisite files not found")
sys.exit(1)
queryFile = open(str(sys.argv[1]), 'r')
for line in queryFile.readlines():
line = line.split(',')
K = int(line[0])
query = line[1]
start = timeit.default_timer()
if ":" not in query:
queryWords = query.split()
# print(queryWords)
Normal_query_words(queryWords, K)
else:
Field_query_words(query, K)
stop = timeit.default_timer()
dn = len(query.split(':'))
if ":" in query:
dn -= 1
outFile.write(str(stop - start) + ", " + str((stop - start) / dn) + "\n\n")
outFile.close()
queryFile.close()