-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindexer.py
373 lines (292 loc) · 8.64 KB
/
indexer.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
import sys
from nltk.corpus import stopwords
# from nltk.stem import SnowballStemmer
import re
from os import mkdir
from os import rename
import xml.sax
from collections import defaultdict
from nltk.corpus import stopwords
from Stemmer import Stemmer
stop_words = set(stopwords.words('english'))
stemmer = Stemmer('porter')
try:
mkdir("inddir")
except:
pass
indict=defaultdict(dict)
def indexer(title,body,references,categories,links,infobox,pagenum):
global indict
# indict[i][pagenum]=[0,0,0,0,0,0]
for i in title:
try:
indict[i][pagenum]
except:
indict[i][pagenum]=[0,0,0,0,0,0]
indict[i][pagenum][0]+=1
for i in body:
try:
indict[i][pagenum]
except:
indict[i][pagenum]=[0,0,0,0,0,0]
indict[i][pagenum][1]+=1
for i in references:
try:
indict[i][pagenum]
except:
indict[i][pagenum]=[0,0,0,0,0,0]
indict[i][pagenum][2]+=1
for i in categories:
try:
indict[i][pagenum]
except:
indict[i][pagenum]=[0,0,0,0,0,0]
indict[i][pagenum][3]+=1
for i in links:
try:
indict[i][pagenum]
except:
indict[i][pagenum]=[0,0,0,0,0,0]
indict[i][pagenum][4]+=1
for i in infobox:
try:
indict[i][pagenum]
except:
indict[i][pagenum]=[0,0,0,0,0,0]
indict[i][pagenum][5]+=1
# print(indict)
indcount = 0
totcount = 0
def tokenize(text):
global totcount
tokens = re.split(r'[^A-Za-z0-9]+', text)
final = []
for token in tokens:
totcount+=1
word = stemmer.stemWord(token)
# word = token
if len(word) <= 1 or word in stop_words or len(word)>50:
continue
final.append(word)
# print (final)
return final
def getLinks(text):
# print(text)
ans = []
raw = text.split("\n")
for lines in raw:
# print(lines)
if lines and lines[0] == '*':
# print("lololol link found")
line = tokenize(lines)
ans += line
# print (ans)
return ans
brcount =0
uncount =0
def getInfobox(text):
global uncount
global brcount
cont = text.split("{{infobox")
info = []
if len(cont) <= 1:
uncount += 1
return []
# if len(cont)>2:
# brcount+=1
flag= False
for infob in cont:
traw = infob.split("\n")
if (not flag):
flag=True
else :
for lines in traw:
# print (lines)
if lines == "}}":
break
info += tokenize(lines)
# print (info)
return info
def getReferences(text):
refarr = []
reflist = re.findall(r'\|\s*title[^\|]*',text)
for i in reflist:
refarr.append(i.replace('title','',1))
# print("++++")
# print(reflist)
return (tokenize(' '.join(refarr)))
def getCategories(text):
categoryList = re.findall(r"\[\[category:(.*)\]\]",text)
return (tokenize(' '.join(categoryList)))
def processContent(text):
text=text.lower()
references=[]
links=[]
categories=[]
data=text.split('==references==')
if data[0] == text:
data= text.split('== references ==')
if data[0]==text:
data= text.split('== references==')
if data[0]==text:
data= text.split('==references ==')
if len(data)==1:
categories = getCategories(data[0])
haslink=1
catdata=data[0].split('==external links==')
if len(catdata)==1:
catdata=data[0].split('==external links ==')
if len(catdata)==1:
catdata=data[0].split('== external links==')
if len(catdata)==1:
catdata=data[0].split('== external links ==')
if len(catdata)==1:
links=[]
haslink=0
if (haslink):
links=getLinks(catdata[1])
else:
haslink=1
catdata=data[1].split('==external links==')
if len(catdata)==1:
catdata=data[1].split('==external links ==')
if len(catdata)==1:
catdata=data[1].split('== external links==')
if len(catdata)==1:
catdata=data[1].split('== external links ==')
if len(catdata)==1:
links=[]
haslink=0
if (haslink):
links = getLinks(catdata[1])
references = getReferences(data[1])
categories = getCategories(data[1])
# links = getlinks(data[1])
infobox= getInfobox(data[0])
body = tokenize(data[0])
return body, references, categories, links, infobox
def printDisk():
global indict
global titlearr
global tempfilecount
f=open('title'+str(tempfilecount)+'.txt','w')
f.writelines(titlearr)
f.close()
titlearr=[]
# cntr = cntr - 1
fieldtype = ['t', 'b', 'r', 'c', 'l', 'i']
namer = "inddir/"+ 'index' + str(tempfilecount) + '.txt'
file = open(namer, 'w')
ind=0
for word in (sorted(indict.keys())):
mystr = word + ':'
for doc in indict[word]:
posting = indict[word][doc]
mystr += "d" + str(doc)
ct=0
for fs in (posting):
if fs > 0:
mystr += str(fieldtype[ct]) + str(fs)
ct+=1
file.write(mystr + "\n")
ind+=1
indict=defaultdict(dict)
tempfilecount+=1
print ("created file "+str(tempfilecount-1))
file.close()
# print(cntr)
# file = open("./titles/title" + str(cntr) + ".txt", 'w')
# for title in titles:
# file.write(title + '\n')
# file.close()
def mergefiles(a,b):
print ("merging files "+ str(a)+" and "+str(b))
file1 = open('inddir/index'+str(a)+'.txt','r')
file2 = open('inddir/index'+str(b)+'.txt','r')
newfile = open('inddir/'+"strb"+'.txt','w')
line1 = file1.readline()
line2 = file2.readline()
while (line1 and line2 ):
linespl1 = line1.split(':')
linespl2 = line2.split(':')
if linespl1[0] < linespl2[0]:
newfile.write(line1)
line1 = file1.readline()
elif linespl2[0] < linespl1[0] :
newfile.write(line2)
line2 = file2.readline()
elif linespl1[0] == linespl2[0] :
newfile.write(linespl1[0]+':' + linespl1[1].strip() + linespl2[1])
line1 = file1.readline()
line2 = file2.readline()
while (line1):
newfile.write(line1)
line1 = file1.readline()
while (line2):
newfile.write(line2)
line2 = file2.readline()
file1.close()
file2.close()
newfile.close()
def processTitle(title):
return tokenize(title.lower())
class PageHandler(xml.sax.ContentHandler):
global pageCount
def __init__(self):
print("called once")
self.CurrentData = ''
self.title = ''
self.text = ''
self.data = ''
def startElement(self, tag, attributes):
self.CurrentData = tag
def endElement(self, tag):
global titlearr
if tag == 'page':
global pageCount
self.title = self.title.strip()
titlearr.append(self.title+'\n')
title = processTitle(self.title)
body,references,categories,links,infobox = processContent(self.text)
self.text=''
self.title=''
indexer(title,body,references,categories,links,infobox,pageCount)
pageCount += 1
# print ("printing at: file-"+ str(tempfilecount)+ " page-"+str(pageCount))
if pageCount == tempfilecount * docthreshold:
print(pageCount)
printDisk()
# print('page ra')
def characters(self, content):
if self.CurrentData == 'title':
self.title += content
elif self.CurrentData == 'text':
self.text+=content
# elif tag == 'text':
# print (self.CurrentData)
def merge():
mergefiles(1,2)
rename('inddir/strb.txt','inddir/index0.txt')
for i in range (3,tempfilecount):
mergefiles(0,i)
rename('inddir/strb.txt','inddir/index0.txt')
import os
global pageCount
titlearr = []
tempfilecount = 1
docthreshold = 1500
tokthreshold = 15000
pageCount = 0
# f1 = sys.argv[1]
parser = xml.sax.make_parser()
parser.setFeature(xml.sax.handler.feature_namespaces, 0)
Handler = PageHandler()
parser.setContentHandler(Handler)
directory = './phase2-unzip'
for filename in os.listdir(directory):
print(filename)
parser.parse('./phase2-unzip/'+filename)
# print (brcount)
# print (uncount)
printDisk()
# merge()