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

# + colab={"base_uri": "https://localhost:8080/"} executionInfo={"elapsed": 3608, "status": "ok", "timestamp": 1647174823842, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "12339607467640853832"}, "user_tz": -480} id="I8K2zJiIMYkD" outputId="40e4c4c4-c770-4abe-ed0e-65819cd8e01e"
pip install vaderSentiment

# + executionInfo={"elapsed": 469, "status": "ok", "timestamp": 1647174880679, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "12339607467640853832"}, "user_tz": -480} id="dJAk0-VyMFvT"
# Import all the python package
import pandas as pd
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
from sklearn import metrics
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
from google.colab import drive


# + executionInfo={"elapsed": 458, "status": "ok", "timestamp": 1647174886999, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "12339607467640853832"}, "user_tz": -480} id="L65u-WL-MFvZ"
# Convert star rating to sentiments
def startosentiment(_star):
    if _star >= 4:
        return 'pos'
    elif _star <= 2:
        return 'neg'
    else:
        return 'neu'

# Use Vader to analyse the sentences and convert the compound score to sentiments
def get_score(analyser, _text):
    vpolarity = analyser.polarity_scores(_text)
    # return vpolarity['compound']
    if vpolarity['compound'] > 0.05:
        return 'pos'
    elif vpolarity['compound'] < -0.05:
        return 'neg'
    else:
        return 'neu'


# + colab={"base_uri": "https://localhost:8080/"} executionInfo={"elapsed": 3030, "status": "ok", "timestamp": 1647174892595, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "12339607467640853832"}, "user_tz": -480} id="sWzsQOmGMFvb" outputId="ef9feb65-921d-4cbf-a69f-6fd9f73867d2"
# Initilize the Vader Sentiment Analyzer
sia = SentimentIntensityAnalyzer()

# Connect to Google drive
drive.mount('/content/drive')

# + executionInfo={"elapsed": 481, "status": "ok", "timestamp": 1647174903696, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "12339607467640853832"}, "user_tz": -480} id="OLmVy-kKNp6q"
# Load the dataset into pandas dataframe
icreviews = pd.read_csv('drive/MyDrive/Colab Notebooks/reviews.csv')

# + colab={"base_uri": "https://localhost:8080/"} executionInfo={"elapsed": 4689, "status": "ok", "timestamp": 1647174909048, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "12339607467640853832"}, "user_tz": -480} id="UNVpx4QRMFvc" outputId="43741ae4-2aed-420c-87fe-1b3fa651fca7"
# Data cleaning - fill 'title' column with blank if it is N/A
icreviews['title'] = icreviews['title'].fillna('')

# Combine title and text to a new column
icreviews['com_titletext'] = icreviews['title'] + " " + icreviews['text']

# Convert start rating to sentiments
icreviews['b_star'] = icreviews.apply(lambda x: startosentiment(x['stars']),axis=1)  

# Perform prediction on newly combined column 'title'+'text' and store the sentiments into new column
icreviews['com_score'] = icreviews.apply(lambda x: get_score(sia, x['com_titletext']),axis=1)

# Compute the confusion matrix
conf1 = metrics.confusion_matrix(icreviews['b_star'], icreviews['com_score'])

# Print the precision and recall, among other metrics
print(metrics.classification_report(icreviews['b_star'], icreviews['com_score'], digits=3))

# + colab={"base_uri": "https://localhost:8080/", "height": 298} executionInfo={"elapsed": 55, "status": "ok", "timestamp": 1647174909050, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "12339607467640853832"}, "user_tz": -480} id="PeLjBIA2MFvd" outputId="65594f6f-73e3-41db-c03f-fb413e50072e"
# Plot the confusion matrix
group_counts = ["{0:0.0f}".format(value) for value in conf1.flatten()]
group_percentages = ["{0:.2%}".format(value) for value in conf1.flatten()/np.sum(conf1)]
labels = [f"{v2}\n{v3}" for v2, v3 in zip(group_counts,group_percentages)]
labels = np.asarray(labels).reshape(3,3)
x_axis_labels = ['neg','neu','pos'] # labels for x-axis
y_axis_labels = ['neg','neu','pos'] # labels for y-axis
sns.heatmap(conf1, xticklabels=x_axis_labels, yticklabels=y_axis_labels, annot=labels, fmt='', cmap="Blues").set_title("Ben & Jerry's")

# + colab={"base_uri": "https://localhost:8080/", "height": 279} executionInfo={"elapsed": 26, "status": "ok", "timestamp": 1647174909051, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "12339607467640853832"}, "user_tz": -480} id="I-0kcFtAMFve" outputId="eef48d95-3f68-40c3-a643-733230b9424a"
# Overview of predicted sentiments by Vader
ax = sns.countplot(x ='com_score', data = icreviews)
ax.set(ylabel='Sentiment count', xlabel='Sentiments')
plt.show()

# + colab={"base_uri": "https://localhost:8080/", "height": 281} executionInfo={"elapsed": 975, "status": "ok", "timestamp": 1647174910005, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "12339607467640853832"}, "user_tz": -480} id="Xhjg8WuNMFvf" outputId="fad4e753-5227-4fdd-df41-2879a0aa1287"
# Sentiment distribution pie plot
icreviews['com_score'].value_counts().plot.pie(autopct='%1.0f%%')
plt.title("Sentiment distribution pie chart")

