-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclean_tweets.py
402 lines (365 loc) · 12.1 KB
/
clean_tweets.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
import csv, re, string
import nltk
from nltk.corpus import stopwords
nltk.download('stopwords', quiet=True)
stopwords = stopwords.words('english')
abbreviations = {
"$" : " dollar ",
"€" : " euro ",
"4ao" : "for adults only",
"a.m" : "before midday",
"a3" : "anytime anywhere anyplace",
"aamof" : "as a matter of fact",
"acct" : "account",
"adih" : "another day in hell",
"afaic" : "as far as i am concerned",
"afaict" : "as far as i can tell",
"afaik" : "as far as i know",
"afair" : "as far as i remember",
"afk" : "away from keyboard",
"app" : "application",
"approx" : "approximately",
"apps" : "applications",
"asap" : "as soon as possible",
"asl" : "age, sex, location",
"atk" : "at the keyboard",
"ave." : "avenue",
"aymm" : "are you my mother",
"ayor" : "at your own risk",
"b&b" : "bed and breakfast",
"b+b" : "bed and breakfast",
"b.c" : "before christ",
"b2b" : "business to business",
"b2c" : "business to customer",
"b4" : "before",
"b4n" : "bye for now",
"b@u" : "back at you",
"bae" : "before anyone else",
"bak" : "back at keyboard",
"bbbg" : "bye bye be good",
"bbc" : "british broadcasting corporation",
"bbias" : "be back in a second",
"bbl" : "be back later",
"bbs" : "be back soon",
"be4" : "before",
"bfn" : "bye for now",
"blvd" : "boulevard",
"bout" : "about",
"brb" : "be right back",
"bros" : "brothers",
"brt" : "be right there",
"bsaaw" : "big smile and a wink",
"btw" : "by the way",
"bwl" : "bursting with laughter",
"c/o" : "care of",
"cet" : "central european time",
"cf" : "compare",
"cia" : "central intelligence agency",
"csl" : "can not stop laughing",
"cu" : "see you",
"cul8r" : "see you later",
"cv" : "curriculum vitae",
"cwot" : "complete waste of time",
"cya" : "see you",
"cyt" : "see you tomorrow",
"dae" : "does anyone else",
"dbmib" : "do not bother me i am busy",
"diy" : "do it yourself",
"dm" : "direct message",
"dwh" : "during work hours",
"e123" : "easy as one two three",
"eet" : "eastern european time",
"eg" : "example",
"embm" : "early morning business meeting",
"encl" : "enclosed",
"encl." : "enclosed",
"etc" : "and so on",
"faq" : "frequently asked questions",
"fawc" : "for anyone who cares",
"fb" : "facebook",
"fc" : "fingers crossed",
"fig" : "figure",
"fimh" : "forever in my heart",
"ft." : "feet",
"ft" : "featuring",
"ftl" : "for the loss",
"ftw" : "for the win",
"fwiw" : "for what it is worth",
"fyi" : "for your information",
"g9" : "genius",
"gahoy" : "get a hold of yourself",
"gal" : "get a life",
"gcse" : "general certificate of secondary education",
"gfn" : "gone for now",
"gg" : "good game",
"gl" : "good luck",
"glhf" : "good luck have fun",
"gmt" : "greenwich mean time",
"gmta" : "great minds think alike",
"gn" : "good night",
"g.o.a.t" : "greatest of all time",
"goat" : "greatest of all time",
"goi" : "get over it",
"gps" : "global positioning system",
"gr8" : "great",
"gratz" : "congratulations",
"gyal" : "girl",
"h&c" : "hot and cold",
"hp" : "horsepower",
"hr" : "hour",
"hrh" : "his royal highness",
"ht" : "height",
"ibrb" : "i will be right back",
"ic" : "i see",
"icq" : "i seek you",
"icymi" : "in case you missed it",
"idc" : "i do not care",
"idgadf" : "i do not give a damn fuck",
"idgaf" : "i do not give a fuck",
"idk" : "i do not know",
"ie" : "that is",
"i.e" : "that is",
"ifyp" : "i feel your pain",
"IG" : "instagram",
"iirc" : "if i remember correctly",
"ilu" : "i love you",
"ily" : "i love you",
"imho" : "in my humble opinion",
"imo" : "in my opinion",
"imu" : "i miss you",
"iow" : "in other words",
"irl" : "in real life",
"j4f" : "just for fun",
"jic" : "just in case",
"jk" : "just kidding",
"jsyk" : "just so you know",
"l8r" : "later",
"lb" : "pound",
"lbs" : "pounds",
"ldr" : "long distance relationship",
"lmao" : "laugh my ass off",
"lmfao" : "laugh my fucking ass off",
"lol" : "laughing out loud",
"ltd" : "limited",
"ltns" : "long time no see",
"m8" : "mate",
"mf" : "motherfucker",
"mfs" : "motherfuckers",
"mfw" : "my face when",
"mofo" : "motherfucker",
"mph" : "miles per hour",
"mr" : "mister",
"mrw" : "my reaction when",
"ms" : "miss",
"mte" : "my thoughts exactly",
"nagi" : "not a good idea",
"nbc" : "national broadcasting company",
"nbd" : "not big deal",
"nfs" : "not for sale",
"ngl" : "not going to lie",
"nhs" : "national health service",
"nrn" : "no reply necessary",
"nsfl" : "not safe for life",
"nsfw" : "not safe for work",
"nth" : "nice to have",
"nvr" : "never",
"nyc" : "new york city",
"oc" : "original content",
"og" : "original",
"ohp" : "overhead projector",
"oic" : "oh i see",
"omdb" : "over my dead body",
"omg" : "oh my god",
"omw" : "on my way",
"p.a" : "per annum",
"p.m" : "after midday",
"pm" : "prime minister",
"poc" : "people of color",
"pov" : "point of view",
"pp" : "pages",
"ppl" : "people",
"prw" : "parents are watching",
"ps" : "postscript",
"pt" : "point",
"ptb" : "please text back",
"pto" : "please turn over",
"qpsa" : "what happens", #"que pasa",
"ratchet" : "rude",
"rbtl" : "read between the lines",
"rlrt" : "real life retweet",
"rofl" : "rolling on the floor laughing",
"roflol" : "rolling on the floor laughing out loud",
"rotflmao" : "rolling on the floor laughing my ass off",
"rt" : "retweet",
"ruok" : "are you ok",
"sfw" : "safe for work",
"sk8" : "skate",
"smh" : "shake my head",
"sq" : "square",
"srsly" : "seriously",
"ssdd" : "same stuff different day",
"tbh" : "to be honest",
"tbs" : "tablespooful",
"tbsp" : "tablespooful",
"tfw" : "that feeling when",
"thks" : "thank you",
"tho" : "though",
"thx" : "thank you",
"tia" : "thanks in advance",
"til" : "today i learned",
"tl;dr" : "too long i did not read",
"tldr" : "too long i did not read",
"tmb" : "tweet me back",
"tntl" : "trying not to laugh",
"ttyl" : "talk to you later",
"u" : "you",
"u2" : "you too",
"u4e" : "yours for ever",
"utc" : "coordinated universal time",
"w/" : "with",
"w/o" : "without",
"w8" : "wait",
"wassup" : "what is up",
"wb" : "welcome back",
"wtf" : "what the fuck",
"wtg" : "way to go",
"wtpa" : "where the party at",
"wuf" : "where are you from",
"wuzup" : "what is up",
"wywh" : "wish you were here",
"yd" : "yard",
"ygtr" : "you got that right",
"ynk" : "you never know",
"zzz" : "sleeping bored and tired"
}
# Remove all URLs, replace by URL
def remove_URL(text):
url = re.compile(r'https?://\S+|www\.\S+')
return url.sub(r'URL',text)
# Remove HTML beacon
def remove_HTML(text):
html=re.compile(r'<.*?>')
return html.sub(r'',text)
# Remove non printable characters
def remove_not_ASCII(text):
text = ''.join([word for word in text if word in string.printable])
return text
# Change an abbreviation by its true meaning
def word_abbrev(word):
return abbreviations[word.lower()] if word.lower() in abbreviations.keys() else word
# Replace all abbreviations
def replace_abbrev(text):
string = ""
for word in text.split():
string += word_abbrev(word) + " "
return string
# Remove @ and mention, replace by USER
def remove_mention(text):
at=re.compile(r'@\S+')
return at.sub(r'USER',text)
# Remove numbers, replace it by NUMBER
def remove_number(text):
num = re.compile(r'[-+]?[.\d]*[\d]+[:,.\d]*')
return num.sub(r'NUMBER', text)
# Remove all emojis, replace by EMOJI
def remove_emoji(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"\U00002702-\U000027B0"
u"\U000024C2-\U0001F251"
"]+", flags=re.UNICODE)
return emoji_pattern.sub(r'EMOJI', text)
# Replace some others smileys with SADFACE
def transcription_sad(text):
eyes = "[8:=;]"
nose = "['`\-]"
smiley = re.compile(r'[8:=;][\'\-]?[(\\/]')
return smiley.sub(r'SADFACE', text)
# Replace some smileys with SMILE
def transcription_smile(text):
eyes = "[8:=;]"
nose = "['`\-]"
smiley = re.compile(r'[8:=;][\'\-]?[)dDp]')
#smiley = re.compile(r'#{eyes}#{nose}[)d]+|[)d]+#{nose}#{eyes}/i')
return smiley.sub(r'SMILE', text)
# Replace <3 with HEART
def transcription_heart(text):
heart = re.compile(r'<3')
return heart.sub(r'HEART', text)
# Factorize elongated words, add ELONG
def remove_elongated_words(text):
rep = re.compile(r'\b(\S*?)([a-z])\2{2,}\b')
return rep.sub(r'\1\2 ELONG', text)
# Factorize repeated punctuation, add REPEAT
def remove_repeat_punct(text):
rep = re.compile(r'([!?.]){2,}')
return rep.sub(r'\1 REPEAT', text)
# Remove all punctuations
def remove_all_punct(text):
table = str.maketrans('','',string.punctuation)
return text.translate(table)
# Remove punctuations
def remove_punct(text):
punctuations = '@#!?+&*[]-%.:/();$=><|{}^' + "'`"
for p in punctuations:
text = text.replace(p, f' {p} ')
text = text.replace('...', ' ... ')
if '...' not in text:
text = text.replace('..', ' ... ')
return text
# Remove all english stopwords
def remove_stopwords(text):
text = ' '.join([word for word in text.split() if word not in (stopwords)])
return text
def clean_tweet(text):
# Remove non text
text = remove_URL(text)
text = remove_HTML(text)
text = remove_not_ASCII(text)
# Lower text, replace abbreviations
text = text.lower()
text = replace_abbrev(text)
text = remove_mention(text)
text = remove_number(text)
# Remove emojis / smileys
text = remove_emoji(text)
text = transcription_sad(text)
text = transcription_smile(text)
text = transcription_heart(text)
# Remove repeated puntuations / words
text = remove_elongated_words(text)
text = remove_repeat_punct(text)
#text = remove_all_punct(text)
#text = remove_punct(text)
#text = remove_stopwords(text)
return text
fr = open("test/AutoNLPUI/kaggle-competition/assets/train.csv") # our train data
csv_read = csv.reader(fr)
next(csv_read) # skipping the header row
fw = open("test/AutoNLPUI/kaggle-competition/assets/train_clean.csv", "w", encoding="UTF8") # train data cleaned
csv_write = csv.writer(fw)
csv_write.writerow(['id','keyword','location','text','target']) # writing the header row
for row in csv_read:
write_row = [row[0],row[1],row[2],clean_tweet(row[3]), row[4]]
csv_write.writerow(write_row)
fr = open("test/AutoNLPUI/kaggle-competition/assets/validation.csv") # our validation data
csv_read = csv.reader(fr)
next(csv_read) # skipping the header row
fw = open("test/AutoNLPUI/kaggle-competition/assets/validation_clean.csv", "w", encoding="UTF8") # validation data cleaned
csv_write = csv.writer(fw)
csv_write.writerow(['id','keyword','location','text','target']) # writing the header row
for row in csv_read:
write_row = [row[0],row[1],row[2],clean_tweet(row[3]), row[4]]
csv_write.writerow(write_row)
fr = open("test/AutoNLPUI/kaggle-competition/assets/test.csv") # our test data
csv_read = csv.reader(fr)
next(csv_read) # skipping the header row
fw = open("test/AutoNLPUI/kaggle-competition/assets/test_clean.csv", "w", encoding="UTF8") # test data cleaned
csv_write = csv.writer(fw)
csv_write.writerow(['id','keyword','location','text','target']) # writing the header row
for row in csv_read:
write_row = [row[0],row[1],row[2],clean_tweet(row[3])]
csv_write.writerow(write_row)