Repository navigation
Expand file tree
/
Copy pathsenti.py
More file actions
94 lines (85 loc) · 2.37 KB
/
Copy pathsenti.py
File metadata and controls
94 lines (85 loc) · 2.37 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
from textblob import TextBlob
from textblob.sentiments import NaiveBayesAnalyzer
import json, pickle, math
def compare(a,b):
c = int(math.floor(a['rating']+a['tripAdvisorRating']*2 - b['rating']-b['tripAdvisorRating']*2))
return c
def load_classifier():
f = open('NB_classifier.pickle')
classifier = pickle.load(f)
f.close()
return classifier
def sentimentAnalysis():
fi = open("reviews.json")
cityHotels = open("city.json")
data = json.load(fi)
hoteldata = json.load(cityHotels)
count = 0
hList = []
for hotel in data:
rcount = data[hotel]['count']
reviews = data[hotel]['reviews']
if data[hotel]['count'] >= 50:
count+=1
bin=[0,0,0,0,0,0,0,0,0,0]
for rev in reviews:
blob = TextBlob(rev)
pol = blob.sentiment.polarity
bin[int(math.ceil((pol+1)*5))-1]+=1
rating = 0
bsum = 0
for i in range(10):
rating += bin[i]*(i+1)
bsum += bin[i]
rating /= float(bsum)
hoteldata[hotel]['rating'] = round(rating,2)
hList.append(hoteldata[hotel])
hList = sorted(hList,cmp=compare)
fo = open("final.json","w")
json.dump(hList,fo)
cityHotels.close()
fi.close()
def getSentimentRating():
fi = open("reviews.json")
data = json.load(fi)
count = 0
for hotel in data:
rcount = data[hotel]['count']
reviews = data[hotel]['reviews']
if data[hotel]['count'] >= 50:
count+=1
bin=[0,0,0,0,0,0,0,0,0,0]
for rev in reviews:
blob = TextBlob(rev)
pol = blob.sentiment.polarity
bin[int(math.ceil((pol+1)*5))-1]+=1
rating = 0
bsum = 0
for i in range(10):
rating += bin[i]*(i+1)
bsum += bin[i]
rating /= float(bsum)
rating = round(rating,2)
fi.close()
return rating
# hotel = hotel.encode('ascii','ignore')
# if hoteldata[hotel]['hotelRating'] == 1:
# star1.append(data[hotel])
# elif hoteldata[hotel]['hotelRating'] == 2:
# star2.append(data[hotel])
# elif hoteldata[hotel]['hotelRating'] == 3:
# star3.append(data[hotel])
# elif hoteldata[hotel]['hotelRating'] == 4:
# star4.append(data[hotel])
# elif hoteldata[hotel]['hotelRating'] == 5:
# star5.append(data[hotel])
# print star3
# text = 'This is a good hotel. had a lot of sex'
# t2 = 'the stay was excellent'
# blob = TextBlob(text, analyzer=NaiveBayesAnalyzer())
# bl = TextBlob(t2)
# # print(blob.sentiment.analyze())
# # print(bl.sentiment.analyze())
# # PatternAnalyzer.analyze(blob)
# print blob.sentiment
# print bl.sentiment