-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSentiment.py
More file actions
71 lines (54 loc) · 2.36 KB
/
Copy pathSentiment.py
File metadata and controls
71 lines (54 loc) · 2.36 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
import tweepy
from textblob import TextBlob
import datetime
#Change Keys to useable Twitter API key
consumer_key = 'AczBVcVl7W1kIqoEwKjZa34jO'
consumer_secret = 'sjWqaeCKqBghkxqYLOYKZWKMz8LM6i3K0YMn5Lhw6Zn1KRySqy'
access_token = '1873709048-bsx9JjSvNQv7vEtZLcufaHEMzjLUJLIvc7BwOkQ'
access_token_secret = 'aIpz3h7axzJhLobZjfhLrXchdvhQZOyyXgBk9WX0smcSY'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
#Authenticates API
api = tweepy.API(auth)
#public_tweets_sox = api.search('Red Sox')
#public_tweets_celtics = api.search('Celtics')
#public_tweets_bruins = api.search('Bruins')
#Function that returns the average polarity of a tweet
def avg_sentiment(api):
counter = 0
total_polarity = 0
for tweet in api:
analysis = TextBlob(tweet.text)
counter = counter + 1
total_polarity = total_polarity + analysis.sentiment[0]
return total_polarity/counter
#Function that compares the average sentiment of the teams
def compare_sentiment(t):
big_polarity = 0
winning_team = ""
for i in t:
public_tweets = api.search(i)
avg_team_sent = avg_sentiment(public_tweets)
print(i, avg_team_sent)
if (avg_team_sent > big_polarity):
big_polarity = max(avg_team_sent, big_polarity)
winning_team = i
return "The team with the highest polarity is:", winning_team,"with an average polarity of:", big_polarity
teams = ['Red Sox', 'Celtics', 'Bruins']
print(compare_sentiment(teams))
#Ask the user to enter three team/topic and tell the user which team/topic has the highest average polarity
new_list = [input("What is your first topic: "),input("What is your second topic: "),input("What is your third topic: ")]
print(big_function(new_list))
#Return the tweet with the highest polarity given an user input
def high_pol():
wt = input("What is your tweet word be?")
public_tweets = api.search(wt)
highest_sent = 0
good_tweet = ""
for tweet in public_tweets:
analysis = TextBlob(tweet.text)
if analysis.sentiment[0] > highest_sent:
highest_sent = analysis.sentiment[0]
good_tweet = tweet.text
return "The most positive tweet in your subject is", good_tweet, "with a sentiment of", highest_sent
print(high_pol())