from tweet_preprocess import load_df

from textblob import TextBlob
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
import seaborn as sns
import cartopy

# set graph display options 
pd.set_option('display.max_colwidth', 200)
pd.options.display.mpl_style = 'default'
matplotlib.style.use('ggplot')
sns.set_context('talk')
sns.set_style('darkgrid')

# load captured tweets
df = load_df('/Users/alanseciwa/Desktop/results3.csv')

# See the overall count relating to the keys 
df.info()

# prints out first row from tweets
print(df[['candidate', 'created_at', 'lang', 'place', 'user_followers_count', 
    'user_time_zone', 'polarity', 'influenced_polarity', 'text']].head(1))

# find polarity of ONLY english words and display the set
# the textblob function translate() could be used
english_df = df[df.lang == 'en']
english_df.sort('polarity', ascending=False).head(3)[['candidate', 'polarity', 'subjectivity', 'text']]

# Find mean polarity for each candidate by looking at the influenced_polarity. 
# this takes into account the number of retweets and number of followers
candidate_group = english_df.groupby('candidate')
print(candidate_group[['polarity', 'influence', 'influenced_polarity']].mean())

# Look at the influential Tweets about Donald Trump and Bernie Sanders
'''
jeb = candidate_group.get_group('Jeb Bush')
jeb_influence = jeb.sort('influence', ascending=False)
print('')
print('-----------')
print('Jeb Bush')
print('-----------')
print(jeb_influence[['influence', 'polarity', 'influenced_polarity', 'user_name', 'text', 'created_at']].head(5))

print('')
print('-----------')
print(df[df.user_name == 'Jeb Bush'].groupby('candidate').size())
'''
# Trump
trump = candidate_group.get_group('Donald Trump')
trump_influence = trump.sort('influence', ascending=False)
print('--------------')
print('Donald Trump')
print('--------------')
trump_influence[['influence', 'polarity', 'influenced_polarity', 'user_name', 'text', 'created_at']].head(5)


# Sanders
sanders = candidate_group.get_group('Bernie Sanders')
sanders_influence = sanders.sort('influence', ascending=False)
print('--------------')
print('Bernie Sanders')
print('--------------')
sanders_influence[['influence', 'polarity', 'influenced_polarity', 'user_name', 'text', 'created_at']].head(5)

# LANGUAGE
# display who are all twitter from different languages
print('')
print('Language')
lang_group = df.groupby(['candidate', 'lang'])
print(lang_group.size())

# graph the languages
print('')
l_lang = lang_group.filter(lambda group: len(group) > 10)

# get rid of english language
non_eng = l_lang[l_lang.lang != 'en']
non_eng_grp = non_eng.groupby(['lang', 'candidate'], as_index = False)
non_eng_grp

print('')
print('ploting...')
s = non_eng_grp.text.agg(np.size)
s = s.rename(columns={'text': 'count'})
s_pivot_dis = s.pivot_table(index='lang', columns='candidate', values='count', fill_value=0)

plot = sns.heatmap(s_pivot_dis)
plot.set_title('Number of non-English Tweets by Candidate')
plot.set_ylabel('language code')
plot.set_xlabel('candidate')
plot.figure.set_size_inches(12, 7)
print('')
print('ending plotting')

# Time-influence polarity over time for each candidate
mean_pol = df.groupby(['candidate', 'created_at']).influenced_polarity.mean()
plot = mean_pol.unstack('candidate').resample('60min').plot()
plot.set_title('Influence Polarity Over Time for Candidates')
plot.set_ylabel('Influence Polarity')
plot.set_xlabel('Time')
plot.figure.set_size_inches(15, 9)

# Get top languages
lang_size =df.groupby('lang').size()
th = lang_size.quantile(.75)

top_lang_df = lang_size[lang_size > th]
top_lang = set(top_lang_df.index) - {'und'}
print(top_lang)

# Get tweet frequency
df['hour'] = df.created_at.apply(lambda datetime: datetime.hour)

for lang_code in top_lang:
    l_df = df[df.lang == lang_code]
    normalized_freq = l_df.groupby('hour').size() / l_df.lang.count()
    plot = normalized_freq.plot(label = lang_code)
    
plot.set_title('Tweet Frequency by hour of day')
plot.set_ylabel('frequency')
plot.set_xlabel('hr of day')
plot.legend()
plot.figure.set_size_inches(10, 8)

# find the uniqueness of tweets
spike_interest = df[(df.hour == 23) & (df.lang == 'in')]

print('Number of tweets:', spike_interest.text.count())
print('Number of unique users:', spike_interest.user_name.unique().size)

#investigate spike from Indonesia
spike_interest.text.head(10).unique()

# Find the Timezone of tweets in different locations with Influenced_Polarity
timez_df = english_df.dropna(subset=['user_time_zone'])
us_timez_df = timez_df[timez_df.user_time_zone.str.contains('US & Canada')]
us_timez_candidate_group = us_timez_df.groupby(['candidate', 'user_time_zone'])
us_timez_candidate_group.influenced_polarity.mean()

# Graph timezone on a map
timez_map = cartopy.io.shapereader.Reader("/Users/alanseciwa/Desktop/World_Maps/tz_world_mp.shp")
timez_rec = list(timez_map.records())
timez_trans = {
    'Eastern Time (US & Canada)': 'America/New_York',
    'Central Time (US & Canada)': 'America/Chicago',
    'Mountain Time (US & Canada)': 'America/Denver',
    'Pacific Time (US & Canada)': 'America/Los_Angeles',
}
america_timez_rec = {
    timez_name: next(filter(lambda record: record.attributes['TZID'] == timez_id, timez_rec))
    for timez_name, timez_id 
    in timez_trans.items()
}

# -----
aea = cartopy.crs.AlbersEqualArea(-95, 35)
pc = cartopy.crs.PlateCarree()

state_province = cartopy.feature.NaturalEarthFeature(
    category='cultural',
    name='admin_1_states_provinces_lines',
    scale='50m',