-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrawler.py
344 lines (274 loc) · 9.84 KB
/
crawler.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
import os
import sys
import re
import mmap
from collections import Counter
import contextlib
from math import log
from porter import PorterStemmer
import helperFunctions
import constants
termSize = 15
dFileData = {} # {docID:dFileContents}
T ="T" #"title"
W = "W" #"contents"
B = "B" #"date"
A = "A" #"author"
N = "N" # "entryDate"
I = "I" # "docID"
X = "X" #ref
termData = {} # term:L[ctf:df:D[docid:tf]]
termsList = []
stopwords_l = []
termMapList = []
def makeTermsList():
global termsList,termData,termMapList
offset = 0
for term,data in termData.items():
Sstr=term+" "+str(data[0])+" "+str(data[1])+getStr(data[2])
termsList.append(Sstr)
lenSstr = len(Sstr)
termMapList.append(term+" "+str(offset)+" "+str(offset+lenSstr)+"\n")
offset = offset+lenSstr
def mergeTermFiles():
for root, subFolders, files in os.walk("./indexes/tmp"):
files.sort()
linesToAdd = 0
linesinFile = len(files)
fullSize = pow(2,int(log(linesinFile,2))+1)-1
if (fullSize == linesinFile ):
linesToAdd = 0
elif (fullSize < linesinFile):
fullSize2 = pow(2,int(log(linesinFile,2))+1)-1
linesToAdd = fullSize2 - linesinFile
else:
linesToAdd = fullSize - linesinFile
print(" Line to add : "+str(linesToAdd)+" mapSize"+str(linesinFile))
while linesToAdd > 0:
Str = "_"+str(linesToAdd) #+" "+makeFixedLengthStr(0,6)+" "+makeFixedLengthStr(0,6)+"\n"
# print("Adding Line")
linesToAdd = linesToAdd-1
files.append(Str)
files.sort()
termFile = "./indexes/termsx.txt"
termMapFile = "./indexes/termsMapy.txt"
termsListFile = "./indexes/termsList.txt"
with open(termFile, "wb") as f:
f.write(b"*")
with open(termFile, "r+b") as f:
# memory-map the file, size 0 means whole file
map = mmap.mmap(f.fileno(), 0)
fr = open(termsListFile,"w")
#with open(termMapFile, "wb") as f2:
# f2.write(b"*")
with open(termMapFile, "wb") as f2:
# memory-map the file, size 0 means whole file
# map2 = mmap.mmap(f2.fileno(), 0)
byteLen = 0
for filex in files:
if (filex[0:1] != "_"):
fr.write(filex+" ")
fx = open("./indexes/tmp/"+filex, "r+b")
# memory-map the file, size 0 means whole file
map1 = mmap.mmap(fx.fileno(), 0)
map1.seek(0)
map.resize(map.size()+map1.size())
map.write(map1[0:])
#map.flush()
Str = makeFixedLengthSpace(filex,20)+" "+makeFixedLengthStr(byteLen,6)+" "+makeFixedLengthStr(byteLen+map1.size(),6)+"\n"
f2.write(Str.encode("utf-8"))
byteLen = byteLen+map1.size()
else:
Str = makeFixedLengthSpace(filex,20)+" "+makeFixedLengthStr(0,6)+" "+makeFixedLengthStr(0,6)+"\n"
f2.write(Str.encode("utf-8"))
fr.close()
#map.close()
#map1.close()
#map2.flush()
#map2.close()
def fillTerms(docID,lTerms):
dWordsCnt = Counter(lTerms)
for term,cnt in dWordsCnt.items():
term = "./indexes/tmp/"+term
lTermProp = []
tf = cnt
sdx = " "+str(docID)+" "+str(tf)
if os.path.isfile(term):
with open(term, "a") as f:
# memory-map the file, size 0 means whole file
#map = mmap.mmap(f.fileno(), 0)
#ctfx = int(map[0:6])
#dfx = int(map[7:13])
#sctfx=makeFixedLengthStr(ctfx+ctf,6)
#sdfx =makeFixedLengthStr(dfx+df,6)
#sx = sctfx+" "+sdfx+" "
#map.seek(0)
#map.write(sx.encode("utf-8"))
#map.flush()
#fileLen = map.size()
#map.seek(fileLen-1)
#map.resize(fileLen+len(sdx))
#map.write(sdx.encode("utf-8"))
f.write(sdx)
f.close()
#map.flush()
#map.close
else:
with open(term, "w") as f:
f.write( term+" "+sdx)
f.close()
#with open(term, "r+b") as f:
# memory-map the file, size 0 means whole file
# map = mmap.mmap(f.fileno(), 0)
#sctfx=makeFixedLengthStr(ctf,6)
#sdfx =makeFixedLengthStr(df,6)
# sx = str(docID)+" "+str(tf)
# map.seek(0)
# map.resize(map.size()+len(sx))
# sx=sx.encode("utf-8")
# map.write(sx)
# map.close
def getDocStuff(dDocProps):
global T,W,B,A,N,I
lAllLists = []
if (T in dDocProps):
lAllLists.append(dDocProps[T])
if (W in dDocProps):
lAllLists.append(dDocProps[W])
#if (B in dDocProps):
# lAllLists.append(dDocProps[B])
if (A in dDocProps):
lAllLists.append(dDocProps[A])
#if (N in dDocProps):
# lAllLists.append(dDocProps[N])
lAllLines = []
for lList in lAllLists:
lAllLines.extend(lList)
lAllWords = []
for sLine in lAllLines:
lWords = sLine.split()
lAllWords.extend(lWords)
lAllWords = helperFunctions.remStopWords(lAllWords)
p = PorterStemmer()
lAllWordsStemmed = []
for word in lAllWords:
word = p.stem(word,0,len(word)-1)
lAllWordsStemmed.append(word)
#print("All words :", lAllWordsStemmed,"\n")
lUniqueWords = list(set(lAllWordsStemmed))
lenAllWords = len(lAllWordsStemmed)
lenAllWords
sRet = makeFixedLengthStr(len(lAllWordsStemmed),6)+" "+makeFixedLengthStr(len(lUniqueWords),6) #+":"+dDocProps[B][0]
return [sRet,lAllWordsStemmed]
def makeFixedLengthStr(length,n):
sLen = str(length)
zeros=""
#print(n-len(sLen))
while (n-len(sLen)) > 0:
zeros=zeros+"0"
#print(zeros)
n=n-1
zeros = zeros+sLen
#print(zeros)
return zeros
def makeFixedLengthSpace(length,n):
sLen = str(length)
zeros=""
#print(n-len(sLen))
while (n-len(sLen)) > 0:
zeros=zeros+" "
#print(zeros)
n=n-1
zeros = sLen+zeros
#print(zeros)
return zeros
def makeIndexes():
global T,W,B,A,N,I,dFileData,termData,termsList,termMapList
docFile = "./indexes/d3.txt"
docWordsFile = "./indexes/docWords.txt"
termsFile = "./indexes/terms.txt"
termsMapFile = "./indexes/termsMap.txt"
docList = []
lDocWords = []
lTerms = []
with open(docFile, "wb") as f:
f.write(b"Hello Python!\n")
with open(docFile, "r+b") as f:
# memory-map the file, size 0 means whole file
map = mmap.mmap(f.fileno(), 0)
map.resize(5000*14)
for docID,dProps in dFileData.items():
docList.append(makeFixedLengthStr(int(docID),6)+" "+getDocStuff(dProps)[0]+"\n")
map.seek(int(docID)*14)
str1 = getDocStuff(dProps)[0]+"\n"
map.write(str1.encode("utf-8"))
sAllWords = " ".join(getDocStuff(dProps)[1])
lDocWords.append(docID+":"+sAllWords+"\n")
fillTerms(docID,getDocStuff(dProps)[1])
map.close()
mergeTermFiles()
makeTermsList()
#FILE = open(docFile,"w")
FILE1 = open(docWordsFile,"w")
FILE2 = open(termsFile,"w")
FILE3 = open(termsMapFile,"w")
# Write all the lines at once:
#FILE.writelines(docList)
FILE1.writelines(lDocWords)
FILE2.writelines(termsList)
FILE3.writelines(termMapList)
def fetchFromFile():
global T,W,B,A,N,I,dFileData
with open(constants.filetoIndex,encoding=constants.encoding) as inLinksFile:
dFileContents = {} # {title:S,contents:S,date:S,author:S,entryDate:S}
dFile={} # {docID:dFileContents}
previousProp=""
for line in inLinksFile:
#print(line[0:2])
if(len(line)>2):
line = line.replace("\n","")
if(line[0:2] == ".I"):
#print("Line has docID")
dFileData.update(dFile)
dFile={}
dFileContents={}
dFile[line.split()[1]]=dFileContents
#print(dFile)
previousProp=I
elif(line[0:2] == ".T"):
#print("Line has title ")
previousProp=T
elif(line[0:2] == ".W"):
#print("Line has contents")
previousProp=W
elif(line[0:2] == ".B"):
#print("Line has date of pub")
previousProp=B
elif(line[0:2] == ".A"):
#print("Line has Author")
previousProp=A
elif(line[0:2] == ".N"):
#print("Line has ENtry Date")
previousProp=N
elif(line[0:2] == ".X"):
#print("Line has reference")
previousProp=X
else :
#print()
tempX = []
if( previousProp != X):
if(previousProp in dFileContents):
tempX = dFileContents[previousProp]
#print("tempX : ",tempX)
line = re.sub('[^a-zA-Z0-9]', ' ', line)
tempX.append(line.lower())
dFileContents[previousProp]=tempX
dFileData.update(dFile)
#print(dFileData)
makeIndexes()
def main(fileName):
print("Started indexing : "+fileName)
helperFunctions.populateStopWords()
fetchFromFile()
#print(d)
#main(sys.argv[1])