Skip to content

Instantly share code, notes, and snippets.

@beta-decay
Created February 5, 2018 17:11
Show Gist options
  • Save beta-decay/d02d2b4c804b2f2b5b75b195f1d42714 to your computer and use it in GitHub Desktop.
Save beta-decay/d02d2b4c804b2f2b5b75b195f1d42714 to your computer and use it in GitHub Desktop.
import tweepy
import nltk
from nltk.sentiment.vader import SentimentIntensityAnalyzer
import matplotlib.pyplot as plt
# Consumer keys and access tokens, used for OAuth
consumer_key = ''
consumer_secret = ''
access_token = ''
access_token_secret = ''
# OAuth process, using the keys and tokens
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
user = "realDonaldTrump"
# Creation of the actual interface, using authentication
api = tweepy.API(auth)
sid = SentimentIntensityAnalyzer()
D = {'cnn':[],'nbc':[],'new york times':[],'washington post':[],'cbs':[],'abc':[],'time':[],'fox':[]}
for status in tweepy.Cursor(api.user_timeline, screen_name='@'+user).items():
tweet = status.text#.encode('utf-8',errors='ignore')
#print(tweet)
ss = sid.polarity_scores(tweet)['compound']
for outlet in D.keys():
if outlet in tweet.lower():
D[outlet] += [ss]
for outlet in D.keys():
if len(D[outlet])>0:
avg = sum(D[outlet])/len(D[outlet])
else:
avg = 0
D[outlet] = avg
plt.bar(range(len(D)), D.values(), align='center')
plt.xticks(range(len(D)), list(D.keys()), rotation=17)
plt.xlabel("News Outlets")
plt.ylabel("Average Sentiment")
plt.ylim(-0.4,0.2)
plt.grid()
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment