In [299]:
!pip install oauth2
!pip install django
!pip install textblob
!pip install plotly


Requirement already satisfied: oauth2 in c:\users\mildoosj\appdata\local\continuum\anaconda2\lib\site-packages
Requirement already satisfied: httplib2 in c:\users\mildoosj\appdata\local\continuum\anaconda2\lib\site-packages (from oauth2)
Requirement already satisfied: django in c:\users\mildoosj\appdata\local\continuum\anaconda2\lib\site-packages
Requirement already satisfied: pytz in c:\users\mildoosj\appdata\local\continuum\anaconda2\lib\site-packages (from django)
Requirement already satisfied: textblob in c:\users\mildoosj\appdata\local\continuum\anaconda2\lib\site-packages
Requirement already satisfied: nltk>=3.1 in c:\users\mildoosj\appdata\local\continuum\anaconda2\lib\site-packages (from textblob)
Requirement already satisfied: six in c:\users\mildoosj\appdata\local\continuum\anaconda2\lib\site-packages (from nltk>=3.1->textblob)
Requirement already satisfied: plotly in c:\users\mildoosj\appdata\local\continuum\anaconda2\lib\site-packages
Requirement already satisfied: requests in c:\users\mildoosj\appdata\local\continuum\anaconda2\lib\site-packages (from plotly)
Requirement already satisfied: decorator>=4.0.6 in c:\users\mildoosj\appdata\local\continuum\anaconda2\lib\site-packages (from plotly)
Requirement already satisfied: pytz in c:\users\mildoosj\appdata\local\continuum\anaconda2\lib\site-packages (from plotly)
Requirement already satisfied: nbformat>=4.2 in c:\users\mildoosj\appdata\local\continuum\anaconda2\lib\site-packages (from plotly)
Requirement already satisfied: six in c:\users\mildoosj\appdata\local\continuum\anaconda2\lib\site-packages (from plotly)

In [300]:
import oauth2 as oauth
import urllib2 as urllib
import json
from django.utils.encoding import smart_str
import matplotlib.pyplot as plt
import bokeh
from bokeh.plotting import figure, ColumnDataSource
from bokeh.io import show, output_notebook
from bokeh.layouts import row, column, gridplot
from bokeh.models import HoverTool
from bokeh.models.widgets import Panel, Tabs
import pandas as pd
import numpy as np
from textblob import TextBlob
import plotly.plotly as py
import plotly.graph_objs as go
import plotly 
plotly.tools.set_credentials_file(username='imdimd123', api_key='CAQgLv5gFyj0ZMubi9IQ')

In [301]:
# coding: utf-8

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 = []
    temp = []
    for i in tweets:
        score = TextBlob(i).sentiment.polarity
        if(score != 0):
            scores.append(score)
            temp.append(i)
    return temp, 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")
    tweet_file = open("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")
    tweet_file = open("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")
    tweet_file = open("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")
    tweet_file = open("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")
    tweet_file = open("tweets.json")
    dictionary = create_dictionary(sent_file)

    rocky_mountains_tweets = create_rocky_mountains_list(tweet_file)

    west_coast_tweets, scores_west_coast = calculate_scores(west_coast_tweets, dictionary)
    east_coast_tweets, scores_east_coast = calculate_scores(east_coast_tweets, dictionary)
    south_tweets, scores_south = calculate_scores(south_tweets, dictionary)
    midwest_tweets, scores_midwest = calculate_scores(midwest_tweets, dictionary)
    rocky_mountains_tweets, 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 [302]:
west_coast_df = pd.DataFrame({
    "west coast tweets": west_coast_tweets,
    "scores tweets west coast": scores_west_coast
})
west_coast_df


