-
Notifications
You must be signed in to change notification settings - Fork 477
/
Main.py
416 lines (350 loc) · 13.7 KB
/
Main.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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
from __future__ import division
from __future__ import print_function
import re, string, timeit
from nltk.stem.porter import *
from nltk.corpus import stopwords
from nltk import FreqDist
from nltk.stem.porter import *
import json
import os
import nltk
import numpy as np
from builtins import str
from builtins import range
from past.utils import old_div
from builtins import object
import math
from tqdm import tqdm
stemmer = PorterStemmer()
def createSentences(content,stopwords):
sent_word=[]
sentences = nltk.sent_tokenize(content)
for sent in sentences:
words = nltk.word_tokenize(sent)
temp = [stemmer.stem(w.lower()) for w in words if w not in string.punctuation]
temp2 = [v for v in temp if v not in stopwords]
if len(temp2)>0:
sent_word.append(temp2)
return sent_word
class createVocabularyFile:
def create_stopwords(self):
init_stopwords = [stemmer.stem(v) for v in stopwords.words('english')]
additional_stopwords = ["'s","...","'ve","``","''","'m",'--',"'ll","'d"]
self.stopwords = additional_stopwords + init_stopwords
def createVocab(self):
All_Contents = []
i=0
for hotel in self.corpus:
print("File ."+str(i+1))
for review in hotel.get("Reviews"):
s= []
for v in createSentences(review.get('Content'),self.stopwords):
s = v + s
All_Contents = All_Contents + s
i=i+1
term_freq = FreqDist(All_Contents)
Vocab = []
Count = []
VocabDict={}
for k,v in term_freq.items():
if v>5:
Vocab.append(k)
Count.append(v)
self.Vocab = np.array(Vocab)[np.argsort(Vocab)].tolist()
self.Count = np.array(Count)[np.argsort(Vocab)].tolist()
self.VocabDict = dict(zip(self.Vocab,range(len(self.Vocab))))
stemmer = PorterStemmer()
def sentencesWithVocab(content,Vocab,VocabDict):
sent_word=[]
try:
sentences = nltk.sent_tokenize(content)
except:
print('unable to tokenize: ' + content)
try:
for sent in sentences:
words = nltk.word_tokenize(sent)
temp = [stemmer.stem(w.lower()) for w in words if stemmer.stem(w.lower()) in Vocab]
temp2 = [VocabDict.get(w) for w in temp]
if len(temp2)>0:
sent_word.append(temp2)
except:
print('unable to stem: ' + content)
return sent_word
class Analyzer(object):
def __init__(self, analyze, Vocab, VocabDict):
self.analyze = FreqDist(analyze)
self.unilength = len(self.analyze)
self.label = -1 # initialize the label
class Review(object):
def __init__(self, revData, Vocab, VocabDict):
self.all = revData.get("all")
self.revID = revData.get("revID")
self.User = revData.get("User")
self.Date = revData.get("Date")
content = revData.get("Content")
#self.Content = revData.get("text")
# print("Review")
# print("*******************************************************************************")
# print(content)
# print()
# print()
try:
anayzedWord = sentencesWithVocab(content,Vocab,VocabDict)
self.Analyzers = [Analyzer(analyze, Vocab, VocabDict) for analyze in anayzedWord]
UniWord = {}
for analyze in self.Analyzers:
UniWord = list(UniWord) + list(analyze.analyze.keys())
self.UniWord = np.array([w for w in UniWord])
self.UniWord.sort()
self.NumOfUniWord = len(self.UniWord)
except:
pass
def labelreview(self):
self.revlabel = -1
for analyze in self.Analyzers:
if analyze.label != -1:
self.revlabel = 1
break
def analyzeDetails(self):
self.NumOfAnnotatedAnalyzers = 0
for analyze in self.Analyzers:
if analyze.label != -1:
self.NumOfAnnotatedAnalyzers = self.NumOfAnnotatedAnalyzers + 1
class Restaurant(object):
def __init__(self, rest_data, Vocab, VocabDict):
self.RestaurantID = rest_data.get('RestaurantInfo').get('RestaurantID')
self.Name = rest_data.get('RestaurantInfo').get('Name')
self.Address = rest_data.get('RestaurantInfo').get('Address')
self.Reviews = [Review(review, Vocab,VocabDict) for review in rest_data.get("Reviews") ]
self.NumOfReviews = len(self.Reviews)
def Calc_annotated_Reviews(self):
self.NumOfAnnotatedReviews = 0
for review in self.Reviews:
if review.revlabel != -1:
self.NumOfAnnotatedReviews = self.NumOfAnnotatedReviews + 1
def sent_aspect_match(analyze,aspects):
count = np.zeros(len(aspects))
i=0
for a in aspects:
for w in list(analyze.analyze.keys()):
if w in a:
count[i]=count[i]+1
i=i+1
return count
class Corpus(object):
def __init__(self, corpus, Vocab, Count, VocabDict):
self.Vocab = Vocab
self.VocabDict = VocabDict
self.VocabTF = Count
self.V = len(Vocab)
self.Aspect_Terms = []
self.Restaurants = [Restaurant(rest, Vocab, VocabDict) for rest in tqdm(corpus)]
self.NumOfRestaurants = len(corpus)
def listCombine(lists):
L=[]
for l in lists:
L=L+l
return L
def ChisqTest(N, taDF, tDF, aDF):
A = taDF
B = tDF - A
C = aDF - A
D = N - A - B - C
return N * ( A * D - B * C ) * ( A * D - B * C ) / aDF / ( B + D ) / tDF / ( C + D )
def statReview(review,aspect,Vocab):
K = len(aspect)
try:
review.num_analyze_aspect_word = np.zeros((K,review.NumOfUniWord))
review.num_analyze_aspect = np.zeros(K)
review.num_anayzedWord = np.zeros(review.NumOfUniWord)
review.num_analyze = 0
except:
pass
try:
for analyze in review.Analyzers:
if analyze.label != -1:
review.num_analyze = review.num_analyze + 1
for l in analyze.label:
review.num_analyze_aspect[l] = review.num_analyze_aspect[l] + 1
for w in list(analyze.analyze.keys()):
z = np.where(w == review.UniWord)[0]
review.num_anayzedWord[z] = review.num_anayzedWord[z] +1
for l in analyze.label:
for w in list(analyze.analyze.keys()):
z = np.where(w == review.UniWord)[0]
review.num_analyze_aspect_word[l,z] = review.num_analyze_aspect_word[l,z]+1
except:
pass
class bootStrap(object):
def sentence_label(self,corpus):
if len(self.Aspect_Terms)>0:
for rest in corpus.Restaurants:
for review in rest.Reviews:
for analyze in review.Analyzers:
count=sent_aspect_match(analyze,self.Aspect_Terms)
if max(count)>0:
s_label = np.where(np.max(count)==count)[0].tolist()
analyze.label = s_label
else:
pass
def chiSquare(self,corpus):
K=len(self.Aspect_Terms)
V=len(corpus.Vocab)
corpus.all_num_analyze_aspect_word = np.zeros((K,V))
corpus.all_num_analyze_aspect = np.zeros(K)
corpus.all_num_anayzedWord = np.zeros(V)
corpus.all_num_analyze = 0
Chi_sq = np.zeros((K,V))
if K>0:
for rest in corpus.Restaurants:
for review in rest.Reviews:
try:
statReview(review,self.Aspect_Terms,corpus.Vocab)
corpus.all_num_analyze = corpus.all_num_analyze + review.num_analyze
corpus.all_num_analyze_aspect = corpus.all_num_analyze_aspect + review.num_analyze_aspect
except:
pass
try:
for w in review.UniWord:
z = np.where(w == review.UniWord)[0][0] # index, since the matrix for review is small
corpus.all_num_anayzedWord[w] = corpus.all_num_anayzedWord[w] + review.num_anayzedWord[z]
corpus.all_num_analyze_aspect_word[:,w] = corpus.all_num_analyze_aspect_word[:,w] + review.num_analyze_aspect_word[:,z]
except:
pass
for k in range(K):
try:
for w in range(V):
Chi_sq[k,w] = ChisqTest(corpus.all_num_analyze, corpus.all_num_analyze_aspect_word[k,w], corpus.all_num_anayzedWord[w], corpus.all_num_analyze_aspect[k])
except:
pass
self.Chi_sq = Chi_sq
else:
pass
def loadAspect(analyzer,filepath,VocabDict):
analyzer.Aspect_Terms=[]
f = open(filepath, "r")
for line in f:
aspect = [VocabDict.get(stemmer.stem(w.strip().lower())) for w in line.split(",")]
analyzer.Aspect_Terms.append(aspect)
f.close()
print("Aspect Keywords loading completed")
def genAspect(analyzer, p, NumIter,c):
for i in range(NumIter):
analyzer.sentence_label(c)
analyzer.chiSquare(c)
t=0
for cs in analyzer.Chi_sq:
x = cs[np.argsort(cs)[::-1]] # descending order
y = np.array([not math.isnan(v) for v in x], dtype=np.bool) # return T of F, force boolean
words = np.argsort(cs)[::-1][y] #
aspect_num = 0
for w in words:
if w not in listCombine(analyzer.Aspect_Terms):
analyzer.Aspect_Terms[t].append(w)
aspect_num = aspect_num +1
if aspect_num > p:
break
t=t+1
print("Iteration " + str(i+1) +"/"+str(NumIter))
def save_Aspect_Keywords_to_file(analyzer,filepath,Vocab):
try:
f = open(filepath, 'w')
for aspect in analyzer.Aspect_Terms:
for w in aspect:
try:
f.write(Vocab[w]+", ")
except:
pass
f.write("\n\n")
f.close()
except:
pass
def genW_Review(analyzer,review,corpus):
try:
Nd = len(review.UniWord)
K=len(analyzer.Aspect_Terms)
review.W = np.zeros((K,Nd))
for k in range(K):
for w in range(Nd):
sum_row = sum(review.num_analyze_aspect_word[k])
if sum_row > 0:
review.W[k,w] = old_div(review.num_analyze_aspect_word[k,w],sum_row)
except:
print('unable to create W matrix for review')
def genW(analyzer,corpus):
rest_num=0
for rest in corpus.Restaurants:
print("Creating W matrix for Restaurant "+str(rest_num+1))
for review in rest.Reviews:
genW_Review(analyzer,review,corpus)
rest_num= rest_num+1
def genRating(analyzer,corpus,outputfolderpath):
dir = outputfolderpath
if not os.path.exists(dir):
os.makedirs(dir)
vocabfile = outputfolderpath+"vocab1.txt"
f = open(vocabfile,"w")
for w in corpus.Vocab:
try:
f.write(w.encode('utf8', 'replace') + ",")
except:
pass
f.close()
reviewfile = outputfolderpath + "revData.txt"
f = open(reviewfile, 'w')
for rest in corpus.Restaurants:
for review in rest.Reviews:
print(review)
try:
f.write(rest.RestaurantID)
f.write(":")
f.write(review.all)
f.write(":")
f.write(str(review.UniWord.tolist()))
f.write(":")
f.write(str(review.W.tolist()))
f.write("\n")
except:
print('unable to produce data for rating for review')
f.close()
def Stats(corpus):
TotalNumOfRest = corpus.NumOfRestaurants
TotalNumOfReviews = 0
TotalNumOfAnnotatedReviews = 0
AnalyzersperReviewList = []
for rest in corpus.Restaurants:
TotalNumOfReviews = TotalNumOfReviews + rest.NumOfReviews
for review in rest.Reviews:
try:
review.analyzeDetails()
AnalyzersperReviewList.append(review.NumOfAnnotatedAnalyzers)
review.labelreview() # -1 or 1
TotalNumOfAnnotatedReviews = TotalNumOfAnnotatedReviews + 1
except:
print('unable to calc summary stats for review.')
AnalyzersperReviewList = np.array(AnalyzersperReviewList)
m = np.mean(AnalyzersperReviewList)
sd = np.std(AnalyzersperReviewList)
print("TotalNumOfRest =" + str(TotalNumOfRest) +"\n")
print("TotalNumOfReviews =" + str(TotalNumOfReviews) +"\n")
print("TotalNumOfAnnotatedReviews =" + str(TotalNumOfAnnotatedReviews) +"\n")
print("AnalyzersperReview=" + str(m) + "+-" + str(sd) + "\n")
def main():
cv_obj = createVocabularyFile()
loadfilepath = "./data/yelp_mp1_corpus.npy"
(cv_obj.corpus,cv_obj.Vocab,cv_obj.Count,cv_obj.VocabDict)=np.load(loadfilepath,allow_pickle=True)
# otherwise
data = Corpus(cv_obj.corpus, cv_obj.Vocab, cv_obj.Count,cv_obj.VocabDict)
BSanalyzer = bootStrap()
loadfilepath = "./init_aspect_word.txt"
loadAspect(BSanalyzer,loadfilepath,cv_obj.VocabDict)
#### expand aspect keywords
genAspect(BSanalyzer, 5, 5, data)
savefilepath = './output/final_aspect_words.txt'
save_Aspect_Keywords_to_file(BSanalyzer,savefilepath,cv_obj.Vocab)
# genW(BSanalyzer,data)
# W_outputfolderpath = "./output/"
# genRating(BSanalyzer,data,W_outputfolderpath)
# Stats(data)
if __name__ == "__main__":
main()