Skip to content

Instantly share code, notes, and snippets.

@tonybaloney
Created August 9, 2016 05:32
Show Gist options
  • Save tonybaloney/4c3c0fbaf8a9d9941c7345908ac85810 to your computer and use it in GitHub Desktop.
Save tonybaloney/4c3c0fbaf8a9d9941c7345908ac85810 to your computer and use it in GitHub Desktop.
import pandas as pd
from nltk.sentiment.vader import SentimentIntensityAnalyzer
from matplotlib import pyplot as plt
import matplotlib.dates as mdates
import numpy
import pylab
with open('tweets.csv') as tweets:
df = pd.read_csv(tweets)
data = []
sid = SentimentIntensityAnalyzer()
df['timestamp'] = pd.to_datetime(df['timestamp'])
chart = []
for (sentence, id) in zip(df.text.values, df.timestamp.values):
ss = sid.polarity_scores(sentence)
chart.append({'sentiment': ss['compound'], 'date': id})
df = pd.DataFrame.from_records(chart)
x = mdates.date2num(df.date)
z = numpy.polyfit(x, df.sentiment, 1)
p = numpy.poly1d(z)
pylab.plot(x, p(x), "r--")
plt.plot(df.date, df.sentiment)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment