# -*- coding: utf-8 -*-
"""Assignment10.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1dB3vg8F1SiE1CLgoumFAJbsPCvwRmYwb
"""

import pandas
from textblob import TextBlob
import re
import matplotlib.pyplot as plt

def clean_tweet(tweet):
    '''
    Utility function to clean tweet text by removing links, special characters
    using simple regex statements.
    '''
    return ' '.join(re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z \t])|(\w+:\/\/\S+)", " ", tweet).split())
  
def get_tweet_sentiment(tweet):
    '''
    Utility function to classify sentiment of passed tweet
    using textblob's sentiment method
    '''
    # create TextBlob object of passed tweet text
    analysis = TextBlob(clean_tweet(tweet))
    # set sentiment
    if analysis.sentiment.polarity > 0:
        return 'positive'
    elif analysis.sentiment.polarity == 0:
        return 'neutral'
    else:
        return 'negative'

def determine_sentiment(df):
    sentiments = list()
    for i in range(len(df)):
#         blob = TextBlob(df[i])
        sentiments.append(get_tweet_sentiment(df[i]))
    newFrame = df.to_frame()
    newFrame['Sentiment'] = sentiments
    return newFrame

def analyze(fpath):
    df = pandas.read_csv(fpath)
    tweetText = df["text"]
    sentiments = determine_sentiment(tweetText)
    print(sentiments[0:30])
    numPos = 0
    numNeu = 0
    numNeg = 0
    for i in range(len(sentiments)):
        if sentiments["Sentiment"][i] == "positive": numPos += 1
        if sentiments["Sentiment"][i] == "neutral":  numNeu += 1
        if sentiments["Sentiment"][i] == "negative": numNeg += 1
    labels = 'Positive', 'Neutral', 'Negative'
    sizes  = [numPos, numNeu, numNeg]
    plt.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=90)
    plt.axis('equal')
    plt.show()