-
Notifications
You must be signed in to change notification settings - Fork 0
/
linking_and_metrics.py
228 lines (189 loc) · 8.83 KB
/
linking_and_metrics.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
"""Linking Metadata and Review Data document into pandas dataframe"""
import os
import sys
import json
import re
import metadata
import jsonReviewRead_vBlackfyre
import reviewdataNB
import nltk
import datetime
import readability
from pycorenlp import StanfordCoreNLP
import pickle
import statistics
def main():
review_list = []
metadata_dictionary = {}
vote_thresh = 10
text_thresh = 10
category = 'Grocery_and_Gourmet_Food'
if os.path.isfile(category + '_review_list_metadata' + '.pkl'):
with open(category + '_review_list_metadata' + '.pkl', 'rb') as f:
review_list, metadata_dictionary = pickle.load(f)
else:
if category is not 'Apps_for_Android':
review_list = jsonReviewRead_vBlackfyre.readInReviewRaw('reviews_' + category + '.json', '', vote_thresh, text_thresh)
else:
review_list = reviewdataNB.readInReviewRaw('reviews_Apps_for_Android.json', '', vote_thresh, text_thresh)
metadata_dictionary = metadata.readInMetadata('meta_' + category + '.json')
with open(category + '_review_list_metadata' + '.pkl', 'wb') as f:
pickle.dump([review_list, metadata_dictionary], f)
# reviewerID -> list of products reviewed
productList_dict = {}
# reviewerID -> averageReviewRating [favorable rating number ratings]
hahaha = {}
nlp = StanfordCoreNLP('http://localhost:9000')
# reviewerID -> average a
averageSentiment_dict = {}
# productID -> average product rating
averageRating_dict = {}
favorableRatingList = []
for review in review_list:
favorableRatingList.append(review.favorableRating)
ratio_threshold = statistics.median(favorableRatingList)
print("Ratio Threshold Used + Median Favorable Rating: " + str(ratio_threshold))
counter = 0
print("Number of reviews to be featurized: " + str(len(review_list)))
for review in review_list:
# set review age
if category is not 'Apps_for_Android':
d0 = datetime.datetime.fromtimestamp(int(review.unixReviewTime)).date()
d1 = datetime.date(2018, 4, 16)
delta = d1 - d0
review.reviewAge = delta.days
if review.favorableRating >= ratio_threshold:
review.helpfulLabel = 1
else:
review.helpfulLabel = 0
person = review.reviewerID
if person in metadata_dictionary:
review.reviewedAlsoBought = metadata_dictionary[person].reviewAlsoBought
review.reviewedAlsoViewed = metadata_dictionary[person].reviewedAlsoViewed
review.reviewedAlsoBoughtTogether = metadata_dictionary[person].reviewedAlsoBoughtTogether
counter+=1
# build the reviewer to product list dictionary
if person not in productList_dict:
productList_dict[person] = {review.productID: 1}
else:
productList_dict[person][review.productID] = 1
# build the averageRating dictionary
if review.productID not in averageRating_dict:
averageRating_dict[review.productID] = {'avg': review.overallRating, 'num': 1}
else:
averageRating_dict[review.productID]['num'] += 1
averageRating_dict[review.productID]['avg'] = float(averageRating_dict[review.productID]['avg'] + review.overallRating) / float(averageRating_dict[review.productID]['num'])
if review.reviewerID in hahaha:
denominator = hahaha[review.reviewerID][1]
numerator = hahaha[review.reviewerID][0]
hahaha[review.reviewerID] = [float(numerator + review.favorableRating)/float(denominator + 1), (denominator + 1)]
else:
hahaha[review.reviewerID] = [float(review.favorableRating), 1]
for review in review_list:
productsReviewed = productList_dict[review.reviewerID]
alsoBoughtC = 0
alsoViewedC = 0
boughtTogetherC = 0
for product in productsReviewed:
if product in metadata_dictionary[review.productID].alsoBought:
boughtTogetherC += 1
if product in metadata_dictionary[review.productID].alsoViewed:
alsoViewedC += 1
if product in metadata_dictionary[review.productID].boughtTogether:
alsoBoughtC += 1
review.reviewedAlsoBought = alsoBoughtC
review.reviewedAlsoViewed = alsoViewedC
review.reviewedAlsoBoughtTogether = boughtTogetherC
review.NumberOfUserReviews = hahaha[review.reviewerID][1]
review.AverageReviewRating = hahaha[review.reviewerID][0]
review.reviewDeviationFromMean = float(review.overallRating)/float(averageRating_dict[review.productID]['avg'])
review.reviewProductMean = averageRating_dict[review.productID]['avg']
print("Metadata + Reviewer metrics calculated")
if os.path.isfile(category + '_final_review_list' + '.pkl'):
with open(category + '_final_review_list' + '.pkl', 'rb') as f:
review_list = pickle.load(f)
else:
for counter, review in enumerate(review_list):
dummyText = review.reviewText
dummyText = dummyText.split()
review.reviewLength = len(dummyText)
review.numberQuestionMarks = review.reviewText.count('?')
review.numberExclamationPoints = review.reviewText.count('!')
review.numberOfPunctuations = review.numberQuestionMarks + review.numberExclamationPoints + review.reviewText.count(
'.')
STOPWORDS = open('stopwords.txt', 'r')
STOPWORDS = STOPWORDS.read()
STOPWORDS = STOPWORDS.split()
STOPWORDS_DICT = {}
for word in STOPWORDS:
STOPWORDS_DICT[word] = 1
stopwordCount = 0
for word in dummyText:
if word in STOPWORDS_DICT:
stopwordCount += 1
review.numberStopWords = stopwordCount
output = nlp.annotate(review.reviewText, properties={
'annotators': 'tokenize,ssplit,pos',
'outputFormat': 'json'
})
try:
numSentances = len(output["sentences"])
numWords = 0
jajaja = 0
merryChristmas = 0
noun = "NN"
nounCount = 0
pronoun = "PRP"
pronounCount = 0
active = "VBZ"
activeCount = 0
passive = "VBD"
passiveCount = 0
adj = "JJ"
adjCount = 0
title = "NNP"
titleCount = 0
for sent in output["sentences"]:
jajaja += len(sent["tokens"])
for tok in sent["tokens"]:
numWords += 1
merryChristmas += len(tok["word"])
if tok["pos"] == noun:
nounCount += 1
if tok["pos"] == pronoun:
pronounCount += 1
if tok["pos"] == active:
activeCount += 1
if tok["pos"] == passive:
passiveCount += 1
if tok["pos"] == adj:
adjCount += 1
if tok["pos"] == title:
titleCount += 1
review.namedEntities = titleCount
review.numberNouns = nounCount
review.numberPassiveVerbs = passiveCount
review.numberActiveVerbs = activeCount
review.numberAdjectives = adjCount
review.numberPronous = pronounCount
review.averageSentanceLength = float(jajaja) / float(numSentances)
review.averageWordLength = float(merryChristmas) / float(numWords)
results = readability.getmeasures(review.reviewText, lang='en')
allReviewScore = results['readability grades']['FleschReadingEase'] + \
results['readability grades']['Kincaid'] + results['readability grades']['ARI'] + \
results['readability grades']['Coleman-Liau'] + results['readability grades']['GunningFogIndex'] + \
results['readability grades']['LIX'] + \
results['readability grades']['SMOGIndex'] + results['readability grades']['RIX']
review.readability = allReviewScore/float(8)
print("Review Number: " + str(counter))
if category is not 'Apps_for_Android':
print(review.getPrintString())
except Exception as e:
print(e)
with open(category + '_final_review_list' + '.pkl', 'wb') as f:
pickle.dump(review_list, f)
print("Review metrics updated")
if __name__ == '__main__':
print('Main Function Beginning')
main()
print('End of Main')