-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
433 lines (416 loc) · 14.3 KB
/
app.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
from flask import Flask, render_template, url_for, request
import pandas as pd
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.model_selection import train_test_split
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from nltk.stem import WordNetLemmatizer
from nltk.tokenize import word_tokenize
import nltk
import re
import string
import pickle
import numpy as np
import gensim
app = Flask(__name__)
stopwords
urlPattern = r"((http://)[^ ]*|(https://)[^ ]*|( www\.)[^ ]*)"
userPattern = '@[^\s]+'
some = 'amp,today,tomorrow,going,girl'
word2vec = gensim.models.Word2Vec.load("word2vec.model")
model = pickle.load(open('sentiment.pkl', 'rb'))
def process_tweets(tweet):
# Lower Casing
tweet = re.sub(r"he's", "he is", tweet)
tweet = re.sub(r"there's", "there is", tweet)
tweet = re.sub(r"We're", "We are", tweet)
tweet = re.sub(r"That's", "That is", tweet)
tweet = re.sub(r"won't", "will not", tweet)
tweet = re.sub(r"they're", "they are", tweet)
tweet = re.sub(r"Can't", "Cannot", tweet)
tweet = re.sub(r"wasn't", "was not", tweet)
tweet = re.sub(r"don\x89Ûªt", "do not", tweet)
tweet = re.sub(r"aren't", "are not", tweet)
tweet = re.sub(r"isn't", "is not", tweet)
tweet = re.sub(r"What's", "What is", tweet)
tweet = re.sub(r"haven't", "have not", tweet)
tweet = re.sub(r"hasn't", "has not", tweet)
tweet = re.sub(r"There's", "There is", tweet)
tweet = re.sub(r"He's", "He is", tweet)
tweet = re.sub(r"It's", "It is", tweet)
tweet = re.sub(r"You're", "You are", tweet)
tweet = re.sub(r"I'M", "I am", tweet)
tweet = re.sub(r"shouldn't", "should not", tweet)
tweet = re.sub(r"wouldn't", "would not", tweet)
tweet = re.sub(r"i'm", "I am", tweet)
tweet = re.sub(r"I\x89Ûªm", "I am", tweet)
tweet = re.sub(r"I'm", "I am", tweet)
tweet = re.sub(r"Isn't", "is not", tweet)
tweet = re.sub(r"Here's", "Here is", tweet)
tweet = re.sub(r"you've", "you have", tweet)
tweet = re.sub(r"you\x89Ûªve", "you have", tweet)
tweet = re.sub(r"we're", "we are", tweet)
tweet = re.sub(r"what's", "what is", tweet)
tweet = re.sub(r"couldn't", "could not", tweet)
tweet = re.sub(r"we've", "we have", tweet)
tweet = re.sub(r"it\x89Ûªs", "it is", tweet)
tweet = re.sub(r"doesn\x89Ûªt", "does not", tweet)
tweet = re.sub(r"It\x89Ûªs", "It is", tweet)
tweet = re.sub(r"Here\x89Ûªs", "Here is", tweet)
tweet = re.sub(r"who's", "who is", tweet)
tweet = re.sub(r"I\x89Ûªve", "I have", tweet)
tweet = re.sub(r"y'all", "you all", tweet)
tweet = re.sub(r"can\x89Ûªt", "cannot", tweet)
tweet = re.sub(r"would've", "would have", tweet)
tweet = re.sub(r"it'll", "it will", tweet)
tweet = re.sub(r"we'll", "we will", tweet)
tweet = re.sub(r"wouldn\x89Ûªt", "would not", tweet)
tweet = re.sub(r"We've", "We have", tweet)
tweet = re.sub(r"he'll", "he will", tweet)
tweet = re.sub(r"Y'all", "You all", tweet)
tweet = re.sub(r"Weren't", "Were not", tweet)
tweet = re.sub(r"Didn't", "Did not", tweet)
tweet = re.sub(r"they'll", "they will", tweet)
tweet = re.sub(r"they'd", "they would", tweet)
tweet = re.sub(r"DON'T", "DO NOT", tweet)
tweet = re.sub(r"That\x89Ûªs", "That is", tweet)
tweet = re.sub(r"they've", "they have", tweet)
tweet = re.sub(r"i'd", "I would", tweet)
tweet = re.sub(r"should've", "should have", tweet)
tweet = re.sub(r"You\x89Ûªre", "You are", tweet)
tweet = re.sub(r"where's", "where is", tweet)
tweet = re.sub(r"Don\x89Ûªt", "Do not", tweet)
tweet = re.sub(r"we'd", "we would", tweet)
tweet = re.sub(r"i'll", "I will", tweet)
tweet = re.sub(r"weren't", "were not", tweet)
tweet = re.sub(r"They're", "They are", tweet)
tweet = re.sub(r"Can\x89Ûªt", "Cannot", tweet)
tweet = re.sub(r"you\x89Ûªll", "you will", tweet)
tweet = re.sub(r"I\x89Ûªd", "I would", tweet)
tweet = re.sub(r"let's", "let us", tweet)
tweet = re.sub(r"it's", "it is", tweet)
tweet = re.sub(r"can't", "cannot", tweet)
tweet = re.sub(r"don't", "do not", tweet)
tweet = re.sub(r"you're", "you are", tweet)
tweet = re.sub(r"i've", "I have", tweet)
tweet = re.sub(r"that's", "that is", tweet)
tweet = re.sub(r"i'll", "I will", tweet)
tweet = re.sub(r"doesn't", "does not", tweet)
tweet = re.sub(r"i'd", "I would", tweet)
tweet = re.sub(r"didn't", "did not", tweet)
tweet = re.sub(r"ain't", "am not", tweet)
tweet = re.sub(r"you'll", "you will", tweet)
tweet = re.sub(r"I've", "I have", tweet)
tweet = re.sub(r"Don't", "do not", tweet)
tweet = re.sub(r"I'll", "I will", tweet)
tweet = re.sub(r"I'd", "I would", tweet)
tweet = re.sub(r"Let's", "Let us", tweet)
tweet = re.sub(r"you'd", "You would", tweet)
tweet = re.sub(r"It's", "It is", tweet)
tweet = re.sub(r"Ain't", "am not", tweet)
tweet = re.sub(r"Haven't", "Have not", tweet)
tweet = re.sub(r"Could've", "Could have", tweet)
tweet = re.sub(r"youve", "you have", tweet)
tweet = re.sub(r"donå«t", "do not", tweet)
tweet = re.sub(r"some1", "someone", tweet)
tweet = re.sub(r"yrs", "years", tweet)
tweet = re.sub(r"hrs", "hours", tweet)
tweet = re.sub(r"2morow|2moro", "tomorrow", tweet)
tweet = re.sub(r"2day", "today", tweet)
tweet = re.sub(r"4got|4gotten", "forget", tweet)
tweet = re.sub(r"b-day|bday", "b-day", tweet)
tweet = re.sub(r"mother's", "mother", tweet)
tweet = re.sub(r"mom's", "mom", tweet)
tweet = re.sub(r"dad's", "dad", tweet)
tweet = re.sub(r"hahah|hahaha|hahahaha", "haha", tweet)
tweet = re.sub(r"lmao|lolz|rofl", "lol", tweet)
tweet = re.sub(r"thanx|thnx", "thanks", tweet)
tweet = re.sub(r"goood", "good", tweet)
tweet = re.sub(r"some1", "someone", tweet)
tweet = re.sub(r"some1", "someone", tweet)
tweet = tweet.lower()
tweet = tweet[1:]
# Removing all URls
tweet = re.sub(urlPattern, '', tweet)
# Removing all @username.
tweet = re.sub(userPattern, '', tweet)
# remove some words
tweet = re.sub(some, '', tweet)
# Remove punctuations
tweet = tweet.translate(str.maketrans("", "", string.punctuation))
# tokenizing words
tokens = word_tokenize(tweet)
# tokens = [w for w in tokens if len(w)>2]
# Removing Stop Words
final_tokens = [w for w in tokens if w not in stopword]
# reducing a word to its word stem
wordLemm = WordNetLemmatizer()
finalwords = []
for w in final_tokens:
if len(w) > 1:
word = wordLemm.lemmatize(w)
finalwords.append(word)
return ' '.join(finalwords)
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",
"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"
}
stopword = []
def convert_abbrev_in_text(tweet):
t = []
words = tweet.split()
t = [abbreviations[w.lower()] if w.lower() in abbreviations.keys()
else w for w in words]
return ' '.join(t)
def vectorize(x):
# Load Word2vec pkl file
vdoc = [word2vec.wv[word] for word in x]
doc2vec = np.sum(vdoc, axis=0)
if doc2vec.size < 300:
doc2vec = np.zeros(300)
return doc2vec
@app.route('/')
def home():
nltk.download('punkt')
nltk.download('wordnet')
nltk.download('stopwords')
stopword = set(stopwords.words('english'))
return render_template('index.html')
@app.route('/predict', methods=['POST'])
def predict():
if request.method == 'POST':
comment = request.form['comment']
comment = process_tweets(comment)
comment = convert_abbrev_in_text(comment)
vec = vectorize(comment.split())
data = [process_tweets(comment)]
my_prediction = model.predict(vec.reshape(1, -1))
return render_template('result.html', prediction=my_prediction)
if __name__ == '__main__':
app.run(debug=True)