-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKod Dosyası
64 lines (52 loc) · 1.82 KB
/
Kod Dosyası
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
from textblob import TextBlob
import sys. tweepy
import matplotlib.pyplot as plt
def percentage(part, whole):
return 100 * float (part)/float(whole)
consumerKey=" "
consumerSecret=" "
accessToken=" "
accessTokenSecret= " "
auth = tweepy.OAuthHandler(consumer_key, consumerSecret)
auth.set_access_token(accessToken, accessTokenSecret)
api = tweepy.API(auth)
searchTerm = input ("Enter keyword/hashtag to search about: ")
noOfSearchTerms = int(input(Enter how many tweets to analyze:"))
tweets = tweepy.Cursor(api.search, q=searchTerm, lang="English").items(noOfSearchTerms)
positive = 0
negative = 0
neutral = 0
polarity = 0
for tweet in tweets:
#print(tweet.text)
analysis = TextBlob(tweet.text)
polarity += analysis.sentiment.polarity
if(analysis.sentiment.polarity==0):
neutral += 1
elif(analysis.sentiment.polarity <0.00):
negative += 1
elif(analysis.sentiment.polarity>0.00):
positive += 1
positive = percentage(positive, noOfSearchTerms)
negative = percentage(negative, noOfSearchTerms)
neutral = percentage(neutral, noOfSearchTerms)
polarity = percentage(polarity, noOfSearchTerms)
positive = format(positive, '.2f')
negative = format(negative, '.2f')
neutral = format(neutral, '.2f')
print("How people are recting on " + searchTerm + " by analyzing " + str(noOfSearchTerms)+ "Tweets.")
if (polarity == 0):
print ("Neutral")
elif (polarity < 0):
print("Negative")
elif (polarity >0):
print("Positive")
labels = ['Positive ['str(positive)+'%]' , 'Neutral ['+ str(neutral)+'%]', 'Negative ['+ str(negative)+'%]']
sizes = [positive, neutral, negative]
colors = [ 'yellowgreen' , 'gold' , 'red']
patches, texts = plt.pie(sizes, colors=colors, startangle=90)
plt.legend(patches, labels, loc="best")
plt.title('how people are reacting on '+searchTerm+' by analyzing '+str(noOfSearchTerms)+' Tweets.')
plt.axis('equal')
plt.tight_layout()
plt.show()