Skip to content

Instantly share code, notes, and snippets.

@beta-decay
Created January 31, 2018 15:00
Show Gist options
  • Save beta-decay/232ba5c70f7ed4ff0578c1ff2cfdb8b4 to your computer and use it in GitHub Desktop.
Save beta-decay/232ba5c70f7ed4ff0578c1ff2cfdb8b4 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 = "mailonline"
# Creation of the actual interface, using authentication
api = tweepy.API(auth)
sid = SentimentIntensityAnalyzer()
scores = []
times = []
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']
if abs(ss) >= 0.07:
scores.append(ss)
#print(float(status.created_at.timestamp())*(3.17098*10**-8) + 1970.0)
times.append(float(status.created_at.timestamp())*(3.17098*10**-8) + 1970.0)
plt.subplot(2,1,1)
plt.hist(scores, 15, range=(-1,1), rwidth=0.9)
plt.title("Sentiment of tweets by @" + user)
plt.ylabel("Frequency")
plt.xlabel("Sentiment")
plt.subplot(2,1,2)
plt.hist2d(times, scores, bins=20)
ax = plt.gca()
ax.get_xaxis().get_major_formatter().set_scientific(False)
ax.get_xaxis().get_major_formatter().set_useOffset(False)
plt.title("Sentiment of tweets over time by @" + user)
plt.ylabel("Sentiment")
plt.xlabel("Time")
plt.grid()
plt.tight_layout()
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment