import graphlab;
graphlab.product_key.set_product_key("7348-CE53-3B3E-DBED-152B-828E-A99E-F303");
                                    

"""
Explanation: Predicting sentiment from product reviews
Fire up GraphLab Create
End of explanation
"""


products = graphlab.SFrame('amazon_baby.gl/')

"""
Explanation: Read some product review data
Loading reviews for a set of baby products.
End of explanation
"""


products.head()

"""
Explanation: Let's explore this data together
Data includes the product name, the review text and the rating of the review.
End of explanation
"""


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

products.head()

graphlab.canvas.set_target('ipynb')

products['name'].show()

"""
Explanation: Build the 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: Examining the reviews for most-sold product:  'Vulli Sophie the Giraffe Teether'
End of explanation
"""


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

"""
Explanation: Build a sentiment classifier
End of explanation
"""


#ignore all 3* reviews
products = products[products['rating'] != 3]

#positive sentiment = 4* or 5* reviews
products['sentiment'] = products['rating'] >=4

products.head()

"""
Explanation: Define what's a positive and a negative sentiment
We will ignore all reviews with rating = 3, since they tend to have a neutral sentiment.  Reviews with a rating of 4 or higher will be considered positive, while the ones with rating of 2 or lower will have a negative sentiment.
End of explanation
"""


train_data,test_data = products.random_split(.8, seed=0)

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

"""
Explanation: Let's train the sentiment classifier
End of explanation
"""


sentiment_model.evaluate(test_data, metric='roc_curve')

sentiment_model.show(view='Evaluation')

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


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

giraffe_reviews.head()

"""
Explanation: Applying the learned model to understand sentiment for Giraffe
End of explanation
"""


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

giraffe_reviews.head()

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


giraffe_reviews[0]['review']

giraffe_reviews[1]['review']

"""
Explanation: Most positive reviews for the giraffe
End of explanation
"""


giraffe_reviews[-1]['review']

giraffe_reviews[-2]['review']

selected_words = ['awesome', 'great', 'fantastic', 'amazing', 'love', 'horrible', 'bad', 'terrible', 'awful', 'wow', 'hate']


products['word_count'] = graphlab.text_analytics.count_words(products['review'])
selected_words = ['awesome', 'great', 'fantastic', 'amazing', 'love', 'horrible', 'bad', 'terrible', 'awful', 'wow', 'hate']

def awesome_count(cell):
    if 'wow' in cell:
        return cell['wow']
    else:
        return 0
products['wow'] = products['word_count'].apply(awesome_count)
products.head()

train_data,test_data = products.random_split(.8, seed=0)
selected_words = ['awesome', 'great', 'fantastic', 'amazing', 'love', 'horrible', 'bad', 'terrible', 'awful', 'wow', 'hate']
selected_words_model = graphlab.logistic_classifier.create(train_data,target='sentiment',features=selected_words,validation_set=test_data, )
selected_words_model['coefficients'].sort('value', ascending = True)



selected_words_model.evaluate(test_data)

"""
Explanation: Show most negative reviews for giraffe
End of explanation
"""


