-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSentiment Analysis of Restaurant Reviews.py
48 lines (39 loc) · 1.67 KB
/
Sentiment Analysis of Restaurant Reviews.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import numpy as np
import pandas as pd
import nltk
import re
nltk.download('stopwords')
from nltk.corpus import stopwords
from nltk.stem.porter import PorterStemmer
import sklearn
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import accuracy_score
import pickle
df = pd.read_csv('Restaurant_Reviews.tsv', delimiter='\t', quoting=3)
df.drop_duplicates(inplace=True)
df.reset_index(drop=True, inplace=True)
corpus = []
ps = PorterStemmer()
for i in range(0,df.shape[0]):
message = re.sub(pattern='[^a-zA-Z]', repl=' ', string=df.Review[i]) #Cleaning special character from the message
message = message.lower() #Converting the entire message into lower case
words = message.split() # Tokenizing the review by words
words = [word for word in words if word not in set(stopwords.words('english'))] #Removing the stop words
words = [ps.stem(word) for word in words] #Stemming the words
message = ' '.join(words) #Joining the stemmed words
corpus.append(message) #Building a corpus of messages
file_name = "corpus.pkl"
pickle.dump(corpus, open(file_name, 'wb'))
cv = CountVectorizer(max_features=1500)
X = cv.fit_transform(corpus).toarray()
y = df.iloc[:, 1].values
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=0)
classifier = MultinomialNB(alpha=0.1)
classifier.fit(X_train, y_train)
file_name = "Restaurant-reviews-model.pkl"
pickle.dump(classifier, open(file_name, 'wb'))
y_pred = classifier.predict(X_test)
acc_s = accuracy_score(y_test, y_pred)*100
print("Accuracy Score {} %".format(round(acc_s,2)))