-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2-CreatingSentiment-jupyter.py
More file actions
570 lines (354 loc) · 12.7 KB
/
Copy path2-CreatingSentiment-jupyter.py
File metadata and controls
570 lines (354 loc) · 12.7 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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
# Databricks notebook source
# MAGIC %md
# MAGIC
# MAGIC # Sentiment Analysis: TextBlob Vs VADER Vs Flair
# MAGIC
# MAGIC By Ameenah Al-Haidari
# COMMAND ----------
# MAGIC %md
# MAGIC This notebook will go over the Python implementation of TextBlob, VADER, and Flair for non-model sentiment analysis.
# COMMAND ----------
# MAGIC %md
# MAGIC ### Step 1: Install And Import Python Libraries
# COMMAND ----------
# MAGIC %md
# MAGIC For the sentiment analysis, we need to import TextBlob, SentimentIntensityAnalyzer from vaderSentiment, and TextClassifier from flair. We also need to load the English sentiment data from TextClassifier and import Sentence for text processing for the flair pre-trained model.
# MAGIC
# MAGIC To check the sentiment prediction accuracy, we need to import accuracy_score from sklearn.
# MAGIC
# MAGIC Last but not least, we set the pandas dataframe column width to be 1000, which will allow us to see more content from the review.
# COMMAND ----------
from datetime import date
#import snscrape.modules.twitter as sntwitter
import pandas as pd
import numpy as np
from textblob import TextBlob
from wordcloud import WordCloud
import re
import matplotlib.pyplot as plt
import seaborn as sns
plt.style.use('fivethirtyeight')
import nltk
from nltk.stem.snowball import SnowballStemmer
from nltk.corpus import stopwords
import spacy
nlp = spacy.load("en_core_web_sm")
# COMMAND ----------
# Data processing
import pandas as pd
import numpy as np
# Import TextBlob
from textblob import TextBlob
# Import VADER sentiment
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
# Import flair pre-trained sentiment model
from flair.models import TextClassifier
classifier = TextClassifier.load('en-sentiment')
# Import flair Sentence to process input text
from flair.data import Sentence
# Import accuracy_score to check performance
from sklearn.metrics import accuracy_score
# Set a wider colwith
pd.set_option('display.max_colwidth', 1000)
# COMMAND ----------
import nltk
import pandas as pd
nltk.download('vader_lexicon')
# COMMAND ----------
# MAGIC %md
# MAGIC # VADER
# COMMAND ----------
from nltk.sentiment.vader import SentimentIntensityAnalyzer
sid = SentimentIntensityAnalyzer()
# COMMAND ----------
df = pd.read_csv('tweets_cleaned.csv')
df
# COMMAND ----------
df.info()
# COMMAND ----------
df.dropna(inplace=True)
# COMMAND ----------
df.iloc[0]['tweet']
# COMMAND ----------
sid.polarity_scores(df.iloc[0]['tweet'])
# COMMAND ----------
df['tweet'].apply(lambda tweet: sid.polarity_scores(tweet))
# COMMAND ----------
df['scores_V'] = df['tweet'].apply(lambda tweet: sid.polarity_scores(tweet))
# COMMAND ----------
df.head()
# COMMAND ----------
df['compound'] = df['scores_V'].apply(lambda d: d['compound'])
# COMMAND ----------
df.head()
# COMMAND ----------
df['comp_vader'] = df['compound'].apply(lambda score: 'POSITIVE' if score >0 else ('NEUTRAL' if score ==0 else 'NEGATIVE'))
# COMMAND ----------
df.head()
# COMMAND ----------
df['comp_vader'].value_counts()
# COMMAND ----------
#plot a bar graph to show count of tweet sentiment
fig = plt.figure(figsize=(7,5))
color = ['green', 'grey', 'red']
df['comp_vader'].value_counts().plot(kind='bar',color = color)
plt.title('Value count of tweet polarity')
plt.ylabel('Count')
plt.xlabel('Polarity')
plt.grid(False)
plt.show()
# COMMAND ----------
#pie chart to show percentage distribution of polarity
fig = plt.figure(figsize=(7,7))
colors = ('green', 'grey', 'red')
wp={'linewidth':2, 'edgecolor': 'black'}
tags=df['comp_vader'].value_counts()
explode = (0.1,0.1,0.1)
tags.plot(kind='pie', autopct='%1.1f%%', shadow=True, colors=colors,
startangle=90, wedgeprops=wp, explode=explode, label='')
plt.title('Distribution of polarity')
# COMMAND ----------
# COMMAND ----------
# MAGIC %md
# MAGIC # TextBlob
# COMMAND ----------
#get subjectivity and polarity of tweets with a function
def getSubjectivity(text):
return TextBlob(text).sentiment.subjectivity
#get polarity with a function
def getPolarity(text):
return TextBlob(text).sentiment.polarity
df['Subjectivity'] = df['tweet'].apply(getSubjectivity)
df['Polarity'] = df['tweet'].apply(getPolarity)
# COMMAND ----------
from textblob import TextBlob
#create a function to check negative, neutral and positive analysis
def getAnalysis(score):
if score<0:
return 'NEGATIVE'
elif score ==0:
return 'NEUTRAL'
else:
return 'POSITIVE'
# COMMAND ----------
df['Analysis_TB'] = df['Polarity'].apply(getAnalysis)
# COMMAND ----------
df.head()
# COMMAND ----------
df['Analysis_TB'].value_counts()
# COMMAND ----------
df['comp_vader'].value_counts()
# COMMAND ----------
#pie chart to show percentage distribution of polarity
fig = plt.figure(figsize=(7,7))
colors = ('green', 'grey', 'red')
wp={'linewidth':2, 'edgecolor': 'black'}
tags=df['Analysis_TB'].value_counts()
explode = (0.1,0.1,0.1)
tags.plot(kind='pie', autopct='%1.1f%%', shadow=True, colors=colors,
startangle=90, wedgeprops=wp, explode=explode, label='')
plt.title('Distribution of polarity')
# COMMAND ----------
# COMMAND ----------
# MAGIC %md
# MAGIC # FLAIR
# COMMAND ----------
# Define a function to get Flair sentiment prediction score
def score_flair(text):
sentence = Sentence(text)
classifier.predict(sentence)
score = sentence.labels[0].score
value = sentence.labels[0].value
return score, value
# COMMAND ----------
# Get sentiment score for each review
df['scores_flair'] = df['tweet'].apply(lambda s: score_flair(s)[0])
# Predict sentiment label for each review
df['pred_flair'] = df['tweet'].apply(lambda s: score_flair(s)[1])
# Check the distribution of the score
#df['scores_flair'].describe()
# COMMAND ----------
# Check the distribution of the score
df['scores_flair'].describe()
# COMMAND ----------
df['pred_flair'].value_counts()
# COMMAND ----------
df.head(50)
# COMMAND ----------
#pie chart to show percentage distribution of polarity
fig = plt.figure(figsize=(7,7))
colors = ('green', 'red')
wp={'linewidth':2, 'edgecolor': 'black'}
tags=df['pred_flair'].value_counts()
explode = (0.1,0.1)
tags.plot(kind='pie', autopct='%1.1f%%', shadow=True, colors=colors,
startangle=90, wedgeprops=wp, explode=explode, label='')
plt.title('Distribution of polarity')
# COMMAND ----------
# MAGIC %md
# MAGIC ### Let's compare the Negative situation
# COMMAND ----------
# record 2
df.iloc[2]['tweet']
# COMMAND ----------
# MAGIC %md
# MAGIC This expresion, it looks like hidden negative feeling.
# MAGIC - Flair gets the meaning (Negative) whereas both Vader and TextBlob get Postive meaning. Both of them fail to guess the meaning.
# COMMAND ----------
# record 3
df.iloc[3]['tweet']
# COMMAND ----------
# MAGIC %md
# MAGIC This expresion, it seems taylor was blocked from twitter and Musk has reinstated her account.
# MAGIC The feeling is positive but what about who blocked Taylor and the story behind that.
# MAGIC - Flair gets the meaning (Negative) whereas both Vader and TextBlob get Neutral meaning. Both of them guess the meaning better than Flair. But what about if Elon Musk who gave the order to block Taylor?
# COMMAND ----------
# record 7
df.iloc[7]['tweet']
# COMMAND ----------
# MAGIC %md
# MAGIC This expresion, it seems Negative
# MAGIC - Flair and Vader get the meaning (Negative) whereas TextBlob get Neutral meaning. TextBlob fails to guess the meaning.
# COMMAND ----------
# record 11
df.iloc[11]['tweet']
# COMMAND ----------
# MAGIC %md
# MAGIC This expresion, it seems Negative against Elon Musk and Trumps
# MAGIC - Flair gets the meaning (Negative) whereas both Vader and TextBlob get Positive. Both of them fail to guess the meaning.
# COMMAND ----------
# record 12
df.iloc[12]['tweet']
# COMMAND ----------
# MAGIC %md
# MAGIC This expresion, it seems Negative against Elon Musk.
# MAGIC - Vader gets the meaning (Negative) whereas both Flir gets Positive and TextBlob get Neutral. Both of them fail to guess the meaning.
# COMMAND ----------
# record 20
df.iloc[20]['tweet']
# COMMAND ----------
# MAGIC %md
# MAGIC This expresion, it seems Negative against Elon Musk. It is a kind of negative Question d'exclamation!?
# MAGIC - Flair gets the meaning (Negative) whereas both Vader gets Neutral and TextBlob get Positive. Both of them fail to guess the meaning.
# COMMAND ----------
# record 25
df.iloc[25]['tweet']
# COMMAND ----------
# MAGIC %md
# MAGIC This expresion, it seems Neutral
# MAGIC - TextBlob gets the meaning (Neutral) whereas both Flir gets Negative and Vader get Positive. Both of them fail to guess the meaning.
# COMMAND ----------
# record 28
df.iloc[28]['tweet']
# COMMAND ----------
# MAGIC %md
# MAGIC This expresion, it seems Negative against Elon Musk.
# MAGIC - Flair gets the meaning (Negative) whereas both Vader and TextBlob get Positive. Both of them fail to guess the meaning.
# COMMAND ----------
# MAGIC %md
# MAGIC ### Result
# MAGIC - Flair is more sentiment analysing emotion and opinion than Vader and TextBlod.
# MAGIC
# COMMAND ----------
# COMMAND ----------
df.columns
# COMMAND ----------
# COMMAND ----------
df_VTBF = df.drop(['tweet', 'scores_V', 'compound', 'Subjectivity', 'Polarity', 'scores_flair'], axis=1)
df_VTBF
# COMMAND ----------
df_VTBF.head()
# COMMAND ----------
ElonMusk_Sentiment = df.drop(['scores_V', 'compound', 'Subjectivity', 'Polarity', 'scores_flair', 'comp_vader', 'Analysis_TB'], axis=1)
ElonMusk_Sentiment
# COMMAND ----------
# MAGIC %md
# MAGIC we need to map the ‘NEGATIVE’ value to 0 and the ‘POSITIVE’ value to 1
# COMMAND ----------
# Change the label of flair prediction to 0 if negative and 1 if positive
mapping = {'NEGATIVE': 0, 'POSITIVE': 1}
ElonMusk_Sentiment['pred_flair'] = ElonMusk_Sentiment['pred_flair'].map(mapping)
# COMMAND ----------
ElonMusk_Sentiment
# COMMAND ----------
ElonMusk_Sentiment = ElonMusk_Sentiment.rename(columns={'pred_flair':'Sentiment'})
# COMMAND ----------
ElonMusk_Sentiment
# COMMAND ----------
ElonMusk_Sentiment.info()
# COMMAND ----------
# MAGIC %md
# MAGIC ### Creating a word cloud for the tweets
# MAGIC
# MAGIC To understand which words have been used most in the tweets, we create a word cloud function for both positive and negative tweets.
# COMMAND ----------
#create a function for wordcloud
def create_wordcloud(text):
allWords = ' '.join([tweets for tweets in text])
wordCloud = WordCloud(background_color='white', width=800, height=500, random_state=21, max_font_size=130).generate(allWords)
plt.figure(figsize=(20,10))
plt.imshow(wordCloud)
plt.axis('off')
plt.show()
# COMMAND ----------
#wordcloud for positive tweets
posTweets = df.loc[df['pred_flair']=='POSITIVE', 'tweet']
create_wordcloud(posTweets)
# COMMAND ----------
#wordcloud for negative tweets
negTweets = df.loc[df['pred_flair']=='NEGATIVE', 'tweet']
create_wordcloud(negTweets)
# COMMAND ----------
# COMMAND ----------
# MAGIC %md
# MAGIC ### Finding the most popular words in tweets and their frequency
# MAGIC
# MAGIC Here, every tweet is broken down into words and analyzed
# COMMAND ----------
#break each tweet sentence into words
sentences = []
for word in df['tweet']:
sentences.append(word)
sentences
lines = list()
for line in sentences:
words = line.split()
for w in words:
lines.append(w)
lines[:20] #shows first 10 words in the first tweet
# COMMAND ----------
# MAGIC %md
# MAGIC Next, we remove stop words which are the common words used in the English Language such as ‘on’, ‘the’, ‘is’ etc. We then group the rest together to their root words eg joined, joining, and joint are grouped together as a single word — join and save it to a new data frame df.
# COMMAND ----------
#stemming all the words to their root word
stemmer = SnowballStemmer(language='english')
stem=[]
for word in lines:
stem.append(stemmer.stem(word))
stem[:20]
#removes stopwords (very common words in a sentence)
stem2 = []
for word in stem:
if word not in nlp.Defaults.stop_words:
stem2.append(word)
#creates a new dataframe for the stem and shows the count of the most used words
df = pd.DataFrame(stem2)
df=df[0].value_counts()
df #shows the new dataframe
# COMMAND ----------
df.head(10)
# COMMAND ----------
# COMMAND ----------
# MAGIC %md
# MAGIC ### Finally, we plot the most used words.
# COMMAND ----------
df.head(20).plot(kind='bar',title='Top Words', color = ['blue','red','green','yellow','orange'])
plt.xlabel('Count of Words')
plt.ylabel('Words from Twitter');
# COMMAND ----------
# COMMAND ----------
# COMMAND ----------
ElonMusk_Sentiment.to_csv('ElonMusk_Sentiment.csv', index=False)
# COMMAND ----------
pd.read_csv('ElonMusk_Sentiment.csv')
# COMMAND ----------