In [52]:
!pip install oauth2
!pip install django
In [49]:
# coding: utf-8
import oauth2 as oauth
import urllib2 as urllib
import json
from django.utils.encoding import smart_str
import matplotlib.pyplot as plt
import bokeh
def is_ascii(s):
return all(ord(c) < 128 for c in s)
api_key = "YU0KmCt8JMhSml0EwurQDvUtv"
api_secret = "Iuu7zawVK2zmMJMeRgFGDl19LsqhkQs19MmKNUZSrCh7qk5oCq"
access_token_key = "825902122893971458-KMgjVVWqMibax6bDz8IwqOhzzQCQgnf"
access_token_secret = "wdfiGxkPBoRa6pWyhQE5FW2soUUAMeOCnuzKpVeSvIlGI"
_debug = 0
oauth_token = oauth.Token(key=access_token_key, secret=access_token_secret)
oauth_consumer = oauth.Consumer(key=api_key, secret=api_secret)
signature_method_hmac_sha1 = oauth.SignatureMethod_HMAC_SHA1()
http_method = "GET"
http_handler = urllib.HTTPHandler(debuglevel=_debug)
https_handler = urllib.HTTPSHandler(debuglevel=_debug)
def twitterreq(url, method, parameters):
req = oauth.Request.from_consumer_and_token(oauth_consumer,
token=oauth_token, http_method=http_method, http_url=url,
parameters=parameters)
req.sign_request(signature_method_hmac_sha1, oauth_consumer, oauth_token)
headers = req.to_header()
if http_method == "POST":
encoded_post_data = req.to_postdata()
else:
encoded_post_data = None
url = req.to_url()
opener = urllib.OpenerDirector()
opener.add_handler(http_handler)
opener.add_handler(https_handler)
response = opener.open(url, encoded_post_data)
return response
def fetchsamples():
url = "https://stream.twitter.com/1.1/statuses/sample.json"
parameters = []
response = twitterreq(url, "GET", parameters)
for line in response:
print line.strip()
def create_dictionary(sentiment):
afinn_file = sentiment
scores = {}
for line in afinn_file:
term, score = line.split('\t')
scores[term] = int(score)
return scores
def create_west_coast_list(tweets):
tweets_list = tweets
list_of_tweets = []
for line in tweets_list:
tweet = json.loads(line)
if tweet.has_key('user'):
user = tweet["user"]
if(user.has_key("location") and user["location"] is not None):
location = user["location"]
if(type(location) is not str):
location = smart_str(location)
if(not is_ascii(location)):
continue
#print(location)
if(location.endswith(", CA") or location.endswith(", OR") or location.endswith(", WA") or location.endswith(", HI") or location.endswith(", AK")):
if tweet.has_key('text'):
text = tweet['text']
text = smart_str(text)
text = unicode( text, "utf-8" )
list_of_tweets.append(text)
return list_of_tweets
def create_east_coast_list(tweets):
tweets_list = tweets
list_of_tweets = []
for line in tweets_list:
tweet = json.loads(line)
if tweet.has_key('user'):
user = tweet["user"]
if(user.has_key("location") and user["location"] is not None):
location = user["location"]
if(type(location) is not str):
location = smart_str(location)
if(not is_ascii(location)):
continue
#print(location)
if(location.endswith(", VA") or location.endswith(", DE") or location.endswith(", DC") or location.endswith(", PA") or location.endswith(", NJ") or location.endswith(", NY") or location.endswith(", ME") or location.endswith(", MA") or location.endswith(", NH") or location.endswith(", VT") or location.endswith(", CT")):
if tweet.has_key('text'):
text = tweet['text']
text = smart_str(text)
text = unicode( text, "utf-8" )
list_of_tweets.append(text)
return list_of_tweets
def create_south_list(tweets):
tweets_list = tweets
list_of_tweets = []
for line in tweets_list:
tweet = json.loads(line)
if tweet.has_key('user'):
user = tweet["user"]
if(user.has_key("location") and user["location"] is not None):
location = user["location"]
if(type(location) is not str):
location = smart_str(location)
if(not is_ascii(location)):
continue
#print(location)
if(location.endswith(", TX") or location.endswith(", OK") or location.endswith(", LA") or location.endswith(", MS") or location.endswith(", AR") or location.endswith(", FL") or location.endswith(", AL") or location.endswith(", GA") or location.endswith(", SC") or location.endswith(", NC") or location.endswith(", MD") or location.endswith(", TN") or location.endswith(", WV")):
if tweet.has_key('text'):
text = tweet['text']
text = smart_str(text)
text = unicode( text, "utf-8" )
list_of_tweets.append(text)
return list_of_tweets
def create_midwest_list(tweets):
tweets_list = tweets
list_of_tweets = []
for line in tweets_list:
tweet = json.loads(line)
if tweet.has_key('user'):
user = tweet["user"]
if(user.has_key("location") and user["location"] is not None):
location = user["location"]
if(type(location) is not str):
location = smart_str(location)
if(not is_ascii(location)):
continue
#print(location)
if(location.endswith(", ND") or location.endswith(", SD") or location.endswith(", NE") or location.endswith(", KS") or location.endswith(", MN") or location.endswith(", IO") or location.endswith(", MO") or location.endswith(", WI") or location.endswith(", MI") or location.endswith(", OH") or location.endswith(", IL") or location.endswith(", IN") or location.endswith(", KY")):
if tweet.has_key('text'):
text = tweet['text']
text = smart_str(text)
text = unicode( text, "utf-8" )
list_of_tweets.append(text)
return list_of_tweets
def create_rocky_mountains_list(tweets):
tweets_list = tweets
list_of_tweets = []
for line in tweets_list:
tweet = json.loads(line)
if tweet.has_key('user'):
user = tweet["user"]
if(user.has_key("location") and user["location"] is not None):
location = user["location"]
if(type(location) is not str):
location = smart_str(location)
if(not is_ascii(location)):
continue
#print(location)
if(location.endswith(", MT") or location.endswith(", ID") or location.endswith(", WY") or location.endswith(", NE") or location.endswith(", UT") or location.endswith(", CO") or location.endswith(", AZ") or location.endswith(", NM")):
if tweet.has_key('text'):
text = tweet['text']
if(is_ascii(text)):
if(type(text) is not str):
continue
list_of_tweets.append(text)
return list_of_tweets
def calculate_scores(tweets, dictionary):
scores = []
for i in tweets:
score = 0
words_list = i.split()
for i in words_list:
if i in dictionary:
score = score + dictionary[i]
scores.append(score)
return scores
if __name__ == '__main__':
list_tweets = []
scores_west_coast = []
scores_east_coast = []
scores_south = []
scores_midwest = []
scores_rocky_mountains = []
# fetchsamples()
sent_file = open("AFINN-111.txt")
tweet_file = open("three_minutes_tweets.json")
dictionary = create_dictionary(sent_file)
west_coast_tweets = create_west_coast_list(tweet_file)
sent_file = open("AFINN-111.txt")
tweet_file = open("three_minutes_tweets.json")
dictionary = create_dictionary(sent_file)
east_coast_tweets = create_east_coast_list(tweet_file)
sent_file = open("AFINN-111.txt")
tweet_file = open("three_minutes_tweets.json")
dictionary = create_dictionary(sent_file)
south_tweets = create_south_list(tweet_file)
sent_file = open("AFINN-111.txt")
tweet_file = open("three_minutes_tweets.json")
dictionary = create_dictionary(sent_file)
midwest_tweets = create_midwest_list(tweet_file)
sent_file = open("AFINN-111.txt")
tweet_file = open("three_minutes_tweets.json")
dictionary = create_dictionary(sent_file)
rocky_mountains_tweets = create_rocky_mountains_list(tweet_file)
scores_west_coast = calculate_scores(west_coast_tweets, dictionary)
scores_east_coast = calculate_scores(east_coast_tweets, dictionary)
scores_south = calculate_scores(south_tweets, dictionary)
scores_midwest = calculate_scores(midwest_tweets, dictionary)
scores_rocky_mountains = calculate_scores(rocky_mountains_tweets, dictionary)
print(west_coast_tweets)
"""print(east_coast_tweets)
print(south_tweets)
print(midwest_tweets)
print(rocky_mountains_tweets)
print(scores_west_coast)
print(scores_east_coast)
print(scores_south)
print(scores_midwest)
print(scores_rocky_mountains)"""
In [87]:
print(midwest_tweets)
In [33]:
from bokeh.plotting import figure, ColumnDataSource
from bokeh.io import show, output_notebook
In [ ]:
In [100]:
scores_south = [2,3,5,-5,8,3-2]
sum_south = 0
for i in scores_south:
sum_south = sum_south + i
avg_south = sum_south/len(scores_south)
scores_midwest = [-5, -2, -3, -3, 0, 3, 2, 1, 1, 0]
sum_midwest = 0
for i in scores_midwest:
sum_midwest = sum_midwest + i
avg_midwest = sum_midwest/len(scores_midwest)
scores_west_coast = [2, 4, 0, 0, 1, -3, -4, 0, 0, -1]
sum_west_coast = 0
for i in scores_west_coast:
sum_west_coast = sum_west_coast + i
avg_west_coast = sum_west_coast/len(scores_west_coast)
scores_east_coast = [4, 0, -1, 4, 3, 2, 0, 0, -3]
sum_east_coast = 0
for i in scores_east_coast:
sum_east_coast = sum_east_coast + i
avg_east_coast = sum_east_coast/len(scores_east_coast)
scores_rocky_mountains = [1, 4, 0, 0, 0, -2, -3, 0, 0, 1, -1]
sum_rocky_mountains = 0
for i in scores_rocky_mountains:
sum_rocky_mountains = sum_rocky_mountains + i
avg_rocky_mountains = sum_rocky_mountains/len(scores_rocky_mountains)
avg = [avg_south, avg_midwest, avg_west_coast, avg_east_coast, avg_rocky_mountains]
p = figure(x_axis_label = "scores_south", y_axis_label = "scores_south")
p.circle(scores_south, avg)
q = figure(x_axis_label = "scores_midwest", y_axis_label = "scores_midwest")
q.circle(scores_midwest, avg)
w = figure(x_axis_label = "scores_wc", y_axis_label = "scores_wc")
w.circle(scores_west_coast, avg)
e = figure(x_axis_label = "scores_ec", y_axis_label = "scores_ec")
e.circle(scores_east_coast, avg)
r = figure(x_axis_label = "scores_rm", y_axis_label = "scores_rm")
r.circle(scores_rocky_mountains, avg)
output_notebook()
show(p)
show(q)
show(w)
show(e)
show(r)
In [99]:
j = figure(x_axis_label = "scores", y_axis_label = "scores")
j.circle(avg, avg)
output_notebook()
show(j)
In [68]:
scores_south = [2,3,5,-8,10,25-8]
scores_south = [20,30,50,-16,15,25-8]
sdf = pd.DataFrame({
"scores": scores_south,
})
sdf
sdf1 = pd.DataFrame({
"scores1": scores_south
})
sdf1
Out[68]:
In [76]:
from bokeh.charts import BoxPlot, output_file, show
from bokeh.sampledata.autompg import autompg as df
# Use Bokeh chart to make plot
p = bokeh.charts.BoxPlot(sdf, values='scores', label='scores', xlabel='scores', ylabel='scores')
# Display it
bokeh.io.show(p)
In [83]:
p = BoxPlot(sdf, values="scores", width=400, height=400, label='scores', xlabel='scores', ylabel='scores')
show(p)
sdf
Out[83]:
In [ ]: