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

import numpy as np
import pandas as pd
import nltk

data=pd.read_csv("data.csv")

data.head()

data.dropna(inplace=True)

data['title_body']=data['Title']+ ' ' + data['Body']

data.reset_index(inplace=True)
data.drop(['index','Title','Body','Date','Verified Purchase','Helpful Votes'],axis=1,inplace=True)

data[data.Rating==3]

inp,out = np.array(data['title_body']),np.array(data['Rating'])

import string
from nltk.corpus import stopwords
stop = stopwords.words('english') + ['phone','iphone','x','apple','samsung']
for i in range(len(inp)):
    new_doc=''
    for j in range(len(inp[i])):
        if not inp[i][j] in string.punctuation:
            new_doc+=(inp[i][j])
        else:
            new_doc+=' '
    new_doc.strip()
    inp[i]=new_doc.strip()

from nltk import pos_tag
from nltk.stem import WordNetLemmatizer
from nltk.corpus import wordnet
from nltk.tokenize import word_tokenize

# +
lem=WordNetLemmatizer()

def get_simple_tag(treebank_tag):
    if treebank_tag.startswith('J'):
        return wordnet.ADJ
    elif treebank_tag.startswith('V'):
        return wordnet.VERB
    elif treebank_tag.startswith('N'):
        return wordnet.NOUN
    elif treebank_tag.startswith('R'):
        return wordnet.ADV
    else:
        return wordnet.NOUN

def NotNumber(w):
    for ch in w:
        if(ch>='0' and ch<='9'):
            return False
    return True
    
def Clean_Words(doc):
    doc=word_tokenize(doc)
    doc = [word for word in doc if word.lower() not in stop and NotNumber(word)==True]
    pos = pos_tag(doc)
    cleaned_words = [lem.lemmatize(w,pos=get_simple_tag(t)).lower() for w,t in pos]
    string=""
    for w in cleaned_words:
        string+=w
        string+=' '
    return string.strip()


# -

clean_in=np.array([Clean_Words(doc) for doc in inp])
sentiment = ['negative','negative','negative','neutral','positive','positive']
clean_out=np.array([sentiment[int(rating)] for rating in out])

np.array([len(doc.split()) for doc in clean_in]).sum(),np.array([len(doc.split()) for doc in clean_in]).mean()

np.savetxt('Word Clouds/X_neg_clean_reviews.csv',np.array(['text']+[clean_in[i] for i in range(len(clean_in)) if clean_out[i]=='negative']),fmt='%s',delimiter=',')
np.savetxt('Word Clouds/X_neut_clean_reviews.csv',np.array(['text']+[clean_in[i] for i in range(len(clean_in)) if clean_out[i]=='neutral']),fmt='%s',delimiter=',')
np.savetxt('Word Clouds/X_pos_clean_reviews.csv',np.array(['text']+[clean_in[i] for i in range(len(clean_in)) if clean_out[i]=='positive']),fmt='%s',delimiter=',')

from sklearn.model_selection import train_test_split

x_train,x_test,y_train,y_test=train_test_split(clean_in,clean_out)

from sklearn.feature_extraction.text import TfidfVectorizer

vec=TfidfVectorizer(max_features=3000,max_df=0.8,min_df=0.1,ngram_range=(1,3),)

vec.fit(x_train)

x_train=vec.transform(x_train)
x_test=vec.transform(x_test)

from sklearn.naive_bayes import MultinomialNB,GaussianNB, BernoulliNB
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.svm import SVC

clf1 = MultinomialNB()

clf1.fit(x_train,y_train)

y_pred=clf1.predict(x_test)

from sklearn.metrics import classification_report

print(classification_report(y_test,y_pred))
print('Accuracy = ', clf1.score(x_test,y_test)*100,'%')

clf2=BernoulliNB()
clf2.fit(x_train.todense(),y_train)
clf2.score(x_test.todense(),y_test)

y_pred = clf2.predict(x_test.todense())

print(classification_report(y_test,y_pred))
print('Accuracy = ', clf2.score(x_test.todense(),y_test)*100,'%')

clf3 = LogisticRegression()
clf3.fit(x_train,y_train)
clf3.score(x_test,y_test)

y_pred = clf3.predict(x_test)

print(classification_report(y_test,y_pred))
print('Accuracy = ', clf3.score(x_test.todense(),y_test)*100,'%')

clf4 = RandomForestClassifier()
clf4.fit(x_train,y_train)
clf4.score(x_test,y_test)

y_pred=clf4.predict(x_test)

print(classification_report(y_test,y_pred))
print('Accuracy = ', clf4.score(x_test.todense(),y_test)*100,'%')

clf5 = SVC()
clf5.fit(x_train,y_train)
clf5.score(x_test,y_test)

y_pred=clf5.predict(x_test)

print(classification_report(y_test,y_pred))
print('Accuracy = ', clf5.score(x_test.todense(),y_test)*100,'%')

test = 'Three stars'
test_final = Clean_Words(test)
test_data=[]
test_data.append(test_final)
f_t_d=vec.transform(test_data)
clf2.predict(f_t_d)



StarcoderdataJupyterScriptsDedupFiltered