-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter_review_data.py
241 lines (167 loc) · 6.5 KB
/
filter_review_data.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
import json
from pathlib import Path
import steamspypi
from langdetect import DetectorFactory, detect, lang_detect_exception
from download_review_data import get_artifact_app_id, load_reviews
def cluster_reviews_by_language(review_dict, verbose=False):
reviews = review_dict['reviews']
if verbose:
print(f'#downloaded reviews = {len(reviews)}')
languages = {}
for review_id in reviews:
language = reviews[review_id]['language']
voted_up = reviews[review_id]['voted_up']
if language not in languages:
languages[language] = {}
for is_upvote in [True, False]:
languages[language][is_upvote] = []
languages[language][voted_up].append(review_id)
return languages
def load_english_reviews(app_id=None, language_str='english', verbose=True):
review_dict = load_reviews(app_id)
languages = cluster_reviews_by_language(review_dict)
english_review_dict = {}
for is_upvote in [True, False]:
try:
for review_id in languages[language_str][is_upvote]:
review_text = review_dict['reviews'][review_id]['review']
review_usefulness = float(
review_dict['reviews'][review_id]['weighted_vote_score'],
)
english_review_dict[review_id] = {}
english_review_dict[review_id]['review'] = review_text
english_review_dict[review_id]['voted_up'] = is_upvote
english_review_dict[review_id][
'weighted_vote_score'
] = review_usefulness
except KeyError:
continue
if verbose:
print(f'Loading {len(english_review_dict)} English reviews.')
return english_review_dict
def get_positive_review_ids(english_review_dict):
review_ids = english_review_dict.keys()
review_ids = filter(lambda x: english_review_dict[x]['voted_up'], review_ids)
return review_ids
def get_negative_review_ids(english_review_dict):
review_ids = english_review_dict.keys()
review_ids = filter(lambda x: not english_review_dict[x]['voted_up'], review_ids)
return review_ids
def filter_out_short_reviews(english_review_dict, length_threshold=150):
review_ids = english_review_dict.keys()
print(
'Filtering out reviews with strictly fewer than {} characters.'.format(
length_threshold,
),
)
review_ids = filter(
lambda x: len(english_review_dict[x]['review']) >= length_threshold,
review_ids,
)
long_english_review_dict = {}
for review_id in review_ids:
long_english_review_dict[review_id] = {}
long_english_review_dict[review_id] = english_review_dict[review_id]
return long_english_review_dict
def get_language_folder_name():
language_folder = 'data/detected_languages/'
Path(language_folder).mkdir(parents=True, exist_ok=True)
return language_folder
def get_language_file_name(app_id=None):
if app_id is None:
app_id = get_artifact_app_id()
file_extension = '.json'
language_file_name = get_language_folder_name() + str(app_id) + file_extension
return language_file_name
def detect_review_language(app_id=None, verbose=False):
english_review_dict = load_english_reviews(app_id)
output_file_name = get_language_file_name(app_id)
try:
with open(output_file_name, encoding='utf8') as f:
detected_languages = json.load(f)
except FileNotFoundError:
detected_languages = {}
DetectorFactory.seed = 0
is_new_review_processed = False
for count, review_id in enumerate(english_review_dict):
if verbose and (count + 1) % 1000 == 0:
print(
'Reviews processed by langdetect: {}/{}.'.format(
count + 1,
len(english_review_dict),
),
)
if review_id in detected_languages:
continue
review_content = english_review_dict[review_id]['review']
is_new_review_processed = True
try:
detected_languages[review_id] = detect(review_content)
except lang_detect_exception.LangDetectException:
detected_languages[review_id] = 'unknown'
print(review_content)
if is_new_review_processed:
with open(output_file_name, 'w', encoding='utf8') as f:
json.dump(detected_languages, f)
return detected_languages
def detect_review_language_for_top_100(verbose=True):
data_request = {}
data_request['request'] = 'top100in2weeks'
data = steamspypi.download(data_request)
app_ids = list(data.keys())
for app_id in app_ids:
_ = detect_review_language(app_id, verbose=verbose)
return
def apply_language_detector():
# Pre-processing: detect languages of reviews for Artifact and for top 100 most played games
# Artifact
detected_languages = detect_review_language(verbose=True)
# Top 100
detect_review_language_for_top_100(verbose=True)
print('Done.\n')
return
def filter_out_reviews_not_detected_as_english(
english_review_dict,
detected_languages,
expected_language_code='en',
):
review_ids = english_review_dict.keys()
print(
'Filtering out reviews which were not detected as {}.'.format(
expected_language_code,
),
)
review_ids = filter(
lambda x: detected_languages[x] == expected_language_code,
review_ids,
)
checked_english_review_dict = {}
for review_id in review_ids:
checked_english_review_dict[review_id] = {}
checked_english_review_dict[review_id] = english_review_dict[review_id]
return checked_english_review_dict
def filter_reviews(app_id=None):
if app_id is None:
app_id = get_artifact_app_id()
english_review_dict = load_english_reviews(app_id)
english_review_dict = filter_out_short_reviews(
english_review_dict,
length_threshold=150,
)
print(f'#reviews = {len(english_review_dict)}')
detected_languages = detect_review_language(app_id)
english_review_dict = filter_out_reviews_not_detected_as_english(
english_review_dict,
detected_languages,
)
print(f'#reviews = {len(english_review_dict)}')
return english_review_dict
def main():
# Pre-processing (only required once)
apply_language_detector()
# Processing: Artifact
app_id = get_artifact_app_id()
english_review_dict = filter_reviews(app_id)
return
if __name__ == '__main__':
main()