Out[302]:
scores tweets west coast west coast tweets
0 0.333333 @Doobybrain pros - sound is awesome, easy to s...
1 0.700000 @snoop_a_loop14 I'm in a good mood today 😇😂
2 -0.100000 Partner Support Center Coordinator job - Uber ...
3 0.500000 How VA's Have Made Josh Stanton A Better Leade...
4 0.500000 RT @oliviaculpo: I love this http://t.co/gk8Lh...
5 -0.100000 50 cent P5 pool today closed at a whopping $70...
6 0.500000 GetSaent #focus and boost #productivity! Learn...
7 -0.200000 RT @reIatabIe: fuck a double text i’ll quad te...
8 0.300000 RT @itfunnygifs: Wow Frozen Are You Drunk.(WOW...

In [303]:
east_coast_df = pd.DataFrame({
    "east coast tweets": east_coast_tweets,
    "scores tweets east coast": scores_east_coast
})
east_coast_df


Out[303]:
east coast tweets scores tweets east coast
0 M.T.A. N.Y.C. Subways: 6 Train normal service... 0.150000
1 RT @TrumanProject: It's been the #1 "Most Read... 0.500000
2 @Canine_Rights pls stand for truth be it eithe... 0.600000
3 Congratulations to Team Relentless athlete Jar... 0.068182
4 RT @GoPittFootball: The Giant Killer @pittbost... 0.250000
5 @JaroslavWrestle Aaron did not dissapoint! Gre... 0.800000
6 Fur vests are too cute! http://t.co/SamOwumgH7 0.625000
7 Meet Shawn Wooden, the former player at the ce... -0.033333
8 @jauregretful don't ever send me that shit aga... -0.200000
9 Flight got delayed 😩 but whatever at least I'... 0.250000
10 RT @ElenaSolc: Pantiesc like to Breathe too Fi... 0.500000
11 RT @BmoreDoc: There is a class &amp; respectab... -0.166667

In [304]:
south_df = pd.DataFrame({
    "south tweets": south_tweets,
    "scores tweets south": scores_south
})
south_df


Out[304]:
scores tweets south south tweets
0 0.050000 You really gotta be careful who you trust.
1 -0.002778 Life is hard apart.. It will only get harder t...
2 -0.500000 http://t.co/oxZzR5LM0m Unsettling: Footage Of ...
3 0.175000 12" Western Leather Horse Pleasure Pony Youth ...
4 0.335227 TOP OF THE MORNING: Live From Tallahassee!, Is...
5 0.500000 RT @DiamondMinecart: Our world is way too focu...
6 0.200000 Check out I Drink Coffee Now! t-shirt availabl...
7 1.000000 RT @ImMadVids: MY FAT IS MY BEST QUALITY #NowI...
8 0.533333 RT @HTC_Liz: My happy place ❤️❤️❤️ felt great ...
9 1.000000 RT @Ari_anna23: Ima be the best mama &amp; wif...
10 -0.528571 it's pathetic how you say that you hate certai...
11 -0.050000 RT @VisitSavannah: @RobinBirdsWeeds @the_color...
12 0.358333 @MommaSavage_ its treating me pretty good so f...
13 0.800000 RT @Corey_Bender: Lol "@SportsVideoss: Watch o...
14 0.200000 RT @lmpuIsive: "Are you ready for school?" \nh...
15 0.285714 That's right: @BarackObama is on #Spotify http...
16 0.350000 RT @Nick_TheHat: @Nick_TheHat OK I LOOKED IT U...
17 0.500000 ESPN seems to have unceremoniously dumped Roya...
18 -0.500000 That weird moment when you visited Nashville 2...
19 0.200000 @FlyingRobotGirl it's already in UPS's capable...
20 -0.714286 RT @omfgmc1120: Loco, Maniac, Sick bitch, pysc...

In [305]:
midwest_df = pd.DataFrame({
    "midwest tweets": midwest_tweets,
    "scores tweets midwest": scores_midwest
})
midwest_df


Out[305]:
midwest tweets scores tweets midwest
0 RT @IAFF526_LexFire: Engine 2 &amp; EC1 on sce... -0.075000
1 When your in the middle of crying and then you... -0.233333
2 RT @HotMulligan: Hot Mulligan #WeirdTour2K15\n... 0.225000
3 I took the kids to the Walker Art Center and M... -0.100000
4 RT @RedCrypted: 4 MINS! GET ACTIVE -0.133333
5 RT @tbhdaphne: OMG IM CRYING http://t.co/XcnLb... -0.200000
6 RT @si_ncaafb: PHOTOS: Vanderbilt football unv... 0.068182
7 @Kevin_VFB @PurplePeoplePod Impressive at any ... 1.000000
8 RT @DailyLoud: Man Kodak Black is 17 putting o... 0.016667

In [306]:
rocky_mountains_df = pd.DataFrame({
    "rocky mountains tweets": rocky_mountains_tweets,
    "scores tweets rocky mountains": scores_rocky_mountains
})
rocky_mountains_df


Out[306]:
rocky mountains tweets scores tweets rocky mountains

In [307]:
y0 = scores_west_coast
y1 = scores_east_coast
y2 = scores_south
y3 = scores_midwest
y4 = scores_rocky_mountains

trace0 = go.Box(
    y=y0,
    name= "west_coast",
    boxpoints='all',
    boxmean='sd'
)
trace1 = go.Box(
    y=y1,
    name= "east_coast",
    boxpoints='all',
    boxmean='sd'
)
trace2 = go.Box(
    y=y2,
    name= "south",
    boxpoints='all',
    boxmean='sd'
)
trace3  = go.Box(
    y=y3,
    name= "midwest",
    boxpoints='all',
    boxmean='sd'
)
trace4 = go.Box(
    y=y4,
    name= "rocky_mountains",
    boxpoints='all',
    boxmean='sd'
)
data = [trace0, trace1, trace2, trace3, trace4]
py.iplot(data)


Out[307]:

In [308]:
fig = {
    'data': [
        {
            'labels': ['positive', 'negative'],
            'values': values0,
            'type': 'pie',
            'name': 'Starry Night',
            'marker': {'colors': ['00FF00',
                                  '#FF0000']},
            'domain': {'x': [0, .48],
                       'y': [0, .49]},
            'hoverinfo':'label+percent+name',
            'textinfo':'percent'
        },
        {
            'labels': ['positive', 'negative'],
            'values': values1,
            'type': 'pie',
            'name': 'Starry Night',
            'marker': {'colors': ['00FF00',
                                  '#FF0000']},
            'domain': {'x': [.52, 1],
                       'y': [0, .49]},
            'hoverinfo':'label+percent+name',
            'textinfo':'percent'
        },
        {
            'labels': ['positive', 'negative'],
            'values': values2,
            'type': 'pie',
            'name': 'Starry Night',
            'marker': {'colors': ['00FF00',
                                  '#FF0000']},
            'domain': {'x': [0, .48],
                       'y': [.51, 1]},
            'hoverinfo':'label+percent+name',
            'textinfo':'percent'
        },
        {
            'labels': ['positive', 'negative'],
            'values': values3,
            'type': 'pie',
            'name': 'Starry Night',
            'marker': {'colors': ['00FF00',
                                  '#FF0000']},
            'domain': {'x': [.52, 1],
                       'y': [.51, 1]},
            'hoverinfo':'label+percent+name',
            'textinfo':'percent'
        },
        {
            'labels': ['positive', 'negative'],
            'values': values4,
            'type': 'pie',
            'name': 'Starry Night',
            'marker': {'colors': ['00FF00',
                                  '#FF0000']},
            'domain': {'x': [.0, 1],
                       'y': [.51, 1]},
            'hoverinfo':'label+percent+name',
            'textinfo':'percent'
        },
    ],
    'layout': {
        'title': 'Van Gogh: 5 Most Prominent Colors Shown Proportionally',
        'showlegend': False,
        "annotations": [
            {
                "font": {
                    "size": 20
                },
                "showarrow": False,
                "text": "South",
                "x": 0.20,
                "y": 1.1
            },
            {
                "font": {
                    "size": 20
                },
                "showarrow": False,
                "text": "Midwest",
                "x": 0.78,
                "y": 1.1
            },
            {
                "font": {
                    "size": 20
                },
                "showarrow": False,
                "text": "West",
                "x": 0.22,
                "y": -0.1
            },
            {
                "font": {
                    "size": 20
                },
                "showarrow": False,
                "text": "East",
                "x": 0.82,
                "y": -0.1
            },
            {
                "font": {
                    "size": 20
                },
                "showarrow": False,
                "text": "Rocky mountains",
                "x": 0.5,
                "y": 1.1
            }
        ]
              }
}

py.iplot(fig, filename='pie_chart_subplots')


Out[308]:

In [ ]: