-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateTokens.py
More file actions
370 lines (285 loc) · 18 KB
/
createTokens.py
File metadata and controls
370 lines (285 loc) · 18 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
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
import os
from nltk import WordNetLemmatizer
from lxml import html
import re
import math
import sys
from bs4 import BeautifulSoup as Soup
import warnings
class CreateTokens:
# treats warnings as errors so we can catch them using bs4
warnings.simplefilter('error', UserWarning)
# nltk.download('wordnet')
lemmatizer = WordNetLemmatizer()
# 173 stop words
stop_words = {'on', "let's", 'so', "that's", 'below', 'the', 'is',
'which', 'their', 'after', 'an', 'her', 'before', 'how',
"he'll", 'if', "i'm", 'between', 'could', 'does', 'it',
'while', 'than', 'whom', 'my', 'further', "weren't",
'here', "she'd", 'his', 'there', 'with', "we've", 'cannot',
'but', 'up', 'had', "wouldn't", 'its', 'for', 'are', 'or',
'as', 'into', 'from', 'again', "when's", "wasn't", 'ought',
"won't", "shan't", 'until', 'herself', 'through', "he's",
"i'll", 'myself', "she's", 'in', 'once', "hadn't", 'only',
'did', 'they', 'being', "can't", "what's", 'very', 'would',
"haven't", "doesn't", "there's", "they've", 'do', "it's",
"they'll", 'am', "you'd", 'a', 'have', 'yourself', 'off',
"you're", 'were', "why's", 'hers', 'out', 'above', "he'd",
'i', 'was', 'each', 'yours', 'those', 'doing', 'you', 'who',
'more', 'she', "where's", 'some', "you'll", "we're", "couldn't",
"we'll", "aren't", 'under', "mustn't", 'to', 'most', "who's",
"we'd", 'no', "they'd", 'he', 'him', 'both', 'of', 'because',
"i'd", 'this', 'all', 'and', 'himself', 'yourselves', 'itself',
'over', 'why', 'these', 'any', "shouldn't", "didn't", 'having',
'not', 'our', 'own', 'at', 'should', 'themselves', "she'll",
'theirs', 'me', "isn't", 'same', 'by', 'been', 'such', 'them',
"i've", 'what', 'when', "here's", 'nor', 'other', 'too', 'be',
"don't", 'ours', 'ourselves', 'that', "you've", 'we', 'where',
"how's", 'during', 'has', 'down', 'then', "they're", 'few',
'against', "hasn't", 'your', 'about'}
def create_markup_dict(self, markupDict: dict, line: str):
# i, u, h5, h6 = 0.5
# h2, h3, h4, bold, strong = 1
# h1 = 2
# title = 3
try:
soup = Soup(line, "html.parser")
for section in soup.find_all(['h5', 'h6', 'i', 'u']):
for word in self.getValidWords(section.text):
if word in markupDict.keys():
markupDict[word] += 0.5
else:
markupDict[word] = 0.5
for section in soup.find_all(['h2', 'h3', 'h4', 'bold', 'strong', 'a']):
for word in self.getValidWords(section.text):
if word in markupDict.keys():
markupDict[word] += 1
else:
markupDict[word] = 1
for section in soup.find_all('h1'):
for word in self.getValidWords(section.text):
if word in markupDict.keys():
markupDict[word] += 2
else:
markupDict[word] = 2
for section in soup.find_all('title'):
for word in self.getValidWords(section.text):
if word in markupDict.keys():
markupDict[word] += 3
else:
markupDict[word] = 3
except UserWarning:
pass
def tokenize_words_in_line(self, snippetArr, urlsAndContentDict: dict, indexDictionary: dict,
helperArrayPositionDict: dict, line: str, urlName: str, markUpDict: dict, corpusSize):
try:
# remove html markup
htmlParse = html.document_fromstring(line.encode("utf-8"))
content = htmlParse.text_content()
content = content.strip()
content = content.lower()
# Split words by space and punctuation
for word in re.split('! |!|; |;|, |,|\s+|\.', content):
if word not in self.stop_words:
word.replace('\'', '')
word.replace('\"', '')
word.replace('-', '')
word.replace('(', '')
word.replace(')', '')
# If its alphanumeric, but we allow # and $
if re.fullmatch(r"[#$a-zA-Z0-9]{2,}", word):
# Include this valid token in the returned snippet array
snippetArr.append(word)
# Example: removes s from plural words
wordAfterLemmatizer = self.lemmatizer.lemmatize(word)
if wordAfterLemmatizer:
# ADDED / IN FRONT OF $ TO NOT BREAK DATABASE
wordAfterLemmatizer = self.hideFirstCharDollarSign(wordAfterLemmatizer)
# Update content dictionary
if wordAfterLemmatizer in urlsAndContentDict[urlName]['words']:
urlsAndContentDict[urlName]['words'][wordAfterLemmatizer] += 1
else:
urlsAndContentDict[urlName]['words'][wordAfterLemmatizer] = 1
# if token is not yet in dictionary, insert it, with the value
# set to an empty dict.
position = -1
if wordAfterLemmatizer not in indexDictionary:
indexDictionary[wordAfterLemmatizer] = dict()
indexDictionary[wordAfterLemmatizer]['word'] = wordAfterLemmatizer
# # INNER dict for the URL's keys (urlName and tf)
# indexDictionary[wordAfterLemmatizer]['urls'] = dict()
indexDictionary[wordAfterLemmatizer]['urls'] = []
indexDictionary[wordAfterLemmatizer]['urls'].append({
'urlName': urlName,
'tf': 1
})
# add the word to the arr index helper dict so we can know what
# INDEX the dictionary with the URL is at so we dont have to
# loop over it checking (cant index it in a list)
helperArrayPositionDict[wordAfterLemmatizer] = dict()
helperArrayPositionDict[wordAfterLemmatizer]['word'] = wordAfterLemmatizer
# we know its at index 0 since the URL list is empty in this if
helperArrayPositionDict[wordAfterLemmatizer]['urls'] = dict()
helperArrayPositionDict[wordAfterLemmatizer]['urls'][urlName] = 0
# token already in our index
else:
# check if URL is in the helper so we can get the array index number to alter the entry
if urlName in helperArrayPositionDict[wordAfterLemmatizer]['urls']:
position = helperArrayPositionDict[wordAfterLemmatizer]['urls'][urlName]
# update the dictionary at that position
indexDictionary[wordAfterLemmatizer]['urls'][position]['tf'] += 1
# url must be appended to the list, and that index must be added to the
# helper dictionary
else:
indexDictionary[wordAfterLemmatizer]['urls'].append({
'urlName': urlName,
'tf': 1
})
position = len(indexDictionary[wordAfterLemmatizer]['urls']) - 1
helperArrayPositionDict[wordAfterLemmatizer]['urls'][urlName] = position
lengthOfUrlDictionary = len(indexDictionary[wordAfterLemmatizer]['urls'])
indexDictionary[wordAfterLemmatizer]['df'] = lengthOfUrlDictionary
# Calculate the tfidf for every search
tf = indexDictionary[wordAfterLemmatizer]['urls'][position]['tf']
tfidf = self.getTfidf(int(tf), int(lengthOfUrlDictionary), corpusSize)
indexDictionary[wordAfterLemmatizer]['urls'][position]['tfidf'] = math.log(tfidf)
# IF THE WORD IS IN SPECIAL MARKUP: add the tf that was in the already scanned markUpDict
for word, additionalTf in markUpDict.items():
if word in urlsAndContentDict[urlName]['words']:
urlsAndContentDict[urlName]['words'][word] += additionalTf
except Exception as e:
if str(e) != 'Document is empty':
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
print(exc_type, fname, exc_tb.tb_lineno)
# exit()
# send back the line so that a snippet can be stored
return snippetArr
def twograms_tokenize_words_in_line(self, twogramUrlsAndContentDict: dict, twogramIndexDictionary: dict,
twogramHelperArrayPositionDict: dict, line: str, urlName: str,
markUpDict: dict, corpusSize):
try:
# remove html markup
htmlParse = html.document_fromstring(line.encode("utf-8"))
content = htmlParse.text_content()
content = content.strip()
content = content.lower()
# Split words by. ......
all_words = re.split('; |;|, |,|\s+|\.', content)
count = 0
while count < len(all_words):
if count + 1 <= len(all_words) - 1:
word = all_words[count]
second_word = all_words[count + 1]
if word not in self.stop_words and second_word not in self.stop_words:
word.replace('\'', '')
word.replace('\"', '')
word.replace('-', '')
word.replace('(', '')
word.replace(')', '')
second_word.replace('\'', '')
second_word.replace('\"', '')
second_word.replace('-', '')
second_word.replace('(', '')
second_word.replace(')', '')
if re.fullmatch(r"[#$a-zA-Z0-9]{2,}", word) and re.fullmatch(r"[#$a-zA-Z0-9]{2,}", second_word): # consider
wordAfterLemmatizer = self.lemmatizer.lemmatize(word)
secondWordAfterLemmatizer = self.lemmatizer.lemmatize(second_word)
wordAfterLemmatizer += ' ' + secondWordAfterLemmatizer
# ADDED / IN FRONT OF $ TO NOT BREAK DATABASE
if wordAfterLemmatizer:
wordAfterLemmatizer = self.hideFirstCharDollarSign(wordAfterLemmatizer)
# IF THE WORD IS NOT IN SPECIAL MARKUP: add one or set to one for tf
if not wordAfterLemmatizer in markUpDict.keys():
# Update content dictionary
if wordAfterLemmatizer in twogramUrlsAndContentDict[urlName]['words']:
twogramUrlsAndContentDict[urlName]['words'][wordAfterLemmatizer] += 1
else:
twogramUrlsAndContentDict[urlName]['words'][wordAfterLemmatizer] = 1
# IF THE WORD IS IN SPECIAL MARKUP: add the tf that was in the alreadu scanned markUpDict
else:
# Update content dictionary
if wordAfterLemmatizer in twogramUrlsAndContentDict[urlName]['words']:
twogramUrlsAndContentDict[urlName]['words'][wordAfterLemmatizer] += markUpDict[
wordAfterLemmatizer]
else:
twogramUrlsAndContentDict[urlName]['words'][wordAfterLemmatizer] = markUpDict[
wordAfterLemmatizer]
# if token is not yet in dictionary, insert it, with the value
# set to an empty dict.
position = -1
if wordAfterLemmatizer not in twogramIndexDictionary:
twogramIndexDictionary[wordAfterLemmatizer] = dict()
twogramIndexDictionary[wordAfterLemmatizer]['word'] = wordAfterLemmatizer
# add the word to the arr index helper dict so we can know what
# INDEX the dictionary with the URL is at so we dont have to
# loop over it checking (cant index it in a list)
twogramHelperArrayPositionDict[wordAfterLemmatizer] = dict()
twogramHelperArrayPositionDict[wordAfterLemmatizer]['word'] = wordAfterLemmatizer
# we know its at index 0 since the URL list is empty in this if
twogramHelperArrayPositionDict[wordAfterLemmatizer]['urls'] = dict()
twogramHelperArrayPositionDict[wordAfterLemmatizer]['urls'][urlName] = 0
# # INNER dict for the URL's keys (urlName and tf)
twogramIndexDictionary[wordAfterLemmatizer]['urls'] = []
twogramIndexDictionary[wordAfterLemmatizer]['urls'].append({
'urlName': urlName,
'tf': 1
})
# token already in our index
else:
# print(urlsAndContentDict)
# check if URL is in the helper so we can get the array index number to alter the entry
if urlName in twogramHelperArrayPositionDict[wordAfterLemmatizer]['urls']:
position = twogramHelperArrayPositionDict[wordAfterLemmatizer]['urls'][urlName]
# update the dictionary at that position
twogramIndexDictionary[wordAfterLemmatizer]['urls'][position]['tf'] += 1
# url must be appended to the list, and that index must be added to the
# helper dictionary
else:
twogramIndexDictionary[wordAfterLemmatizer]['urls'].append({
'urlName': urlName,
'tf': 1
})
position = len(twogramIndexDictionary[wordAfterLemmatizer]['urls']) - 1
twogramHelperArrayPositionDict[wordAfterLemmatizer]['urls'][urlName] = position
lengthOfUrlDictionary = len(twogramIndexDictionary[wordAfterLemmatizer]['urls'])
twogramIndexDictionary[wordAfterLemmatizer]['df'] = lengthOfUrlDictionary
# Calculate the tfidf for every search
tf = twogramIndexDictionary[wordAfterLemmatizer]['urls'][position]['tf']
tfidf = self.getTfidf(int(tf), int(lengthOfUrlDictionary), corpusSize)
twogramIndexDictionary[wordAfterLemmatizer]['urls'][position]['tfidf'] = tfidf
count += 1
# IF THE WORD IS IN SPECIAL MARKUP: add the tf that was in the already scanned markUpDict
for word, additionalTf in markUpDict.items():
if word in twogramUrlsAndContentDict[urlName]['words']:
twogramUrlsAndContentDict[urlName]['words'][word] += additionalTf
except Exception as e:
if str(e) != 'Document is empty':
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
print(exc_type, fname, exc_tb.tb_lineno)
# exit()
def getTfidf(self, tf, df, totalDocs):
return (1 + math.log(tf)) * math.log(totalDocs / df)
def hideFirstCharDollarSign(self, stringToCheck):
while stringToCheck[0] == '$':
stringToCheck = '/' + stringToCheck
return stringToCheck
def getValidWords(self, content):
validWords = []
content = content.strip()
content = content.lower()
for word in re.split('! |!|; |;|, |,|\s+|\.', content):
if word not in self.stop_words:
word.replace('\'', '')
word.replace('\"', '')
word.replace('-', '')
word.replace('(', '')
word.replace(')', '')
if re.fullmatch(r"[#$a-zA-Z0-9]{2,}", word):
wordAfterLemmatizer = self.lemmatizer.lemmatize(word)
# ADDED / IN FRONT OF $ TO NOT BREAK DATABASE
if wordAfterLemmatizer:
wordAfterLemmatizer = self.hideFirstCharDollarSign(wordAfterLemmatizer)
validWords.append(wordAfterLemmatizer)
return validWords