In [1]:
from pattern.en import sentiment
from pattern.web import Twitter
In [2]:
twitter = Twitter()
In [3]:
keywords = "#donaldtrump OR #trump"
In [4]:
samplesize = 100
In [5]:
results = [tweet.text.lower() for tweet in twitter.search(keywords, count=samplesize)]
In [6]:
sentiments = [sentiment(text) for text in results]
In [7]:
positivity = [res[0] for res in sentiments if res[0] > 0]
negativity = [res[0] for res in sentiments if res[0] < 0]
In [8]:
total_pos_score = sum(positivity)
total_neg_score = sum(negativity)
n_pos_score = len(positivity)
n_neg_score = len(negativity)
pos_avg = 0
if (n_pos_score > 0): pos_avg = total_pos_score/n_pos_score
neg_avg = 0
if (n_neg_score > 0): neg_avg = total_neg_score/n_neg_score
In [9]:
print n_pos_score, pos_avg
In [10]:
print n_neg_score, neg_avg
In [ ]: