"""
# Amazon Alexa Reviews Analysis
"""
"""
### The aim is to analyse Alexa's reviews by NLP. If the feedback is positive, the result is 1, else it is 0. Using logistic regression, I have tried to classify the feedback as positive or negative.
"""
"""
### Importing Libraries
"""
import pandas as pd
import nltk 
nltk.download('stopwords')                 # download the stopwords from NLTK

import re                                  # library for regular expression operations
import string                              # for string operations

from nltk.corpus import stopwords          # module for stop words that come with NLTK
from nltk.stem import PorterStemmer        # module for stemming
from nltk.tokenize import TweetTokenizer   # module for tokenizing strings

from sklearn.linear_model import LogisticRegression  
from sklearn.feature_extraction.text import CountVectorizer  
from sklearn.model_selection import train_test_split  
from sklearn.metrics import roc_auc_score
from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt            # library for visualization
import seaborn as sns
"""
### Getting our Data
"""
df = pd.read_csv('../input/amazon-alexa/amazon_alexa.csv')
df
"""
### Data Preprocessing
"""
df = df.drop(['rating', 'date', 'variation'], axis = 1)
df
df.isnull().any()  # checking for null values
df.info()
def process_rev(rev):
    """Process review function.
    Input:
        rev: a string containing a review
    Output:
        rev_clean: a list of words containing the processed review

    """
    stemmer = PorterStemmer()
    stopwords_english = stopwords.words('english')
    # tokenize reviews
    tokenizer = TweetTokenizer(preserve_case=False, strip_handles=True,
                               reduce_len=True)
    rev_tokens = tokenizer.tokenize(rev)

    rev_clean = []
    for word in rev_tokens:
        if (word not in stopwords_english and  # remove stopwords
                word not in string.punctuation):  # remove punctuation
            # rev_clean.append(word)
            stem_word = stemmer.stem(word)  # stemming word
            rev_clean.append(stem_word)

    return rev_clean
# using the process_rev function for:
# 1. Removing stop words
# 2. Tokenization
# 3. Stemming
A = []
a = df['verified_reviews']
for i in a:
  i = process_rev(i)
  A.append(i)
df['verified_reviews'] = A
df
"""
### Vectorizing
"""
cv = CountVectorizer(max_features=1500, analyzer='word', lowercase=False) 
df['verified_reviews'] = df['verified_reviews'].apply(lambda x: " ".join(x) )  # to join all words in the lists
X = cv.fit_transform(df['verified_reviews'])  # predictor variable 'X'
df
y = pd.DataFrame(df['feedback'])  # respose variable 'y'
y.head()
"""
### Splitting for Training and Testing
"""
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state = 0)  # splitting in the ratio 80:20
"""
### Model
"""
classifier = LogisticRegression(random_state = 0)
classifier.fit(X_train, y_train)
"""
### Making Predictions
"""
y_pred = classifier.predict(X_test)
y_pred
"""
### Checking Accuracy
"""
roc_auc_score(y_test, y_pred)
"""
# Predictions are 68.25% accurate.
"""
"""
### Results' Visualization
"""
cm = confusion_matrix(y_test, y_pred)
cm
plt.figure(figsize=(6,6))
sns.heatmap(cm, annot=True, fmt=".0f", linewidths=0.5, square = True, cmap = 'Pastel1')
plt.ylabel('Actual label')
plt.xlabel('Predicted label')
all_sample_title = 'Accuracy Score: {0}'.format(roc_auc_score(y_test, y_pred))
plt.title(all_sample_title, size = 15)