# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Predicting sentiment from product reviews
#
# # Fire up GraphLab Create

import graphlab

# # Read some product review data
#
# Loading reviews for a set of baby products. 

products = graphlab.SFrame('/home/rahul/weak3/amazon_baby.gl')

# # Let's explore this data together
#
# Data includes the product name, the review text and the rating of the review. 

products.head()

# # Build the word count vector for each review

products['word_count'] = graphlab.text_analytics.count_words(products['review'])  
#text_analytics is tool box for the text analytics which is used ofr the  data analysis 

products.head()

graphlab.canvas.set_target('ipynb')   #canvas for visuallisation

products['name'].show()

# # Examining the reviews for most-sold product:  '<NAME>'

giraffe_reviews = products[products['name'] == '<NAME>']

len(giraffe_reviews)

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

# # Build a sentiment classifier
# * we talking about thumbs up and down

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


# ## 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.   

# +
#ignore all 3* reviews 

products = products[products['rating'] != 3] 

# -

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

products.head()


# ## Let's train the sentiment classifier

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)

# # Evaluate the sentiment model

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


sentiment_model.show(view='Evaluation')


# # Applying the learned model to understand sentiment for Giraffe

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


giraffe_reviews.head()

# ## Sort the reviews based on the predicted sentiment and explore

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

giraffe_reviews.head()

# ## Most positive reviews for the giraffe

giraffe_reviews[0]['review']

giraffe_reviews[1]['review']

# ## Show most negative reviews for giraffe

giraffe_reviews[-1]['review']

giraffe_reviews[-2]['review']

# # Here we are going to solve Quize2 problem

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

 def select_words(words_count):
    selected_words = ['awesome', 'great', 'fantastic', 'amazing', 'love', 'horrible', 'bad', 'terrible', 'awful', 'wow', 'hate']
    b = {}
    for word in selected_words:
        if word not in words_count: b[word] = 0
        else: b[word] = words_count[word]
    return b

products['awesome'] = products['word_count'].apply(select_words)


products['awesome']

for word in selected_words:
    products[word] = products['word_count'].apply(lambda word_count: word_count[word] if word in word_count else 0)


products

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

features=selected_words

for word in selected_words:
    products[word] = products['word_count'].apply(lambda word_count: word_count[word] if word in word_count else 0)


products


# #  2.Out of the selected_words, which one is most used in the dataset? Which one is least used?

max_count, min_count = 0, 9223372036854775807
max_word, min_word = selected_words[0], selected_words[0]
for word in selected_words:
    total = products[word].sum()
    if total > max_count:
        max_word = word
        max_count = total
    if total < min_count:
        min_word = word
        min_count = total
print('most used: ', max_word, max_count)
print('least used: ', min_word, min_count)


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

selected_words_model = graphlab.logistic_classifier.create(train_data,
                                                     target='sentiment',
                                                     features=selected_words,
                                                     validation_set=test_data)


selected_words_model['coefficients']


# # 3.Out of the 11 words in selected_words, which one got the most positive weight? Which one got the most negative weight? Do these values make sense for you?
#

# +

most_positive = -1
most_positive_word = ''
most_negative = 1
most_negative_word = ''
for row in selected_words_model['coefficients']:
    if row['name'] == '(intercept)': continue
    if row['value']>most_positive:
        most_positive = row['value']
        most_positive_word = row['name']
    if row['value']<most_negative:
        most_negative = row['value']
        most_negative_word = row['name']
print('most positive: ', most_positive_word, most_positive)
print('most negative: ', most_negative_word, most_negative)
# -

sentiment_model.evaluate(test_data)

# +

selected_words_model.evaluate(test_data)
# -

diaper_champ_reviews = products[products['name']=='Baby Trend Diaper Champ']

# +

diaper_champ_reviews
# -

diaper_champ_reviews = diaper_champ_reviews.sort('rating', ascending = False)

diaper_champ_reviews = diaper_champ_reviews.sort('sentiment', ascending = False)

diaper_champ_reviews

selected_words_model.predict(diaper_champ_reviews[0:10], output_type='probability')

sentiment_model.predict(diaper_champ_reviews[0:10], output_type='probability')


len(products[products['sentiment']==1])/float(len(products))



StarcoderdataJupyterScriptsDedupFiltered