# + colab={"base_uri": "https://localhost:8080/", "height": 297} executionInfo={"elapsed": 23, "status": "ok", "timestamp": 1647174910006, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "12339607467640853832"}, "user_tz": -480} id="V30QXhffMFvh" outputId="20bdce58-239d-4d3b-e8cc-88d4753bbda2"
# Top 10 ice cream brand sort by number of reviews
df_prod_reviewcount = icreviews['key'].value_counts().head(10).reset_index()
df_prod_isin = icreviews.loc[icreviews['key'].isin(df_prod_reviewcount['index'])]
ax1 = sns.countplot(x ='key', hue='com_score', data = df_prod_isin, order = df_prod_isin['key'].value_counts().index)
ax1.set(ylabel='Sentiment count', xlabel='Ice cream brand')

# + colab={"base_uri": "https://localhost:8080/", "height": 297} executionInfo={"elapsed": 20, "status": "ok", "timestamp": 1647174910006, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "12339607467640853832"}, "user_tz": -480} id="GaGHXfVXMFvh" outputId="b6c7091f-0d80-45b4-a2ef-3357dd16e2e1"
# Top 10 ice cream brand sort by positive review
df_posreview = icreviews.loc[icreviews['com_score'] == 'pos']
df_prod_positivecount = df_posreview['key'].value_counts().head(10).reset_index()
ax2 = sns.barplot(x ='index', y= 'key', data = df_prod_positivecount)
ax2.set(ylabel='Sentiment count', xlabel='Ice cream brand')

# + colab={"base_uri": "https://localhost:8080/", "height": 297} executionInfo={"elapsed": 19, "status": "ok", "timestamp": 1647174910007, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "12339607467640853832"}, "user_tz": -480} id="Z_BAVJyLMFvi" outputId="92dcb06f-c802-49b9-ebfc-0956ef78cb4d"
# Top 10 ice cream brand sort by negative review
dfnegreview = icreviews.loc[icreviews['com_score'] == 'neg']
df_prod_negcount = dfnegreview['key'].value_counts().head(10).reset_index()
ax2 = sns.barplot(x ='index', y= 'key', data = df_prod_negcount)
ax2.set(ylabel='Sentiment count', xlabel='Ice cream brand')

# + colab={"base_uri": "https://localhost:8080/", "height": 297} executionInfo={"elapsed": 906, "status": "ok", "timestamp": 1647174910897, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "12339607467640853832"}, "user_tz": -480} id="BvveyOV7MFvj" outputId="6dd7561c-78b2-4023-a6e6-d4129b199f77"
# Top 10 ice cream brand sort by neutral review
dfneureview = icreviews.loc[icreviews['com_score'] == 'neu']
df_prod_neucount = dfneureview['key'].value_counts().head(10).reset_index()
ax2 = sns.barplot(x ='index', y= 'key', data = df_prod_neucount)
ax2.set(ylabel='Sentiment count', xlabel='Ice cream brand')

# + colab={"base_uri": "https://localhost:8080/", "height": 297} executionInfo={"elapsed": 20, "status": "ok", "timestamp": 1647174910898, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "12339607467640853832"}, "user_tz": -480} id="FUfmuwCSMFvj" outputId="a4f5e99c-bf9c-46fd-de7b-f430eb3d023c"
# Top 10 ice cream brand sort by 4 or 5 stars rating
df_posreview = icreviews.loc[icreviews['b_star'] == 'pos']
df_prod_positivecount = df_posreview['key'].value_counts().head(10).reset_index()
ax2 = sns.barplot(x ='index', y= 'key', data = df_prod_positivecount)
ax2.set(ylabel='Sentiment count', xlabel='Ice cream brand')

# + colab={"base_uri": "https://localhost:8080/", "height": 297} executionInfo={"elapsed": 17, "status": "ok", "timestamp": 1647174910898, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "12339607467640853832"}, "user_tz": -480} id="ZQJJqo9JMFvk" outputId="ea592336-5a27-488e-c6ea-da83540f6614"
# Top 10 ice cream brand sort by 1 or 2 stars rating
dfnegreview = icreviews.loc[icreviews['b_star'] == 'neg']
df_prod_negcount = dfnegreview['key'].value_counts().head(10).reset_index()
ax2 = sns.barplot(x ='index', y= 'key', data = df_prod_negcount)
ax2.set(ylabel='Sentiment count', xlabel='Ice cream brand')

# + colab={"base_uri": "https://localhost:8080/", "height": 297} executionInfo={"elapsed": 15, "status": "ok", "timestamp": 1647174910899, "user": {"displayName": "Qi Ning", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "12339607467640853832"}, "user_tz": -480} id="v2fLMab6MFvl" outputId="92372376-8aee-4dbf-8c26-26846863baea"
# Top 10 ice cream brand sort by 3 stars rating
dfneureview = icreviews.loc[icreviews['b_star'] == 'neu']
df_prod_neucount = dfneureview['key'].value_counts().head(10).reset_index()
ax2 = sns.barplot(x ='index', y= 'key', data = df_prod_neucount)
ax2.set(ylabel='Sentiment count', xlabel='Ice cream brand')

StarcoderdataJupyterScriptsDedupFiltered