1+ import csv
2+ import random
3+ import re
4+ import codecs #provides transparent encoding/decoding
5+ from textblob import TextBlob #Library for Text Processing
6+ import time
7+ from collections import Counter
8+
9+ #Plotting dependecies
10+ import matplotlib .pyplot as plt ; plt .rcdefaults ()
11+ import numpy as np
12+ import matplotlib .pyplot as plt
13+
14+ #Sentiment values
15+ positive = 0
16+ negative = 0
17+ neutral = 0
18+ total = 0
19+
20+ hashtags = []
21+
22+ #Loading.....
23+ print ("Performing Sentiment Analysis" ,end = "" )
24+ for i in range (5 ):
25+ print ("." ,end = "" )
26+ time .sleep (1 )
27+
28+
29+ # reading csv file
30+
31+ filepath = "demonetization-tweets.csv"
32+ with codecs .open (filepath , "r" ,encoding = 'utf-8' , errors = 'ignore' ) as csvfile :
33+ reader = csv .reader (csvfile )
34+ tweetsList = []
35+ cleanTweetsList = []
36+ for row in reader :
37+ tweet = row [2 ].strip () #contains tweet
38+ cleanTweet = " " .join (re .findall ("[a-zA-Z]+" ,tweet ))
39+ analysis = TextBlob (cleanTweet )
40+
41+ #appending tweet to list
42+ tweetsList .append (tweet )
43+ cleanTweetsList .append (cleanTweet )
44+
45+
46+ #Assigning polarity and calculating count
47+ total = total + 1
48+ if (analysis .sentiment .polarity > 0 ):
49+ positive = positive + 1
50+ if (analysis .sentiment .polarity == 0 ):
51+ neutral = neutral + 1
52+ else :
53+ negative = negative + 1
54+
55+
56+ #Result
57+ print () #newline
58+ print ("Total Tweets: " ,total )
59+ print ('Positive = ' ,positive )
60+ print ('Neutral= ' ,neutral )
61+ print ('Negative= ' ,negative )
62+
63+ #Random number generator to pick tweets
64+ randomTweets = []
65+ randomCleanTweets = []
66+
67+ randomNumber = random .sample (range (1 , 6000 ), 5 )
68+ index = 0
69+
70+ #Collecting random tweets
71+ for i in range (5 ):
72+ number = random .randint (1 ,6000 ) #Picks a random number between 1-6000
73+ randomTweets .append (tweetsList [randomNumber [index ]]) #Stores a random tweet from data without repeatition
74+ randomCleanTweets .append (tweetsList [randomNumber [index ]])
75+ index = index + 1
76+
77+ for tweet in randomCleanTweets :
78+ print ()
79+ print (tweet ,end = ' ' )
80+ analysis = TextBlob (tweet )
81+ print (" => " ,analysis .sentiment )
82+
83+
84+ #Writing random tweets to a text file for display
85+
86+ with open ('tweets.txt' , 'w' ) as file :
87+ for tweet in randomTweets :
88+ file .write (tweet )
89+ file .write ('<br><br>\n ' )
90+
91+ #Finding the hashtags in all the tweets
92+ finalcount = {}
93+ for i in tweetsList :
94+ hashtags .append (re .findall (r"#(\w+)" , i ))
95+ hashtagnew = [item for sub in hashtags for item in sub ]
96+ counts = Counter (hashtagnew )
97+ counts = dict (counts )
98+ finalcount = dict (sorted (counts .items (), key = lambda kv : kv [1 ], reverse = True ))
99+ countname = list (finalcount .keys ())
100+
101+ #Plotting data
102+
103+
104+ #Bar Graph
105+ objects = ('Positive' ,'Neutral' ,'Negative' )
106+ y_pos = np .arange (len (objects ))
107+ performance = [positive ,neutral ,negative ]
108+
109+ plt .bar (y_pos , performance , align = 'center' , alpha = 0.5 )
110+ plt .xticks (y_pos , objects )
111+ plt .ylabel ('# of tweets' )
112+ plt .title ('Twitter Sentiment Analysis- Demonetisation (Bar Graph) \n ' )
113+ plt .show ()
114+
115+
116+ #Pie Graph
117+
118+ colors = ['yellowgreen' , 'gold' , 'orangered' ]
119+ explode = (0 , 0 , 0.1 ) # explode last slice
120+
121+ plt .pie (performance , explode = explode , labels = objects , colors = colors ,
122+ autopct = '%1.1f%%' , shadow = False , startangle = 140 )
123+
124+ plt .axis ('equal' )
125+ plt .title ('Twitter Sentiment Analysis- Demonetisation (Pie Chart) \n ' )
126+ plt .show ()
127+
128+ # Hashtag Plot
129+ x = np .arange (len (finalcount ))
130+ y = list (finalcount .values ())
131+ x = x [:15 ]
132+ y = y [:15 ]
133+ countname = countname [:15 ]
134+ plt .bar (x , y )
135+ plt .title ('Most Trending Hashtags\n ' )
136+ plt .xticks (x , countname , rotation = 'vertical' )
137+ plt .ylabel ('Number of tweets' )
138+ plt .xlabel ('#Hashtags' )
139+ plt .tight_layout ()
140+ plt .show ()
0 commit comments