-
Notifications
You must be signed in to change notification settings - Fork 1
/
wiki_search.py
348 lines (283 loc) · 12.4 KB
/
wiki_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
import sys
import time
import os
import xml.sax
import re
from collections import defaultdict
import math
import bisect
import nltk
from nltk.stem import *
from nltk.stem.snowball import SnowballStemmer
from nltk.tokenize import RegexpTokenizer
import pickle
from nltk.stem.porter import *
from nltk.stem import PorterStemmer as porter
from Stemmer import Stemmer
import operator
from math import log
stopwords=["a", "about", "above", "above", "across", "after", "afterwards", "again",
"against", "all", "almost", "alone", "along", "already", "also","although",
"always","am","among", "amongst", "amoungst", "amount", "an", "and", "another",
"any","anyhow","anyone","anything","anyway", "anywhere", "are", "around", "as",
"at", "back","be","became", "because","become","becomes", "becoming", "been",
"before", "beforehand", "behind", "being", "below", "beside", "besides",
"between", "beyond", "bill", "both", "bottom","but", "by", "call", "can",
"cannot", "cant", "co", "con", "could", "couldnt", "cry", "de", "describe",
"detail", "do", "done", "down", "due", "during", "each", "eg", "eight", "either",
"eleven","else", "elsewhere", "empty", "enough", "etc", "even", "ever", "every",
"everyone", "everything", "everywhere", "except", "few", "fifteen", "fify", "fill",
"find", "fire", "first", "five", "for", "former", "formerly", "forty", "found",
"four", "from", "front", "full", "further", "get", "give", "go", "had", "has",
"hasnt", "have", "he", "hence", "her", "here", "hereafter", "hereby", "herein",
"hereupon", "hers", "herself", "him", "himself", "his", "how", "however", "hundred",
"ie", "if", "in", "inc", "indeed", "interest", "into", "is", "it", "its", "itself",
"keep", "last", "latter", "latterly", "least", "less", "ltd", "made", "many", "may",
"me", "meanwhile", "might", "mill", "mine", "more", "moreover", "most", "mostly",
"move", "much", "must", "my", "myself", "name", "namely", "neither", "never",
"nevertheless", "next", "nine", "no", "nobody", "none", "noone", "nor", "not",
"nothing", "now", "nowhere", "of", "off", "often", "on", "once", "one", "only",
"onto", "or", "other", "others", "otherwise", "our", "ours", "ourselves", "out",
"over", "own","part", "per", "perhaps", "please", "put", "rather", "re", "same",
"see", "seem", "seemed", "seeming", "seems", "serious", "several", "she", "should",
"show", "side", "since", "sincere", "six", "sixty", "so", "some", "somehow", "someone",
"something", "sometime", "sometimes", "somewhere", "still", "such", "system", "take",
"ten", "than", "that", "the", "their", "them", "themselves", "then", "thence", "there",
"thereafter", "thereby", "therefore", "therein", "thereupon", "these", "they", "thickv",
"thin", "third", "this", "those", "though", "three", "through", "throughout", "thru",
"thus", "to", "together", "too", "top", "toward", "towards", "twelve", "twenty", "two",
"un", "under", "until", "up", "upon", "us", "very", "via", "was", "we", "well", "were",
"what", "whatever", "when", "whence", "whenever", "where", "whereafter", "whereas",
"whereby", "wherein", "whereupon", "wherever", "whether", "which", "while", "whither",
"who", "whoever", "whole", "whom", "whose", "why", "will", "with", "within", "without",
"would", "yet", "you", "your", "yours", "yourself", "yourselves"
]
dbfile = open('titles/titleMap', 'rb')
db = pickle.load(dbfile)
dbfile.close()
stemmer = Stemmer('porter')
def tokenizeWords(data):
# data = data.lower()
tokenizer = RegexpTokenizer(r'[a-zA-Z0-9]+')
return tokenizer.tokenize(data)
def cleanData(data):
# Tokenisation -> lower -> stopWords -> stemming
data = data.lower()
data = tokenizeWords(data)
words = []
for token in data:
token = (token)
if len(token) <= 1 or token in stopwords:
continue
words.append(stemmer.stemWord(token))
return words
class SearchEngine:
def __init__(self):
self.nonfieldQuery = 0
self.tokensToSearch = []
self.wordDocumentId = {}
self.freq_of_word_in_Doc_id = {}
self.SecondaryIndexlist = []
self.getSeconaryIndexList()
self.totalDocuments = len(db)
self.wordIdDict = {}
self.topK = None
self.interSectionList_of_doc = []
self.tfIDF = {}
self.query_types = None
self.query_values = None
def createTokens(self,searchQuery):
if ":" in searchQuery:
self.nonfieldQuery = 0
else:
self.nonfieldQuery = 1
if self.nonfieldQuery == 1:
self.tokensToSearch += cleanData(searchQuery)
else:
self.query_types,self.query_values = self.splitFieldQuery(searchQuery)
def splitFieldQuery(self,search_query):
query_tokens = search_query.split(":")
query_types = []
query_values = []
i=0
for token in query_tokens:
if i == 0:
query_types.append(token[0:1])
i += 1
continue
if i == len(query_tokens)-1:
query_values.append(token)
i += 1
continue
query_types.append(token[token.rfind(" ")+1:len(token)][0:1])
query_values.append(token[0:token.rfind(" ")])
i+=1
return query_types,query_values
def getSeconaryIndexList(self):
secondaryIndexPath = "index/" + "secondaryIndex.txt"
secIdx = open(secondaryIndexPath,'r')
for word in secIdx:
self.SecondaryIndexlist.append(word[:-1])
def getPostingList(self,word):
file_index = bisect.bisect(self.SecondaryIndexlist, word)
if file_index > 0 :
file_index -= 1
filePath = "index/index_" + str(file_index) + ".txt"
file = open(filePath,'r')
for line in file:
if line[:line.find(":")] == word:
return line[line.find(":")+1:-1]
return
def getFieldSpecific(self,postingList,field):
docFreq = None
if field == 'i':
docFreq = re.findall(r'i[0-9]+',postingList)
elif field == 'r':
docFreq = re.findall(r'r[0-9]+',postingList)
elif field == 'b':
docFreq = re.findall(r'b[0-9]+',postingList)
elif field == 'c':
docFreq = re.findall(r'c[0-9]+',postingList)
elif field == 't':
docFreq = re.findall(r't[0-9]+',postingList)
return docFreq
def splitPostingList(self,postingList,field):
docIdList = []
docidFreq = {}
postingList = postingList.split(' ')
for i in postingList:
docId = re.findall(r'd[0-9]+',i)
if(docId):
docId = docId[0][1:]
docFreq = self.getFieldSpecific(i,field) #function for [i b g r t]
if(docFreq):
docFreq = docFreq[0][1:]
docIdList.append(docId)
docidFreq[docId] = docFreq
return docIdList , docidFreq
def getIntersection(self,wordDocumentId):
result = None
for key in wordDocumentId.keys():
result=wordDocumentId[key]
break
for eachterm in wordDocumentId.keys():
result = list(set(result).intersection(wordDocumentId[eachterm]))
return result
def getUnion(self,wordDocumentId):
result = None
for key in wordDocumentId.keys():
result=wordDocumentId[key]
break
for eachterm in wordDocumentId.keys():
result = list(set(result).union(wordDocumentId[eachterm]))
return result
def tfidfPreMethod(self,field):
for word in self.tokensToSearch:
postingList = self.getPostingList(word)
if(postingList):
self.wordDocumentId[word] , self.freq_of_word_in_Doc_id[word] = self.splitPostingList(postingList,field)
if (len(self.wordDocumentId[word]) > 0):
self.wordIdDict[word] = log(self.totalDocuments/len(self.wordDocumentId[word]))
self.interSectionList_of_doc = self.getIntersection(self.wordDocumentId)
if(len(self.interSectionList_of_doc)< self.topK):
self.interSectionList_of_doc = self.getUnion(self.wordDocumentId)
def calculateTfidfScoreforEachWord(self):
if(len(self.interSectionList_of_doc)==0):
return
for docId in self.interSectionList_of_doc:
resultantScore = 0
for word in self.freq_of_word_in_Doc_id:
value = 0
if docId in self.freq_of_word_in_Doc_id[word].keys():
frequency = self.freq_of_word_in_Doc_id[word][docId]
if int(frequency) > 0 :
frequency = 1 + log(int(frequency))
else:
frequency = 0
value = frequency * self.wordIdDict[word]
resultantScore += value
self.tfIDF[docId] = resultantScore
def sortByScores(self,docScoreDict):
temp = dict(sorted(docScoreDict.items(), key=operator.itemgetter(1),reverse=True))
return list(temp.keys())
def TopKdoc(self):
sortedList = self.sortByScores(self.tfIDF)
if len(sortedList) > self.topK:
return sortedList[:self.topK]
else:
return sortedList
def getTitles(self,docList):
titles = []
for docID in docList:
titles.append(db[int(docID)])
return titles
def writeInFile(result,fp):
for i in result:
titlename = db[int(i)]
docId = str(i)
towrite = docId + "," +titlename + '\n'
fp.write(towrite)
def getquery(line):
line= line.split(',')
k = line[0].strip()
query = line[1].strip()
return k , query
rf = open(sys.argv[1],'r')
fp = open("2019201058_queries_op.txt",'w')
while True:
line = rf.readline()
if not line:
break
k , input_query_non_field = getquery(line)
startTime = time.time()
if ":" in input_query_non_field:
se = SearchEngine()
se.topK = int(k)
query_types,query_values = se.splitFieldQuery(input_query_non_field)
listForDifferentQuries = []
for i in range(len(query_types)):
object1 = SearchEngine()
object1.topK = int(k)
query = query_values[i]
object1.createTokens(query)
object1.tfidfPreMethod(query_types[i])
object1.calculateTfidfScoreforEachWord()
result = object1.TopKdoc()
resultList = result
if (len(resultList)>0):
listForDifferentQuries.append(resultList)
result = None
for listq in listForDifferentQuries:
result = listq
break
for listq in listForDifferentQuries:
result = list(set(result).intersection(listq))
if(len(result) < object1.topK):
result = None
for listq in listForDifferentQuries:
result = listq
break
for listq in listForDifferentQuries:
result = list(set(result).union(listq))
if(len(result)>object1.topK):
result = result[:object1.topK]
writeInFile(result,fp)
finalTime = time.time() - startTime
averageTime = finalTime/int(k)
timeToWrite = str(finalTime) +" " + str(averageTime) + '\n'
fp.write(timeToWrite)
else:
se = SearchEngine()
se.topK = int(k)
se.createTokens(input_query_non_field)
se.tfidfPreMethod('b')
se.calculateTfidfScoreforEachWord()
result = se.TopKdoc()
writeInFile(result,fp)
finalTime = time.time() - startTime
averageTime = finalTime/int(k)
timeToWrite = str(finalTime) +" " + str(averageTime) + '\n'
fp.write(timeToWrite)
fp.write('\n')
fp.close()
rf.close()