コード例 #1
0
def update_graph_live(value):

    mongo = MongoWrapper()

    getTweets = mongo.get_tweets_with_lat_long('Facebook')
    allLatitude = getTweets['Latitude']
    allLongitude = getTweets['Longitude']
    allSentiment = getTweets['Sentiment_Value']

    bigGraph = dcc.Graph(
        style={'height': '800px'},
        figure={
            'data': [{
                'type': 'scattergeo',
                'locationmode': 'USA-states',
                'lon': allLongitude,
                'lat': allLatitude,
                'text': allSentiment,
                'mode': 'markers',
                'marker': {
                    'size': 8,
                    'opacity': 0.8,
                    'reversescale': True,
                    'autocolorscale': False,
                    'symbol': 'circle',
                    'line': {
                        'width': 1,
                        'color': 'rgba(102, 102, 102)'
                    },
                    'colorscale': scl,
                    'cmin': -1,
                    'color': allSentiment,
                    'cmax': 1,
                    'colorbar': {
                        'title': "Polarity Scale"
                    }
                }
            }],
            'layout': {
                'title': {
                    'text': 'Tweet locations with sentiment ratings',
                },
                'font': {
                    'size': 15,
                },
                'geo': {
                    # 'scope':'usa',
                    # 'projection':dict( 'type'='albers usa' ),
                    'showland': True,
                    'landcolor': "rgb(250, 250, 250)",
                    'subunitcolor': "rgb(217, 217, 217)",
                    'countrycolor': "rgb(217, 217, 217)",
                    'countrywidth': 0.5,
                    'subunitwidth': 0.5
                },
            }
        })

    return (bigGraph)
コード例 #2
0
import pandas as pd


df = pd.read_csv('/Users/iankresyman/Desktop/2011_february_us_airport_traffic2.csv')
df.head()

df['text'] = df['airport'] + '' + df['city'] + ', ' + df['state'] + '' + 'Arrivals: ' + df['cnt'].astype(str)

scl = [ [0,"rgb(39,174,96)"],[0.35,"rgb(46,204,113)"],[0.5,"rgb(241,196,15)"],\
    [0.6,"rgb(243,156,18)"],[0.7,"rgb(231,76,60)"],[1,"rgb(192,57,43)"] ]

mongo = MongoWrapper()

negCoord, neuCoord, posCoord = mongo.get_lat_long('Facebook')

getTweets =  mongo.get_tweets_with_lat_long('Facebook')
allLatitude = getTweets['Latitude']
allLongitude = getTweets['Longitude']
allSentiment = getTweets['Sentiment_Value']



print('\n')
# print(negCoord[0])
# df1 = pd.DataFrame()
# df2 = pd.DataFrame()
# df3 = pd.DataFrame()
# df4 = pd.DataFrame()
# df5 = pd.DataFrame()
# df6 = pd.DataFrame()
# print(mongo.get_tweets_with_lat_long('Facebook'))
コード例 #3
0
 def test_get_tweets_with_lat_long(self):
     mongo = MongoWrapper()
     test = mongo.get_tweets_with_lat_long('Google')
     print(test)
コード例 #4
0
class RedisWrapper():
    def __init__(self, decrypt_key):
        self.redicclient = redis.StrictRedis(host='35.236.16.13', port=6379, db=0, decode_responses=True)
        self.mng = MongoWrapper(decrypt_key)

    def get_logger(self, logger_name):
        return self.mng.get_logger(logger_name)

    def redis_update_json(self, api_string, key):
        if api_string == 'get_tweets_with_lat_long/':

            json_data = self.mng.get_tweets_with_lat_long(key)
            self.redicclient.execute_command('JSON.SET', api_string+key, '.', json_data)

        elif api_string == 'get_polarity_tweets_of_stock/':

            json_data = self.mng.get_polarity_tweets_of_stock(key)
            self.redicclient.execute_command('JSON.SET', api_string + key, '.', json_data)


    def redis_insert_tweet(self, key, tweet):
        """
        Either insert a single tweet or multiple tweets and this def will update the redis cache accordingly
        :param key:
        :param tweets:
        :return:
        """
        try:
            lat_long_list = tweet['Geo']['coordinates']
            has_lat_long = True
        except:
            has_lat_long = False
            lat_long_list = ['None', 'None']

        if has_lat_long:
            sentiment_value = float(tweet["Sentiment_Value"])
            full_text = tweet["Text"]
            root_json_path = {}
            root_json_path["Latitude"] = lat_long_list[0]
            root_json_path["Longitude"] = lat_long_list[1]
            root_json_path["Tweet_Text"] = full_text
            root_json_path["Sentiment_Value"] = sentiment_value
            api_string = 'get_tweets_with_lat_long/'
            self.redicclient.execute_command('JSON.ARRAPPEND', api_string+key, '.', json.dumps(root_json_path))

        sentiment_polarity = int(tweet["Sentiment_Polarity"])
        full_text = tweet["Text"]
        root_json_path = {}
        root_json_path["Latitude"] = lat_long_list[0]
        root_json_path["Longitude"] = lat_long_list[1]
        root_json_path["Tweet_Text"] = full_text
        root_json_path["Sentiment_Polarity"] = sentiment_polarity
        api_string = 'get_polarity_tweets_of_stock/'
        if sentiment_polarity == -1:
            root_path = '.Negative_Tweets'
        elif sentiment_polarity == 0:
            root_path = '.Neutral_Tweets'
        elif sentiment_polarity == 1:
            root_path = '.Positive_Tweets'
        self.redicclient.execute_command('JSON.ARRAPPEND', api_string + key, root_path, json.dumps(root_json_path))

    def redis_get_json(self, api_string, key):
        return self.redicclient.execute_command('JSON.GET', api_string+key)

    def redis_flush_all(self):
        """
        Danger. This flushes the whole DB. Careful
        :return:
        """
        self.redicclient.flushdb()
コード例 #5
0
def update_graph_live(value):

    mongo = MongoWrapper()

    getTweets = mongo.get_tweets_with_lat_long(value)
    allLatitude = getTweets['Latitude']
    allLongitude = getTweets['Longitude']
    allSentiment = getTweets['Sentiment_Value']

    tots = allSentiment.count()

    scl = [ [0,"rgb(39,174,96)"],[0.35,"rgb(46,204,113)"],[0.5,"rgb(241,196,15)"],\
    [0.6,"rgb(243,156,18)"],[0.7,"rgb(231,76,60)"],[1,"rgb(192,57,43)"] ]

    bigGraph = dcc.Graph(
        style={'height': '800px'},
        figure={
            'data': [{
                'type': 'scattergeo',
                'locationmode': 'USA-states',
                'lon': allLongitude,
                'lat': allLatitude,
                'text': allSentiment,
                'mode': 'markers',
                'marker': {
                    'size': 8,
                    'opacity': 0.8,
                    'reversescale': True,
                    'autocolorscale': False,
                    'symbol': 'circle',
                    'line': {
                        'width': 1,
                        'color': 'rgba(102, 102, 102)'
                    },
                    'colorscale': scl,
                    'cmin': -1,
                    'color': allSentiment,
                    'cmax': 1,
                    'colorbar': {
                        'title': "Polarity Scale",
                        'thickness': 20,
                        'titleside': "right",
                        # 'outlinecolor' : "rgba(68, 68, 68, 0)",
                        'ticks': "outside",
                        'ticklen': 3,
                        # 'showticksuffix' : "last",
                        # 'ticksuffix' : " inches",
                        'dtick': 0.1
                    }
                }
            }],
            'layout': {
                'title': "Twitter Sentiment for {}\n".format(value),
                'font': {
                    'size': 15,
                },
                'geo': {
                    # 'scope':'usa',
                    # 'projection':dict( 'type'='albers usa' ),
                    'showland': True,
                    'landcolor': "rgb(250, 250, 250)",
                    'subunitcolor': "rgb(217, 217, 217)",
                    'countrycolor': "rgb(217, 217, 217)",
                    'countrywidth': 0.5,
                    'subunitwidth': 1,
                    'showsubunits': True,
                    'showcountries': True,
                    'showcoastlines': True,
                    'coastlinecolor': "rgb(155, 155, 155)",
                    'showframe': True,
                    'framecolor': "rgb(155, 155, 155)"
                    # 'showocean':True
                    # 'showlakes':True
                },
            }
        }), html.Div(
            children='Total Tweets pulled for searchword: {}.\n'.format(tots)),

    return (bigGraph)