-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRestaurant Reviews Sentiment Analyser - Deployment.py
63 lines (46 loc) · 1.84 KB
/
Restaurant Reviews Sentiment Analyser - Deployment.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# Importing essential libraries
import pandas as pd
import pickle
# Loading the dataset
df = pd.read_csv('review.tsv', delimiter='\t', quoting=3)
# Importing essential libraries for performing Natural Language Processing on 'Restaurant_Reviews.tsv' dataset
import nltk
import re
nltk.download('stopwords')
from nltk.corpus import stopwords
from nltk.stem.porter import PorterStemmer
# Cleaning the reviews
corpus = []
for i in range(0,1000):
# Cleaning special character from the reviews
review = re.sub(pattern='[^a-zA-Z]',repl=' ', string=df['Review'][i])
# Converting the entire review into lower case
review = review.lower()
# Tokenizing the review by words
review_words = review.split()
# Removing the stop words
review_words = [word for word in review_words if not word in set(stopwords.words('english'))]
# Stemming the words
ps = PorterStemmer()
review = [ps.stem(word) for word in review_words]
# Joining the stemmed words
review = ' '.join(review)
# Creating a corpus
corpus.append(review)
# Creating the Bag of Words model
from sklearn.feature_extraction.text import CountVectorizer
cv = CountVectorizer(max_features=2000)
X = cv.fit_transform(corpus).toarray()
y = df.iloc[:, 1].values
# Creating a pickle file for the CountVectorizer
pickle.dump(cv, open('cv-transform.pkl', 'wb'))
# Model Building
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=0)
# Fitting Naive Bayes to the Training set
from sklearn.naive_bayes import MultinomialNB
classifier = MultinomialNB(alpha=0.2)
classifier.fit(X_train, y_train)
# Creating a pickle file for the Multinomial Naive Bayes model
filename = 'restaurant-sentiment-mnb-model.pkl'
pickle.dump(classifier, open(filename, 'wb'))