/
Helper.py
548 lines (523 loc) · 16.4 KB
/
Helper.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
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
# Code provided by self-generated Capstone II
import pandas as pd
import numpy as np
import re
import nltk
import emoji
import spacy
import math
import string
import unicodedata
from nltk.corpus import stopwords, wordnet
from nltk.stem import WordNetLemmatizer
from collections import Counter
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
from spellchecker import SpellChecker
from nltk.tokenize import sent_tokenize, word_tokenize
from langid.langid import LanguageIdentifier, model
sw = set(stopwords.words('english'))
new_words = set([i.lower() for i in ['trump','biden','joe','joebiden','donald','donaldtrump','white',
'lady','first','president','debate','vote','say','lose','people',
'camila','harris','camilaharris','mike','pence','mikepence']])
sw.update(new_words)
wordnet_map = {"N":wordnet.NOUN, "V":wordnet.VERB, "J":wordnet.ADJ, "R":wordnet.ADV}
lemmatizer = WordNetLemmatizer()
# Provided by https://github.com/NeelShah18/emot/blob/master/emot/emo_unicode.py
EMOTICONS = {
u":‑\)":"Happy face or smiley",
u":\)":"Happy face or smiley",
u":-\]":"Happy face or smiley",
u":\]":"Happy face or smiley",
u":-3":"Happy face smiley",
u":3":"Happy face smiley",
u":->":"Happy face smiley",
u":>":"Happy face smiley",
u"8-\)":"Happy face smiley",
u":o\)":"Happy face smiley",
u":-\}":"Happy face smiley",
u":\}":"Happy face smiley",
u":-\)":"Happy face smiley",
u":c\)":"Happy face smiley",
u":\^\)":"Happy face smiley",
u"=\]":"Happy face smiley",
u"=\)":"Happy face smiley",
u":‑D":"Laughing, big grin or laugh with glasses",
u":D":"Laughing, big grin or laugh with glasses",
u"8‑D":"Laughing, big grin or laugh with glasses",
u"8D":"Laughing, big grin or laugh with glasses",
u"X‑D":"Laughing, big grin or laugh with glasses",
u"XD":"Laughing, big grin or laugh with glasses",
u"=D":"Laughing, big grin or laugh with glasses",
u"=3":"Laughing, big grin or laugh with glasses",
u"B\^D":"Laughing, big grin or laugh with glasses",
u":-\)\)":"Very happy",
u":‑\(":"Frown, sad, andry or pouting",
u":-\(":"Frown, sad, andry or pouting",
u":\(":"Frown, sad, andry or pouting",
u":‑c":"Frown, sad, andry or pouting",
u":c":"Frown, sad, andry or pouting",
u":‑<":"Frown, sad, andry or pouting",
u":<":"Frown, sad, andry or pouting",
u":‑\[":"Frown, sad, andry or pouting",
u":\[":"Frown, sad, andry or pouting",
u":-\|\|":"Frown, sad, andry or pouting",
u">:\[":"Frown, sad, andry or pouting",
u":\{":"Frown, sad, andry or pouting",
u":@":"Frown, sad, andry or pouting",
u">:\(":"Frown, sad, andry or pouting",
u":'‑\(":"Crying",
u":'\(":"Crying",
u":'‑\)":"Tears of happiness",
u":'\)":"Tears of happiness",
u"D‑':":"Horror",
u"D:<":"Disgust",
u"D:":"Sadness",
u"D8":"Great dismay",
u"D;":"Great dismay",
u"D=":"Great dismay",
u"DX":"Great dismay",
u":‑O":"Surprise",
u":O":"Surprise",
u":‑o":"Surprise",
u":o":"Surprise",
u":-0":"Shock",
u"8‑0":"Yawn",
u">:O":"Yawn",
u":-\*":"Kiss",
u":\*":"Kiss",
u":X":"Kiss",
u";‑\)":"Wink or smirk",
u";\)":"Wink or smirk",
u"\*-\)":"Wink or smirk",
u"\*\)":"Wink or smirk",
u";‑\]":"Wink or smirk",
u";\]":"Wink or smirk",
u";\^\)":"Wink or smirk",
u":‑,":"Wink or smirk",
u";D":"Wink or smirk",
u":‑P":"Tongue sticking out, cheeky, playful or blowing a raspberry",
u":P":"Tongue sticking out, cheeky, playful or blowing a raspberry",
u"X‑P":"Tongue sticking out, cheeky, playful or blowing a raspberry",
u"XP":"Tongue sticking out, cheeky, playful or blowing a raspberry",
u":‑Þ":"Tongue sticking out, cheeky, playful or blowing a raspberry",
u":Þ":"Tongue sticking out, cheeky, playful or blowing a raspberry",
u":b":"Tongue sticking out, cheeky, playful or blowing a raspberry",
u"d:":"Tongue sticking out, cheeky, playful or blowing a raspberry",
u"=p":"Tongue sticking out, cheeky, playful or blowing a raspberry",
u">:P":"Tongue sticking out, cheeky, playful or blowing a raspberry",
u":‑/":"Skeptical, annoyed, undecided, uneasy or hesitant",
u":/":"Skeptical, annoyed, undecided, uneasy or hesitant",
u":-[.]":"Skeptical, annoyed, undecided, uneasy or hesitant",
u">:[(\\\)]":"Skeptical, annoyed, undecided, uneasy or hesitant",
u">:/":"Skeptical, annoyed, undecided, uneasy or hesitant",
u":[(\\\)]":"Skeptical, annoyed, undecided, uneasy or hesitant",
u"=/":"Skeptical, annoyed, undecided, uneasy or hesitant",
u"=[(\\\)]":"Skeptical, annoyed, undecided, uneasy or hesitant",
u":L":"Skeptical, annoyed, undecided, uneasy or hesitant",
u"=L":"Skeptical, annoyed, undecided, uneasy or hesitant",
u":S":"Skeptical, annoyed, undecided, uneasy or hesitant",
u":‑\|":"Straight face",
u":\|":"Straight face",
u":$":"Embarrassed or blushing",
u":‑x":"Sealed lips or wearing braces or tongue-tied",
u":x":"Sealed lips or wearing braces or tongue-tied",
u":‑#":"Sealed lips or wearing braces or tongue-tied",
u":#":"Sealed lips or wearing braces or tongue-tied",
u":‑&":"Sealed lips or wearing braces or tongue-tied",
u":&":"Sealed lips or wearing braces or tongue-tied",
u"O:‑\)":"Angel, saint or innocent",
u"O:\)":"Angel, saint or innocent",
u"0:‑3":"Angel, saint or innocent",
u"0:3":"Angel, saint or innocent",
u"0:‑\)":"Angel, saint or innocent",
u"0:\)":"Angel, saint or innocent",
u":‑b":"Tongue sticking out, cheeky, playful or blowing a raspberry",
u"0;\^\)":"Angel, saint or innocent",
u">:‑\)":"Evil or devilish",
u">:\)":"Evil or devilish",
u"\}:‑\)":"Evil or devilish",
u"\}:\)":"Evil or devilish",
u"3:‑\)":"Evil or devilish",
u"3:\)":"Evil or devilish",
u">;\)":"Evil or devilish",
u"\|;‑\)":"Cool",
u"\|‑O":"Bored",
u":‑J":"Tongue-in-cheek",
u"#‑\)":"Party all night",
u"%‑\)":"Drunk or confused",
u"%\)":"Drunk or confused",
u":-###..":"Being sick",
u":###..":"Being sick",
u"<:‑\|":"Dump",
u"\(>_<\)":"Troubled",
u"\(>_<\)>":"Troubled",
u"\(';'\)":"Baby",
u"\(\^\^>``":"Nervous or Embarrassed or Troubled or Shy or Sweat drop",
u"\(\^_\^;\)":"Nervous or Embarrassed or Troubled or Shy or Sweat drop",
u"\(-_-;\)":"Nervous or Embarrassed or Troubled or Shy or Sweat drop",
u"\(~_~;\) \(・\.・;\)":"Nervous or Embarrassed or Troubled or Shy or Sweat drop",
u"\(-_-\)zzz":"Sleeping",
u"\(\^_-\)":"Wink",
u"\(\(\+_\+\)\)":"Confused",
u"\(\+o\+\)":"Confused",
u"\(o\|o\)":"Ultraman",
u"\^_\^":"Joyful",
u"\(\^_\^\)/":"Joyful",
u"\(\^O\^\)/":"Joyful",
u"\(\^o\^\)/":"Joyful",
u"\(__\)":"Kowtow as a sign of respect, or dogeza for apology",
u"_\(\._\.\)_":"Kowtow as a sign of respect, or dogeza for apology",
u"<\(_ _\)>":"Kowtow as a sign of respect, or dogeza for apology",
u"<m\(__\)m>":"Kowtow as a sign of respect, or dogeza for apology",
u"m\(__\)m":"Kowtow as a sign of respect, or dogeza for apology",
u"m\(_ _\)m":"Kowtow as a sign of respect, or dogeza for apology",
u"\('_'\)":"Sad or Crying",
u"\(/_;\)":"Sad or Crying",
u"\(T_T\) \(;_;\)":"Sad or Crying",
u"\(;_;":"Sad of Crying",
u"\(;_:\)":"Sad or Crying",
u"\(;O;\)":"Sad or Crying",
u"\(:_;\)":"Sad or Crying",
u"\(ToT\)":"Sad or Crying",
u";_;":"Sad or Crying",
u";-;":"Sad or Crying",
u";n;":"Sad or Crying",
u";;":"Sad or Crying",
u"Q\.Q":"Sad or Crying",
u"T\.T":"Sad or Crying",
u"QQ":"Sad or Crying",
u"Q_Q":"Sad or Crying",
u"\(-\.-\)":"Shame",
u"\(-_-\)":"Shame",
u"\(一一\)":"Shame",
u"\(;一_一\)":"Shame",
u"\(=_=\)":"Tired",
u"\(=\^\·\^=\)":"cat",
u"\(=\^\·\·\^=\)":"cat",
u"=_\^= ":"cat",
u"\(\.\.\)":"Looking down",
u"\(\._\.\)":"Looking down",
u"\^m\^":"Giggling with hand covering mouth",
u"\(\・\・?":"Confusion",
u"\(?_?\)":"Confusion",
u">\^_\^<":"Normal Laugh",
u"<\^!\^>":"Normal Laugh",
u"\^/\^":"Normal Laugh",
u"\(\*\^_\^\*)" :"Normal Laugh",
u"\(\^<\^\) \(\^\.\^\)":"Normal Laugh",
u"\(^\^\)":"Normal Laugh",
u"\(\^\.\^\)":"Normal Laugh",
u"\(\^_\^\.\)":"Normal Laugh",
u"\(\^_\^\)":"Normal Laugh",
u"\(\^\^\)":"Normal Laugh",
u"\(\^J\^\)":"Normal Laugh",
u"\(\*\^\.\^\*\)":"Normal Laugh",
u"\(\^—\^\)":"Normal Laugh",
u"\(#\^\.\^#\)":"Normal Laugh",
u"\(\^—\^\)":"Waving",
u"\(;_;\)/~~~":"Waving",
u"\(\^\.\^\)/~~~":"Waving",
u"\(-_-\)/~~~ \($\·\·\)/~~~":"Waving",
u"\(T_T\)/~~~":"Waving",
u"\(ToT\)/~~~":"Waving",
u"\(\*\^0\^\*\)":"Excited",
u"\(\*_\*\)":"Amazed",
u"\(\*_\*;":"Amazed",
u"\(\+_\+\) \(@_@\)":"Amazed",
u"\(\*\^\^\)v":"Laughing,Cheerful",
u"\(\^_\^\)v":"Laughing,Cheerful",
u"\(\(d[-_-]b\)\)":"Headphones,Listening to music",
u'\(-"-\)':"Worried",
u"\(ーー;\)":"Worried",
u"\(\^0_0\^\)":"Eyeglasses",
u"\(\^v\^\)":"Happy",
u"\(\^u\^\)":"Happy",
u"\(\^\)o\(\^\)":"Happy",
u"\(\^O\^\)":"Happy",
u"\(\^o\^\)":"Happy",
u"\)\^o\^\(":"Happy",
u":O o_O":"Surprised",
u"o_0":"Surprised",
u"o\.O":"Surpised",
u"\(o\.o\)":"Surprised",
u"oO":"Surprised",
u"\(\* ̄m ̄\)":"Dissatisfied",
u"\(‘A`\)":"Snubbed or Deflated"
}
# Provided by https://github.com/rishabhverma17/sms_slang_translator/blob/master/slang.txt
text_speech = """
AFAIK=As Far As I Know
AFK=Away From Keyboard
ASAP=As Soon As Possible
ATK=At The Keyboard
ATM=At The Moment
A3=Anytime, Anywhere, Anyplace
BAK=Back At Keyboard
BBL=Be Back Later
BBS=Be Back Soon
BFN=Bye For Now
B4N=Bye For Now
BRB=Be Right Back
BRT=Be Right There
BTW=By The Way
B4=Before
B4N=Bye For Now
CU=See You
CUL8R=See You Later
CYA=See You
FAQ=Frequently Asked Questions
FC=Fingers Crossed
FWIW=For What It's Worth
FYI=For Your Information
GAL=Get A Life
GG=Good Game
GN=Good Night
GMTA=Great Minds Think Alike
GR8=Great!
G9=Genius
IC=I See
ICQ=I Seek you (also a chat program)
ILU=ILU: I Love You
IMHO=In My Honest/Humble Opinion
IMO=In My Opinion
IOW=In Other Words
IRL=In Real Life
KISS=Keep It Simple, Stupid
LDR=Long Distance Relationship
LMAO=Laugh My A.. Off
LOL=Laughing Out Loud
LTNS=Long Time No See
L8R=Later
MTE=My Thoughts Exactly
M8=Mate
NRN=No Reply Necessary
OIC=Oh I See
PITA=Pain In The A..
PRT=Party
PRW=Parents Are Watching
QPSA? Que Pasa?
ROFL=Rolling On The Floor Laughing
ROFLOL=Rolling On The Floor Laughing Out Loud
ROTFLMAO=Rolling On The Floor Laughing My A.. Off
SK8=Skate
STATS=Your sex and age
ASL=Age, Sex, Location
THX=Thank You
TTFN=Ta-Ta For Now!
TTYL=Talk To You Later
U=You
SMH = Shake My Head
U2=You Too
U4E=Yours For Ever
WB=Welcome Back
WTF=What The F...
WTG=Way To Go!
WUF=Where Are You From?
W8=Wait...
7K=Sick:-D Laugher
"""
chat_dict = {}
chat_list = []
for i in text_speech.split("\n"):
if i != "":
tw = i.split("=")[0]
full_text = i.split("=")[-1]
chat_list.append(tw)
chat_dict[tw] = full_text
chat_list = set(chat_list)
def lowercase(doc):
'''
lowercase: takes in a string and lowercases it
Parameters
----------
doc: Python string
Returns
-------
doc: Python strin converted to lowercase
'''
doc = doc.lower()
return doc
def remove_punctuation(doc):
'''
remove_punctuation: takes in a string and
revoes the punctuation via RegEx
Parameters
----------
doc: Python string
Returns
-------
doc: Python string with punctuation removed
'''
return re.sub(r'[^\w\s]','',doc)
def remove_html(doc):
'''
remove_url: takes in a string and removes
html values '<.X?>' using RegEx
Parameters
----------
doc: Python string
Returns
-------
doc: Python string with removed html text
'''
html_pattern = re.compile('<.*?>')
return html_pattern.sub(r'',doc)
def remove_url(doc):
'''
remove_url: takes in a string and removes
url values 'https://, etc.' using RegEx
Parameters
----------
doc: Python string
Returns
-------
doc: Python string with removed url text
'''
url = re.compile(r"https?://\S+|www\.\S+")
return url.sub(r'', doc)
def convert_emoticons(doc):
'''
convert_emoticons: takes in a string and converts
emoticons ':), :(, :-), etc.' with word values.
Parameters
----------
doc: Python string
Returns
-------
doc: Python string with converted emoticons
'''
for i in EMOTICONS:
doc = re.sub(u'('+i+')', "_".join(EMOTICONS[i].replace(",","").split()), doc)
return doc
def convert_emojis(doc):
'''
convert_emojis: takes in a string and converts
emojis '😀, 😂, etc.' with word values.
Parameters
----------
doc: Python string
Returns
-------
doc: Python string with converted emojis
'''
doc = emoji.demojize(doc)
doc = re.sub('[:]', '', doc)
return doc
def convert_text_speech(doc):
'''
convert_text_speech: takes in a string and converts any
abbreviated text speech, 'ttyl, imo, wtf, etc.' and converts
it into its component words
Parameters
----------
doc: Python string
Returns
-------
doc: Python string with converted text speech
'''
text = []
#removes punctuation because that throws off the conversion
doc = doc.translate(str.maketrans('', '', string.punctuation))
for i in doc.split():
if i.upper() in chat_list:
text.append(chat_dict[i.upper()])
else:
text.append(i)
return " ".join(text)
def remove_stopwords(doc):
'''
remove_stopwords: takes in a list of strings removes
those strings that contain stopwords imported from the
nlkt.corpus library
Parameters
----------
doc: Python string
Returns
-------
doc: Python list with stop words removed
'''
doc = [word for word in doc if word not in sw]
return doc
def set_english(df, text_column):
'''
set_english: takes in a string of a text column
outputs the predicted languge of that string to a list
which is used to mask a pandas DataFrame.
Parameters
----------
doc: Python string
Returns
-------
doc: Python list with predicted language
'''
identifier = LanguageIdentifier.from_modelstring(model, norm_probs=True)
lst =[]
for i in df[text_column].values:
lang, score = identifier.classify(i)
lst.append(lang)
return lst
def lemmatize_words(doc):
'''
lemmatize_words: takes in a list of strings and
outputs the lemmatized version of each word,
lemmatizes based on part of speech.
Parameters
----------
doc: Python string
Returns
-------
doc: Python list with lemmatized words
'''
pos_tagged_text = nltk.pos_tag(doc)
doc = [lemmatizer.lemmatize(word, wordnet_map.get(pos[0], wordnet.NOUN)) for word, pos in pos_tagged_text]
return doc
def remove_numbers(doc):
'''
remove_numbers: removes all numbers from a string
Parameters
----------
doc: Python string
Returns
-------
doc: Python list with numbers removed
'''
doc = re.sub(r'\w*\d\w*', '', doc).strip()
return doc
def remove_non_english_characters(doc):
'''
remove_non_english_characters: takes in a list of strings and
removes all non-english characters, including ASCII.
Parameters
----------
doc: Python string
Returns
-------
doc: Python list with non-English characters removed
'''
doc = unicodedata.normalize('NFKD', doc).encode('ASCII', 'ignore').decode('utf8')
return doc
def preprocessing(doc):
'''
preprocessing: takes in a string and applies
text preprocessing to that string.
(For each function definition, refer to above.)
Parameters
----------
doc: Python string
Returns
-------
doc: Python list with preprocessed text
'''
doc = lowercase(doc)
doc = remove_punctuation(doc)
doc = convert_emoticons(doc)
doc = convert_emojis(doc)
doc = remove_url(doc)
doc = remove_html(doc)
doc = convert_text_speech(doc)
doc = remove_numbers(doc)
doc = remove_non_english_characters(doc)
doc = word_tokenize(doc)
doc = remove_stopwords(doc)
doc = lemmatize_words(doc)
return doc