-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser_module.py
More file actions
530 lines (493 loc) · 22.3 KB
/
parser_module.py
File metadata and controls
530 lines (493 loc) · 22.3 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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
from nltk.corpus import stopwords
from document import Document
import re
import math
from stemmer import Stemmer
class Parse:
def __init__(self):
self.stop_words = stopwords.words('english')
self.dictionary_term_index = {}
self.array_names_and_entities = {}
self.porter_stemmer = Stemmer()
def parse_sentence(self, text, stemmer=False):
"""
This function tokenize, remove stop words and apply lower case for every word within the text
:param text:
:return:
"""
list_percent = ["percent", "Percent", "Percentage", "percentage"]
self.array_names_and_entities = {}
self.dictionary_index = {}
text = text.replace("\n", ". ")
text = self.ignore_emojis(text)
array_text_space = text.split(" ")
array_text_space = self.separate_words_with_dots(array_text_space)
string_ans = ""
array_size = range(len(array_text_space))
string_ans_index = 0
entities_url = [] # help us to replace the url to "" because in get_entities it returns parts of the url
for word, idx in zip(array_text_space, array_size):
ans = ""
if word == '' or word == ' ': continue
check_digit = self.isdigit(word)
if len(word) < 2 and check_digit is False: continue
if len(word) < 2 or self.is_ascii(word) is False:
if check_digit is False:
word = self.remove_panctuation(word)
if self.is_ascii(word) is False or word == '' or word == " " or len(
word) < 2 or word.lower() not in self.stop_words:
continue
if ans == "" and self.is_url(word):
entities_url.append(word)
if "t.co" in word: continue
ans = self.parse_url(word)
if ans == "":
entities_url.remove(word)
continue
else:
if ans == "" and len(word) < 2 and word[0] != '#' and self.is_ascii(word) and not self.isfloat(word):
word = self.remove_panctuation(word)
if ans == "" and word[0] == '#':
temp_word = self.remove_panctuation(word)
if temp_word == "" or temp_word == "#":
continue
ans = self.parse_hashtag(temp_word)
elif ans == "" and word[0] == '@':
ans = self.remove_panctuation(word)
elif ans == "" and word in list_percent:
if idx > 0 and self.isfloat(array_text_space[idx - 1]):
ans = self.parse_percentage(array_text_space[idx - 1] + " " + word)
string_ans = string_ans[:len(string_ans) - 1 - len(ans)] + string_ans[
len(string_ans) + len(word):] + " "
else:
ans = word
elif ans == "" and (word.lstrip('-').isdigit() or self.isfloat(word.lstrip('-')) or self.isFraction(
word.lstrip('-')) or word.replace('~', '').isdigit()):
ans = self.convert_str_to_number(array_text_space, idx)
if ans == "":
pre_ans = self.remove_panctuation(word)
if len(pre_ans) < 2: continue
array_ans = pre_ans.split()
continued_array = []
for word_array_ans in array_ans:
splitted_word, is_number = self.split_word_to_numbers_strings(word_array_ans)
if splitted_word == '': continue
arr = splitted_word.split(" ")
for spl in arr:
if spl.lower() in self.stop_words or len(word_array_ans) < 2: continue
if is_number or self.check_two_letters(spl):
spl = self.remove_panctuation_special(spl)
string_ans += self.add_to_dictionary(spl, string_ans_index)
string_ans_index += len(word) + 1
continue
else:
string_ans += self.add_to_dictionary(word_array_ans.lower(), string_ans_index)
string_ans_index += len(word) + 1
else:
string_ans += self.add_to_dictionary(ans, string_ans_index)
string_ans_index += len(word) + 1
self.get_name_and_entities(entities_url, array_text_space)
array_parsed = string_ans.split()
ans = []
for word in array_parsed:
if word[0] != '#' and word[0] != '@':
if self.check_two_letters(word):
us_word = self.remove_panctuation_special(word)
ans.append(us_word)
continue
ans.append(word)
return ans, self.array_names_and_entities
def separate_words_with_dots(self, array_text):
new_text = ""
length = range(len(array_text))
for i in length:
word = array_text[i]
if '.' not in word:
if word == '': continue
new_text += word + " "
continue
if "http" in word or "www" in word or "t.co" in word or self.isfloat(word):
check_regular_point = word.split('.', 1)
if check_regular_point[0] != '' and check_regular_point[1] != '' and self.is_url(
check_regular_point[1]):
new_text += check_regular_point[0] + '. ' + check_regular_point[1]
continue
if check_regular_point[1] == '':
new_text += check_regular_point[0] + " "
continue
new_text += word + " "
continue
if self.check_two_letters(word):
us_word = self.remove_panctuation_special(word)
new_text += us_word + " "
continue
separate = str(word).split('.')
new_text += separate[0] + ". " + separate[1] + " "
return new_text.lstrip().split(" ")
def is_url(self, text):
'''
check if string is a url path
:param text: url
:return: boolean
'''
regex = re.compile(
r'^(?:http|ftp)s?://|(?:www)?.' # http:// or https://
r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' # domain...
r'localhost|' # localhost...
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
r'(?::\d+)?' # optional port
r'(?:/?|[/?]\S+)$', re.IGNORECASE)
return re.match(regex, text) is not None
def add_to_dictionary(self, text, index):
array_of_words = text.split(" ")
ans = ""
for word in array_of_words:
ans += word + " "
self.dictionary_index[word] = index
if ans == "": return ""
return ans
def parse_hashtag(self, phrase):
""""
parser hash tag and lower the letters
return array of string
#stayAtHome -> ['#stayathome',stay,at,home]
"""
original_phrase = phrase
pattern = re.compile(r"[A-Z][a-z]+|\d+|[A-Z]+(?![a-z])")
if phrase[1].islower() and '_' not in original_phrase:
phrase = phrase[:1] + phrase[1].upper() + phrase[2:]
temp = pattern.findall(phrase)
all_words = phrase[1:].split("_")
for word in all_words:
if word != phrase[1:] and word.lower() and word not in temp: temp.append(word)
temp = [str_to_lower.lower() for str_to_lower in temp]
# temp.insert(0, original_phrase[0:len(original_phrase)].lower().replace('_', ''))
i = 0
len_temp = len(temp)
while i < len_temp:
if temp[i] in self.stop_words or len(temp[i]) < 2:
temp[i] = ''
i += 1
return " ".join(temp).lstrip().rstrip()
def parse_url(self, string):
"""
parsing url path
return an array of the components
"""
if string is not None:
ans = string.split("/")
ans_len = len(ans)
remove_www = ""
if ans_len > 0:
for term in ans:
remove_www += term.replace("www.", "") + " "
ans[0] = ans[0].replace(ans[0], remove_www)
string_without_stopword = ""
length = range(len(ans))
ans_string = ans[0].split(" ")
for word, idx in zip(ans_string, length):
if word == '' or word == ' ': continue
if len(word) < 2 or (len(word) > 0 and word[0] == '#'): continue
if word not in self.stop_words or word.isnumeric():
if not self.is_url(word):
word = self.remove_panctuation(word)
string_without_stopword += word + " "
return string_without_stopword.lstrip()
else:
return ""
def isdigit(self, word):
if "0" <= word <= "9":
return True
return False
def isfloat(self, value):
"""
check if value is a float number
:return: boolean
"""
try:
float(value)
return True
except ValueError:
return False
def isFraction(self, token):
"""
check if value is a fraction number
:return: boolean
"""
if '/' not in token:
return False
values = token.split('/')
return all(i.isdigit() for i in values)
def convert_str_to_number_kmb(self, word):
"""
check if value is a float number, and return the wanted number. etc: 1000->1K, 1013456->1.013M
:return: boolean
"""
tmb = ''
if word >= 1000000000 or word <= -1000000000:
word = float(word / 1000000000)
tmb = 'B'
elif word >= 1000000 or word <= -1000000:
word = float(word / 1000000)
tmb = 'M'
elif word >= 1000 or word <= -1000:
word = float(word / 1000)
tmb = 'K'
ans = '{:0.3f}'.format(word)
return '{0:g}'.format(float(ans)) + tmb
def check_two_letters(self, word):
if 0 < len(word) < 7 and (word.upper()[0] == 'U' and 'S' in word.upper()):
start = word.upper().find('U') + 1
end = word.upper().find('S', start)
dot = word[start:end]
if dot == '.':
return True
def split_word_to_numbers_strings(self, word):
try:
if self.check_two_letters(word):
us_word = self.remove_panctuation_special(word)
return us_word, False
res = re.findall(r'[A-Za-z]+|\d+', word)
if len(res)==0: return '', False
if len(word) > 0 and self.isfloat(res[0]):
if len(res) > 1 and (
"thousand" in res[1].lower() or "million" in res[1].lower() or "billion" in res[1].lower()
or "b" in res[1].lower() or "m" in res[1].lower() or "k" in res[1].lower()):
if "thousand" in word.lower(): return word.replace(res[1], "K"), True
if 'k' in word.lower(): return word.replace(res[1], "K"),True
if "million" in word.lower(): return word.replace(res[1], "M"),True
if 'm' in word.lower(): return word.replace(res[1], "M"),True
if "billion" in word.lower(): return word.replace(res[1], "B"),True
if 'b' in word.lower(): return word.replace(res[1], "B"),True
else:
return (" ".join(res).lstrip().rstrip(),True)
else:
is_number =False
return (" ".join(res).lstrip().rstrip(),False)
except:
return word, False
def convert_str_to_number(self, text_demo, idx):
"""
check every type of number and return it as a string. etc: 1K,1M,1B,-900,23/5,2020,2K
:return: boolean
"""
help_minus = ''
text_return = []
my_word = text_demo[idx]
text_demo_length = len(text_demo)
my_word = my_word.replace(",", "")
if re.search('-', my_word):
help_minus = '-'
my_word = my_word.replace("-", "")
if not self.isfloat(my_word): my_word = self.remove_panctuation(my_word)
if self.isFraction(my_word):
if idx + 1 == text_demo_length:
return ''.join(help_minus + my_word)
text_return = ''.join(help_minus + my_word)
token_next = text_demo[idx + 1].lower()
if token_next == "billion" or token_next == "billions":
text_return += 'B'
text_demo[idx + 1] = ""
if token_next == "million" or token_next == "millions":
text_return += 'M'
text_demo[idx + 1] = ""
if text_demo[idx + 1] == "thousand" or token_next == "thousands":
text_return += 'K'
text_demo[idx + 1] = ""
return help_minus + ''.join(text_return)
if my_word != '' and not math.isnan(float(my_word)):
number = float(my_word)
number_numerize = self.convert_str_to_number_kmb(number)
if idx + 1 < len(text_demo):
token_next = text_demo[idx + 1].lower()
number_to_input = str(number_numerize)
if token_next == "billion" or token_next == "billions":
if 'K' in number_numerize or 'M' in number_numerize:
number_to_input = (number_to_input.translate({ord('K'): None}))
number_to_input = (number_to_input.translate({ord('M'): None}))
text_return.append(my_word)
else:
text_return.append(str(number_numerize + 'B'))
text_demo[idx + 1] = ""
elif token_next == "million" or token_next == "millions":
if 'K' in number_numerize:
number_to_input = (number_to_input.translate({ord('K'): None}))
text_return.append(number_to_input + 'B')
else:
number_to_input = str(number_numerize)
text_return.append(number_to_input + 'M')
text_demo[idx + 1] = ""
elif token_next == "thousand" or token_next == "thousands":
if 'K' in number_numerize:
number_to_input = (number_to_input.translate({ord('K'): None}))
text_return.append(number_to_input + 'M')
elif 'M' in number_numerize:
number_to_input = (number_to_input.translate({ord('M'): None}))
text_return.append(number_to_input + 'B')
else:
text_return.append(number_to_input + 'K')
text_demo[idx + 1] = ""
elif 1000 > number > -1000:
text_return.append(number_numerize)
else:
text_return.append(number_numerize)
else:
text_return.append(number_numerize)
if 1900 < number < 2100 and help_minus == '':
if '~' in text_demo[idx]:
text_return.append(my_word)
else:
len_number = len(text_demo[idx])
if text_demo[idx][len_number - 1] == '.':
res = my_word.replace('.','')
text_return.append(res)
else:
text_return.append(text_demo[idx])
return help_minus + ' '.join(text_return)
def ignore_emojis(self, text):
emoji_pattern = re.compile("["
u"\U0001F600-\U0001F64F" # emoticons
u"\U0001F300-\U0001F5FF" # symbols & pictographs
u"\U0001F680-\U0001F6FF" # transport & map symbols
u"\U0001F1E0-\U0001F1FF" # flags (iOS)
u"\U00002500-\U00002BEF" # chinese char
u"\U00002702-\U000027B0"
u"\U00002702-\U000027B0"
u"\U000024C2-\U0001F251"
u"\U0001f926-\U0001f937"
u"\U00010000-\U0010ffff"
u"\u2640-\u2642"
u"\u2600-\u2B55"
u"\u200d"
u"\u23cf"
u"\u23e9"
u"\u231a"
u"\ufe0f" # dingbats
u"\u3030"
"]+", flags=re.UNICODE)
ans = emoji_pattern.sub(r'', text)
return ans
def is_ascii(self, s):
ans = all(ord(c) < 128 or c == '…' or c == '’' or c == '³' or c == "¹⁹" for c in s)
return ans
def parse_percentage(self, string):
"""
change word to percent
100 percent -> 100%
:param string: string to check if there is a percent within
:return: array of converted strings
"""
return re.split('\s+', string)[0] + '%'
def remove_panctuation_special(self, word):
"""
remove pancuations from word U.S (like U.S., or U.S.'s)
:param word
:return: word without panctuation
"""
if 'a' in word.lower():
temp = word[:5]
to_pancuate = word.replace(temp, '')
# word = word.lower().replace("u.s", '')
word = temp + self.remove_panctuation(to_pancuate)
return word
else:
temp = word[:3]
to_pancuate = word.replace(temp, '')
# word = word.lower().replace("u.s", '')
word = temp + self.remove_panctuation(to_pancuate.lower())
return word
def remove_panctuation(self, word):
"""
remove pancuations from word (like . or , or : )
:param word
:return: word without panctuation
"""
if self.check_two_letters(word):
#word = self.remove_panctuation_us(word)
return word
if re.match(r'[^@]+@[^@]+\.[^@]+', word): return word
if "#" == word or "##" == word: return ""
if word[-2:] == "'s" or word[-2:] == "’s" or word[-2:] == "`s": word = word.replace(word[-2:], "")
smiles = [":)", ":(", ":-]", ":-)", ";)", ";-)", ":-(", ";(", ";-(", ":-P", ":P", ":p", ":-p"]
for smile in smiles:
if smile in word: word = word.replace(smile, "")
if word in smiles: return ''
if "\n" in word: word = word.replace("\n", " ")
if '#' in word and word[0] != '#': word = word.replace("#", "")
if '_' in word and '#' not in word:
word = word.replace("_", "")
if '@' in word and word[0] != '@': word = word.replace("@", "")
word = word.replace("-", " ")
word = word.replace("'", "")
word = re.sub(r'[€£€4️⃣“”‘‼⑥²⁸¹❶❷❽②⑦&$~’.,!…|?,…:;^"{}*=+()⁰\/[\[\]]', '', word)
return word
def get_name_and_entities(self, entities_url, array_text_space):
text = ""
for word in array_text_space:
if word == '' or word == '' or word[0] == '@' or word[0] == '#' or word == "RT": continue
text += word + " "
rx2 = re.compile(r'[A-Z][-a-zA-Z]+[1-9]*(?:\s+[A-Z][-a-zA-Z]+[1-9]*)*')
matches = rx2.findall(text)
tokinzed_entity_new = set()
i = 0
for i in range(len(matches)):
if len(str(matches[i]).split()) > 1:
tokinzed_entity_new.add(str(matches[i]))
i += 1
if "COVID 19" in text: tokinzed_entity_new.add("COVID 19")
if "Covid 19" in text: tokinzed_entity_new.add("Covid 19")
for word in tokinzed_entity_new:
if word.lower() not in self.stop_words:
all_places = [m.start() for m in re.finditer(word, text)]
self.array_names_and_entities[word] = all_places
return tokinzed_entity_new
def parse_doc(self, doc_as_list, stemmer=False):
"""
This function takes a tweet document as list and break it into different fields
:param doc_as_list: list re-preseting the tweet.
:return: Document object with corresponding fields.
"""
tweet_id = doc_as_list[0]
tweet_date = doc_as_list[1]
full_text = doc_as_list[2]
url = doc_as_list[3]
indices = doc_as_list[4]
retweet_text = doc_as_list[5]
retweet_url = doc_as_list[6]
retweet_indices = doc_as_list[7]
quote_text = doc_as_list[8]
quote_url = doc_as_list[9]
quote_indices = doc_as_list[10]
term_dict = {}
entities_local_dict = {}
array_url_parsed = []
url = str(url)
rt = False
if "RT" in full_text:
rt = True
tokenized_text, names_and_entities = self.parse_sentence(full_text, stemmer=False)
doc_length = len(tokenized_text) # after text operations.
if doc_length == 0:
return None
for term in tokenized_text:
if len(term) < 2:
continue
elif term.isdigit() and len(term) > 3:
continue
if stemmer:
term = self.porter_stemmer.stem_term(term)
if term not in term_dict.keys():
term_dict[term] = 1
else:
term_dict[term] += 1
for term in names_and_entities.keys():
if len(term) < 2: continue
if term in self.stop_words:
continue
if term not in term_dict.keys():
term_dict[term] = 1
else:
term_dict[term] += 1
document = Document(tweet_id, tweet_date, full_text, url, retweet_text, retweet_url, quote_text,
quote_url, term_dict, len(self.array_names_and_entities), rt, doc_length)
return document