1
1
import sys ,tweepy ,csv ,re
2
2
from textblob import TextBlob
3
+ import matplotlib .pyplot as plt
4
+
3
5
4
6
class SentimentAnalysis :
5
7
@@ -113,6 +115,7 @@ def DownloadData(self):
113
115
print (str (snegative ) + "% people thought it was strongly negative" )
114
116
print (str (neutral ) + "% people thought it was neutral" )
115
117
118
+ self .plotPieChart (positive , wpositive , spositive , negative , wnegative , snegative , neutral , searchTerm , NoOfTerms )
116
119
117
120
118
121
def cleanTweet (self , tweet ):
@@ -121,12 +124,23 @@ def cleanTweet(self, tweet):
121
124
122
125
# function to calculate percentage
123
126
def percentage (self , part , whole ):
124
- return 100 * float (part ) / float (whole )
127
+ temp = 100 * float (part ) / float (whole )
128
+ return format (temp , '.2f' )
125
129
130
+ def plotPieChart (self , positive , wpositive , spositive , negative , wnegative , snegative , neutral , searchTerm , noOfSearchTerms ):
131
+ labels = ['Positive [' + str (positive ) + '%]' , 'Weakly Positive [' + str (wpositive ) + '%]' ,'Strongly Positive [' + str (spositive ) + '%]' , 'Neutral [' + str (neutral ) + '%]' ,
132
+ 'Negative [' + str (negative ) + '%]' , 'Weakly Negative [' + str (wnegative ) + '%]' , 'Strongly Negative [' + str (snegative ) + '%]' ]
133
+ sizes = [positive , wpositive , spositive , neutral , negative , wnegative , snegative ]
134
+ colors = ['yellowgreen' ,'lightgreen' ,'darkgreen' , 'gold' , 'red' ,'lightsalmon' ,'darkred' ]
135
+ patches , texts = plt .pie (sizes , colors = colors , startangle = 90 )
136
+ plt .legend (patches , labels , loc = "best" )
137
+ plt .title ('How people are reacting on ' + searchTerm + ' by analyzing ' + str (noOfSearchTerms ) + ' Tweets.' )
138
+ plt .axis ('equal' )
139
+ plt .tight_layout ()
140
+ plt .show ()
126
141
127
142
128
143
129
144
if __name__ == "__main__" :
130
145
sa = SentimentAnalysis ()
131
146
sa .DownloadData ()
132
-
0 commit comments