import graphlab
import os

"""
Explanation: Sentiment Analyzer for Products
End of explanation
"""


URL = 'https://d396qusza40orc.cloudfront.net/phoenixassets/amazon_baby.csv'

def get_data(filename='amazon_baby.csv', url=URL, force_download=False):
    
    """Download and cache the amazon data
    
    Parameters
    ----------
    filename: string (optional)
        location to save the data
        
    url: string (optional)
    
    force_download: bool (optional)
        if True, force redownload of data
    
    Returns
    -------
    data: graphlab SFrame. Similer to a pandas DataFrame,
        but with capacity for faster analysis of larger data sets
    
    """   
    
    if force_download or not os.path.exists(filename):
        urlretrieve(url, filename)
        
    sf = graphlab.SFrame('amazon_baby.csv')
    
    return sf

products = get_data()
products.head()

"""
Explanation: Read data
End of explanation
"""


products['word_count'] = graphlab.text_analytics.count_words(products['review'])
products.head()

# Set graphs to appear within the notebook
graphlab.canvas.set_target('ipynb')

# Look at products with the most reviews
products['name'].show()

"""
Explanation: Build word count vector for each review
End of explanation
"""


giraffe_reviews = products[products['name']=='Vulli Sophie the Giraffe Teether']
len(giraffe_reviews)

giraffe_reviews['rating'].show(view='Categorical')

"""
Explanation: Take a look at Vulli Sophie, top baby toy according to # of reviews
This is a teether, a toy that babies chew on, in the form of a small giraffe. It's the most gifted baby toy on amazon.
End of explanation
"""


products['rating'].show(view='Categorical')

"""
Explanation: Build a sentiment classifier
Take a deeper look at where the most positive and negative reviews came from
End of explanation
"""


# I consider 3 star ratings neutral, so they will be removed from the dataset
products = products[products['rating'] != 3]

# if rating is 4 or 5, the review is considered positive (1), else negative (0)
products['sentiment'] = products['rating'] >= 4
products.head()

"""
Explanation: Add positive or negative sentiment columns based on rating
1 = Positive, 0 = Negative
End of explanation
"""


# randomly split data set into training data and test data
train_data, test_data = products.random_split(.8, seed=0)
print len(train_data)
print len(test_data)

sentiment_model = graphlab.logistic_classifier.create(train_data,
                                                      target='sentiment',
                                                      features=['word_count'],
                                                      validation_set=test_data)

"""
Explanation: Training the sentiment classifier
End of explanation
"""


# evaluate the model using an roc curve
sentiment_model.evaluate(test_data, metric='roc_curve')

sentiment_model.show(view='Evaluation')

giraffe_reviews['predicted_sentiment'] = sentiment_model.predict(giraffe_reviews, output_type='probability')
giraffe_reviews.head()

"""
Explanation: Evaluate the sentiment model
End of explanation
"""


giraffe_reviews = giraffe_reviews.sort('predicted_sentiment', ascending=False)
giraffe_reviews.head()

# review with highest predicted sentiment
giraffe_reviews[0]['review']

giraffe_reviews[1]['review']

# review with lowest predicted sentiment
giraffe_reviews[-1]['review']

"""
Explanation: Sort the reviews based on predicted sentiment and explore
End of explanation
"""


giraffe_reviews[(giraffe_reviews['rating']>=4) & (giraffe_reviews['predicted_sentiment'] < 0.5)]

giraffe_reviews[(giraffe_reviews['rating']>=4) & (giraffe_reviews['predicted_sentiment'] < 0.5)][0]['review']

"""
Explanation: Reviews with high ratings but low predicted sentiment
End of explanation
"""


giraffe_reviews[(giraffe_reviews['rating']<=2) & (giraffe_reviews['predicted_sentiment'] > 0.5)]

giraffe_reviews[(giraffe_reviews['rating']<=2) & (giraffe_reviews['predicted_sentiment'] > 0.5)][0]['review']

"""
Explanation: Reviews with low ratings but high predicted sentiments
End of explanation
"""


