-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathTwitterSentiment.py
99 lines (62 loc) · 1.99 KB
/
TwitterSentiment.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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import tweepy, sys
import matplotlib.pyplot as plt
from textblob import TextBlob
import mpld3
# In[2]:
# function to calculate the percentage
def percent(part, whole):
return 100 * float(part)/float(whole)
# In[3]:
#connection with the api
consumerkey = "Enter key"
consumersecret = "Enter consumer Secret"
accessToken = "Enter access token"
accessTokenSecret = "Enter access token secret"
# In[4]:
auth = tweepy.OAuthHandler(consumer_key=consumerkey, consumer_secret=consumersecret)
auth.set_access_token(accessToken, accessTokenSecret)
api = tweepy.API(auth)
# In[9]:
searchFor = input("Enter the keyword or hastag:")
numberSearch = int(input("number of tweets:"))
# In[19]:
#below down we are providing the search item and the number of items
tweets = tweepy.Cursor(api.search, q=searchFor, lang="English").items(numberSearch)
positive = 0
negative = 0
neutral = 0
polarity = 0
for tweet in tweets:
analysis = TextBlob(tweet.text)
polarity += analysis.sentiment.polarity
if(analysis.sentiment.polarity == 0):
neutral += 1
elif(analysis.sentiment.polarity < 0 ):
negative += 1
elif(analysis.sentiment.polarity > 0):
positive += 1
positive = percent(positive, numberSearch)
negative = percent(negative, numberSearch)
neutral = percent(neutral, numberSearch)
print(positive)
print(negative)
print(neutral)
positive = format(positive, '.2f')
negative = format(negative, '.2f')
neutral = format(neutral, '.2f')
# In[18]:
#get_ipython().run_line_magic('matplotlib', 'inline')
labels = ['Positive ['+str(positive)+'%]', 'Negative ['+str(negative)+'%]', 'Neutral ['+str(neutral)+'%]']
sizes = [positive, negative, neutral]
colors = ['yellowgreen', 'red', 'blue']
explode = (0.1,0,0)
patches, texts = plt.pie(sizes, colors=colors, explode=explode )
plt.legend(patches, labels, loc='best')
plt.title("People reaction on the " +searchFor)
plt.axis('equal')
plt.tight_layout()
plt.show()
# In[ ]: