-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdescribe_reviews.py
317 lines (221 loc) · 9.29 KB
/
describe_reviews.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
import json
import sys
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
from scipy import stats
from textblob import TextBlob
from textstat.textstat import textstat
sns.set(color_codes=True)
np.random.seed(sum(map(ord, "distributions")))
def load_data(app_id):
# Data folder
data_path = "data/"
json_filename = "review_" + app_id + ".json"
data_filename = data_path + json_filename
with open(data_filename, encoding="utf8") as in_json_file:
review_data = json.load(in_json_file)
return review_data
def describe_data(review_data):
try:
query_summary = review_data['query_summary']
sentence = 'Number of reviews: {0} ({1} up ; {2} down)'
sentence = sentence.format(
query_summary["total_reviews"],
query_summary["total_positive"],
query_summary["total_negative"],
)
except KeyError:
query_summary = None
sentence = 'Query summary cannot be found in the JSON file.'
print(sentence)
reviews = list(review_data['reviews'].values())
sentence = 'Number of downloaded reviews: ' + str(len(reviews))
print(sentence)
return query_summary, reviews
def aggregate_reviews(app_id):
review_data = load_data(app_id)
(_, reviews) = describe_data(review_data)
review_stats = {}
##
# Review ID
review_stats['recommendationid'] = []
# Meta-data regarding the reviewers
review_stats['num_games_owned'] = []
review_stats['num_reviews'] = []
review_stats['playtime_forever'] = []
# Meta-data regarding the reviews themselves
review_stats['language'] = []
review_stats['voted_up'] = []
review_stats['votes_up'] = []
review_stats['votes_funny'] = []
review_stats['weighted_vote_score'] = []
review_stats['comment_count'] = []
review_stats['steam_purchase'] = []
review_stats['received_for_free'] = []
# Stats regarding the reviews themselves
review_stats['character_count'] = []
review_stats['syllable_count'] = []
review_stats['lexicon_count'] = []
review_stats['sentence_count'] = []
review_stats['difficult_words_count'] = []
review_stats['flesch_reading_ease'] = []
review_stats['dale_chall_readability_score'] = []
# Sentiment analysis
review_stats['polarity'] = []
review_stats['subjectivity'] = []
##
for review in reviews:
review_content = review['review']
# Review ID
review_stats['recommendationid'].append(review["recommendationid"])
# Meta-data regarding the reviewers
review_stats['num_games_owned'].append(review['author']['num_games_owned'])
review_stats['num_reviews'].append(review['author']['num_reviews'])
review_stats['playtime_forever'].append(review['author']['playtime_forever'])
# Meta-data regarding the reviews themselves
review_stats['language'].append(review['language'])
review_stats['voted_up'].append(review['voted_up'])
review_stats['votes_up'].append(review['votes_up'])
review_stats['votes_funny'].append(review['votes_funny'])
review_stats['weighted_vote_score'].append(review['weighted_vote_score'])
review_stats['comment_count'].append(review['comment_count'])
review_stats['steam_purchase'].append(review['steam_purchase'])
review_stats['received_for_free'].append(review['received_for_free'])
# Stats regarding the reviews themselves
review_stats['character_count'].append(len(review_content))
review_stats['syllable_count'].append(textstat.syllable_count(review_content))
review_stats['lexicon_count'].append(textstat.lexicon_count(review_content))
review_stats['sentence_count'].append(textstat.sentence_count(review_content))
review_stats['difficult_words_count'].append(
textstat.difficult_words(review_content),
)
try:
review_stats['flesch_reading_ease'].append(
textstat.flesch_reading_ease(review_content),
)
except TypeError:
review_stats['flesch_reading_ease'].append(None)
review_stats['dale_chall_readability_score'].append(
textstat.dale_chall_readability_score(review_content),
)
# Sentiment analysis
blob = TextBlob(review_content)
review_stats['polarity'].append(blob.sentiment.polarity)
review_stats['subjectivity'].append(blob.sentiment.subjectivity)
return review_stats
def aggregate_reviews_to_pandas(app_id):
review_stats = aggregate_reviews(app_id)
df = pd.DataFrame(data=review_stats)
# Correction for an inconsistency which I discovered when running df.mean(). These 2 columns did not appear in the
# output of mean(). I don't think it has any real impact for clustering and other purposes, but just to be sure...
if "comment_count" in df.columns:
df["comment_count"] = df["comment_count"].astype('int')
if "weighted_vote_score" in df.columns:
df["weighted_vote_score"] = df["weighted_vote_score"].astype('float')
return df
def find_top_languages_by_review_number(df, num_top_languages=3, verbose=True):
# Extract a dataframe for reviews written in top languages (by review numbers)
sorted_languages = df["language"].value_counts().index.tolist()
top_languages = sorted_languages[0:num_top_languages]
if verbose:
print(top_languages)
print(df["language"].describe())
return top_languages
def extract_reviews_for_top_languages_only(df, top_languages, verbose=True):
# Extract a dataframe for reviews written in top languages (by review numbers)
s = pd.Series([lang in top_languages for lang in df["language"]], name='language')
df_extracted = df[s.values]
if verbose:
print(df_extracted.groupby("language").mean())
return df_extracted
def plot_univariate_distribution(data_frame, str_x="votes_up"):
# Reference: https://seaborn.pydata.org/tutorial/distributions.html
sns.distplot(data_frame[str_x], kde=False, fit=stats.lognorm)
plt.show()
return
def plot_box_plot(data_frame, str_x="language", str_y="votes_up"):
# Reference: https://seaborn.pydata.org/examples/grouped_boxplot.html
sns.boxplot(x=str_x, y=str_y, data=data_frame, palette="PRGn")
plt.show()
# NB: Discriminating between positive and negative reviews (with "voted_up") is not super useful in our case,
# since we will only consider games with mostly positive reviews.
return
def analyze_app_id(app_id, languages_to_extract=None, create_separate_plots=True):
df = aggregate_reviews_to_pandas(app_id)
num_top_languages = 3
if languages_to_extract is None:
top_languages = find_top_languages_by_review_number(df, num_top_languages)
else:
top_languages = languages_to_extract
df_extracted = extract_reviews_for_top_languages_only(df, top_languages)
# All the possible variables are listed here:
variables = df_extracted.keys()
print(variables)
if create_separate_plots:
variable_to_plot = "lexicon_count"
plot_univariate_distribution(df_extracted, variable_to_plot)
if num_top_languages > 1:
plot_box_plot(df_extracted, "language", variable_to_plot)
return df_extracted
def analyze_app_id_in_english(app_id):
df = analyze_app_id(app_id, ['english'], False)
return df
def get_review_content(app_id, review_id):
data = load_data(app_id)
reviews = list(data['reviews'].values())
review_content = "-1"
for review in reviews:
if review['recommendationid'] == review_id:
review_content = review['review']
break
return review_content
def plot_overlays_of_univariate_distribution(
app_id_list,
variable_to_plot="lexicon_count",
languages_to_extract=None,
):
# By definition, we want to overlay plots with this function, hence the following variable is set to False:
if languages_to_extract is None:
languages_to_extract = ['english']
create_separate_plots = False
current_palette = sns.color_palette(n_colors=len(app_id_list))
for (iter_count, appID) in enumerate(app_id_list):
print(appID)
df = analyze_app_id(appID, languages_to_extract, create_separate_plots)
sns.distplot(
df[variable_to_plot],
kde=False,
fit=stats.beta,
color=current_palette[iter_count],
label=appID,
fit_kws={
"label": appID + " fit",
"color": current_palette[iter_count],
"alpha": 0.25,
},
)
plt.legend()
plt.show()
return
def main(argv):
app_id_list = ["723090", "639780", "573170"]
if len(argv) == 0:
app_id = app_id_list[-1]
print("No input detected. AppID automatically set to " + app_id)
compare_app_ids_in_default_list = True
else:
app_id = argv[0]
print("Input appID detected as " + app_id)
compare_app_ids_in_default_list = False
# Analyze one appID
analyze_app_id(app_id)
# Compare different appIDs
if compare_app_ids_in_default_list:
plot_overlays_of_univariate_distribution(app_id_list)
return True
if __name__ == "__main__":
main(sys.argv[1:])