forked from nishantrathi100/Yelp-Data-Mining
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SentimentAnalysis.py
37 lines (29 loc) · 1.06 KB
/
SentimentAnalysis.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
from time import time
from sklearn.feature_extraction.text import TfidfVectorizer, CountVectorizer
from sklearn.decomposition import NMF, LatentDirichletAllocation
from sklearn.naive_bayes import MultinomialNB
def transformRating(rating):
transformed=list()
for r in rating:
if(r<3.5):
transformed.append(1)
else:
transformed.append(5)
return transformed
#transformRatings=transformRating(rating)
def predictSentiment(train_reviews,review,ratings):
n_features = 1000
tfidf_vectorizer = TfidfVectorizer(
max_features=n_features,
stop_words='english')
tratings = transformRating(ratings)
print train_reviews
sentimentXtrain = tfidf_vectorizer.fit_transform(train_reviews)
lst = []
lst.append(review)
sentimentXtest = tfidf_vectorizer.transform(lst)
#sentimentYtrain = transformRating
classifier = MultinomialNB()
clf = classifier.fit(sentimentXtrain, tratings)
preds = clf.predict(sentimentXtest)
return preds