-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIndexer.py
More file actions
executable file
·311 lines (228 loc) · 10.1 KB
/
Copy pathIndexer.py
File metadata and controls
executable file
·311 lines (228 loc) · 10.1 KB
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
from Model import *
import sqlite3
from bs4 import Comment
from bs4 import BeautifulSoup
from nltk.stem.porter import *
from nltk.corpus import stopwords
class Indexer:
def __init__(self):
pass
def update(self,url,content):
#delete old entries of this url (if exists any)
self._deleteOldEntries(url)
# for phrase search
#try:
document,title = self.phraseParser(url,content)
#except:
# print("FUCK!!!")
pass
if title is None:
title = url
pass
self.addToFullPages(document, url,title.strip())
bulkEntries = []
#Stemmer
stemmer = PorterStemmer()
#parse the web page content
wordIndexes, wordImportance = self.parser(content)
#insert page extracted words in a list for bulk db insertions
for word in wordImportance:
##avoiding alphaNum words...
if word in wordIndexes: #and not re.search('\d+',word):
stemmed = stemmer.stem(word)
bulkEntries.append({'keyword' : word ,'stem': stemmed, 'url': url, 'positions' : wordIndexes[word], 'importance':wordImportance[word]})
if(bulkEntries):
#insert new entries in IndexerTable...Fastest way!
with DBIndexer.atomic():
#sqlite max vars = 999 per bulk insertion
size = (999 // len(bulkEntries[0]))
for i in range(0, len(bulkEntries), size):
while True:
try:
IndexerTable.insert_many(bulkEntries[i:i + size]).upsert().execute()
break
except (OperationalError, sqlite3.OperationalError) as e:
if 'binding' in str(e):
break
print('INDEXER: Database busy, retrying. bulk Insertions')
except:
break
print("Inserted:",len(bulkEntries),"new entries for url:",url)
def addToFullPages(self,content,url,title):
while True:
try:
FullPages.create(pageURL=url, pageContent=content, pageTitle=title).update()
break
except (OperationalError, sqlite3.OperationalError) as e:
if 'binding' in str(e):
break
print('INDEXER: Database busy, retrying. FullPages Insertion')
#except:
#print("FUCKK")
#break
def _deleteOldEntries(self,url):
query = IndexerTable.delete().where(IndexerTable.url == url)
pquery = FullPages.delete().where(FullPages.pageURL == url)
pquery.execute()
print("Deleted:", query.execute(), "old entries for url:",url)
'''inserts a new entry(keyword,url)'''
def _addToIndex(self, keyword, url, pos = [], importance = 2):
try:
IndexerTable.create(keyword= keyword , url = url, positions = pos, importance = importance).update()
except IntegrityError: # keyword & url already exists!
return -1
return 1
'''Searching with words...IN: list of keywords, OUT: dict[keyword] of urls'''
def lookupWithWords(self, keywords = []):
result = {}
for word in keywords:
urlList = []
try:
resultQuery = IndexerTable.select(IndexerTable.url).where(IndexerTable.keyword == word)
for x in resultQuery:
urlList.append(x.url)
except IndexerTable.DoesNotExist:
urlList.append(-1)
result.update({word: urlList})
return result
'''getting keywords with urls...IN: list of urls, OUT: dict[url] of keywords'''
def lookupWithPages(self, pages=[]):
result = {}
for pg in pages:
wordList = []
try:
resultQuery = IndexerTable.select(IndexerTable.keyword).where(IndexerTable.url == pg)
for x in resultQuery:
wordList.append(x.keyword)
except IndexerTable.DoesNotExist:
wordList.append(-1)
result.update({pg: wordList})
return result
'''-----------------------------------Parsing functions----------------------------------------------------------'''
'''IN:[word1, word2, ...], OUT:{word1: [pos1, pos2], word2: [pos2, pos3], ...}'''
def _indexFile(self,wordList):
fileIndex = {}
for index, word in enumerate(wordList):
if word in fileIndex.keys():
fileIndex[word].append(index)
else:
fileIndex[word] = [index]
return fileIndex
def _assignImportance(self,keywords,count,imp,importanceMap = {}):
for word in keywords:
if not (word in importanceMap):
importanceMap[word]= imp
count+=1
'''removes text in scripts and comments'''
def _visibleText(self,element):
if element.parent.name in ['style', 'script', 'link', '[document]']:
return False
elif re.match('.*<!--.*-->.*', str(element)):
return False
return True
def _parseKeywords(self,text):
for i in range(len(text)):
pattern = re.compile('[\W_]+')
text[i] = pattern.sub(' ', text[i])
re.sub(r'[\W_]+', '', text[i])
text[i] = text[i].lower()
text += text[i].split()
text[i] = None
return text
def _removeStopWords(self,wordList):
stop = set(stopwords.words('english'))
wordList = [word for word in wordList if word not in stop and len(word) > 2]
return wordList
def _initParser(self,text):
soup = BeautifulSoup(text, 'html.parser')
# ---------------Finding Comments to remove them---------------
comments = soup.findAll(text=lambda text: isinstance(text, Comment))
[comment.extract() for comment in comments]
# ---------------Getting Text without Comments-------------------
texts = soup.find_all(text=True)
# ---------------removing the unwanted script and links from the doc and returning list of words------------
visibleTexts = filter(self._visibleText, texts)
# ---------------remove all symbols like #$%@ , spaces and newlines--------------
parsedWords = self._parseKeywords(list(visibleTexts))
parsedWords = [word for word in parsedWords if word is not ' ' and word is not None]
# remove stop words
parsedWords = self._removeStopWords(parsedWords)
return parsedWords
'''parsing an html doc'''
def parser(self, htmlDoc):
soup = BeautifulSoup(htmlDoc, 'html.parser')
count = 0
importMap ={}
#-----------------parsing Title and assigning its importance map----------------
if(soup.title and soup.title.string is not None):
title = self._initParser(str(soup.title))
self._assignImportance(title,count,0,importMap)
#parsing headers
headers = [soup.h1 , soup.h2 , soup.h3 , soup.h4
, soup.h5 , soup.h6]
headers = [str(x) for x in headers if x is not None]
#print(headers)
headers = self._initParser(' '.join(headers))
self._assignImportance(headers, count, 1, importMap)
#---------------------parsing plain text---------------------
plainText = self._initParser(htmlDoc)
self._assignImportance(plainText,count,2,importMap)
# --------------indexing words -------------
indexMap = self._indexFile(plainText)
return indexMap,importMap
def phraseParser(self,url,text):
soup = BeautifulSoup(text, 'html.parser')
title = None
if (soup.title and soup.title.string is not None):
#try:
title = soup.title.string
#except:
# print("AAA")
#print(title)
if(title is None):
# parsing headers
headers = [soup.h1, soup.h2, soup.h3, soup.h4
, soup.h5, soup.h6]
headers = [str(x) for x in headers if x is not None]
#print(headers)
for header in headers:
#print(header)
soupy = BeautifulSoup(header, 'html.parser')
comments = soupy.findAll(text=lambda text: isinstance(header, Comment))
[comment.extract() for comment in comments]
texts = soupy.find_all(text=True)
header = filter(self._visibleText, texts)
header = list(header)
if len(header) == 1:
title = header[0]
break
if(header):
title = header[0]
pass
#print(soup.get_text())
# ---------------Finding Comments to remove them---------------
comments = soup.findAll(text=lambda text: isinstance(text, Comment))
[comment.extract() for comment in comments]
# ---------------Getting Text without Comments-------------------
texts = soup.find_all(text=True)
# ---------------removing the unwanted script and links from the doc and returning list of words------------
visibleTexts = filter(self._visibleText, texts)
#thefile.close()'''
# ---------------remove all symbols like #$%@ , spaces and newlines--------------
parsedWords = self._parseKeywords(list(visibleTexts))
parsedWords = [word for word in parsedWords if word is not ' ' and word is not None]
pass #extra spaces!
space = ' '
document = space.join(parsedWords)
visibleTexts = filter(self._visibleText, texts)
waw = list(visibleTexts)
listy = [x.strip() for x in waw if x.strip() is not '' and x is not None]
fname = url.replace("/", "")
#with open(fname + '.txt', 'w') as thefile:
# # print(str(visibleTexts))
# for item in listy:
# s = item
# print('s:', s)
# thefile.write("%s\n" % s)
print(title)
return document, title