-
Notifications
You must be signed in to change notification settings - Fork 0
/
export_review_data.py
283 lines (197 loc) · 6.8 KB
/
export_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
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
import sys
import steamreviews
from langdetect import DetectorFactory, detect, lang_detect_exception
def download_recent_reviews(app_id, num_days=28):
request_params = {}
request_params['filter'] = 'recent'
if int(num_days) > 0:
request_params['day_range'] = str(num_days)
request_params['language'] = 'english'
review_dict, _ = steamreviews.download_reviews_for_app_id(
app_id=app_id,
chosen_request_params=request_params,
)
reviews = review_dict['reviews']
return reviews
def get_review_url(author_id, app_id=None):
review_url = (
'https://steamcommunity.com/profiles/' + str(author_id) + '/recommended/'
)
if app_id is not None:
review_url += str(app_id) + '/'
return review_url
def detect_language(reviews, review_ids=None, app_id=None, verbose=True):
if review_ids is None:
review_ids = reviews.keys()
detected_languages = {}
DetectorFactory.seed = 0
for count, review_id in enumerate(review_ids):
if verbose and (count + 1) % 1000 == 0:
print(
'Reviews processed by langdetect: {}/{}.'.format(
count + 1,
len(review_ids),
),
)
review_content = reviews[review_id]['review']
try:
detected_languages[review_id] = detect(review_content)
except lang_detect_exception.LangDetectException:
detected_languages[review_id] = 'unknown'
author_id = reviews[review_id]['author']['steamid']
review_url = get_review_url(author_id, app_id=app_id)
print(f'[review n°{review_id}] {review_url}')
print(review_content)
return detected_languages
def filter_out_reviews_not_written_in_english(
reviews,
review_ids=None,
language_str='english',
):
if review_ids is None:
review_ids = reviews.keys()
print(f'Filtering out reviews which were not written in {language_str}.')
review_ids = list(
filter(lambda x: reviews[x]['language'] == language_str, review_ids),
)
print(f'#reviews = {len(review_ids)}')
return review_ids
def filter_out_short_reviews(reviews, review_ids=None, length_threshold=150):
if review_ids is None:
review_ids = reviews.keys()
print(
'Filtering out reviews with strictly fewer than {} characters.'.format(
length_threshold,
),
)
review_ids = list(
filter(lambda x: len(reviews[x]['review']) >= length_threshold, review_ids),
)
print(f'#reviews = {len(review_ids)}')
return review_ids
def filter_out_reviews_not_detected_as_written_in_english(
reviews,
review_ids=None,
app_id=None,
expected_language_code='en',
):
if review_ids is None:
review_ids = reviews.keys()
detected_languages = detect_language(reviews, review_ids, app_id=app_id)
print(
'Filtering out reviews which were not detected as written in {}.'.format(
expected_language_code,
),
)
review_ids = list(
filter(lambda x: detected_languages[x] == expected_language_code, review_ids),
)
print(f'#reviews = {len(review_ids)}')
return review_ids
def filter_reviews(reviews, app_id=None):
review_ids = list(reviews.keys())
print(f'#reviews = {len(review_ids)}')
review_ids = filter_out_reviews_not_written_in_english(reviews, review_ids)
review_ids = filter_out_short_reviews(reviews, review_ids)
review_ids = filter_out_reviews_not_detected_as_written_in_english(
reviews,
review_ids,
app_id=app_id,
)
return review_ids
def get_data_folder():
data_folder = 'data/'
return data_folder
def get_txt_output_file_name(app_id):
output_text_file_name = get_data_folder() + str(app_id) + '.txt'
return output_text_file_name
def get_line_separator():
line_separator = '\n'
return line_separator
def trim_review_content(review_content):
# Remove empty lines
line_separator = get_line_separator()
review_content_chunks = [
line.strip()
for line in review_content.split(line_separator)
if len(line.strip()) > 0
]
trimmed_review_content = line_separator.join(review_content_chunks)
return trimmed_review_content
def get_model_token_start():
model_token_start = '<|startoftext|>'
return model_token_start
def get_model_token_end():
model_token_end = '<|endoftext|>'
return model_token_end
def concatenate_reviews(
output_text_file_name,
reviews,
review_ids=None,
use_model_token_delimiters=True,
remove_empty_lines=True,
):
if review_ids is None:
review_ids = reviews.keys()
# Append to a large list
review_list = []
for review_id in review_ids:
review_content = reviews[review_id]['review']
if remove_empty_lines:
trimmed_review_content = trim_review_content(review_content)
else:
trimmed_review_content = review_content
review_list.append(trimmed_review_content)
# Concatenate as a large str
if use_model_token_delimiters:
review_separator = (
get_model_token_end() + get_line_separator() + get_model_token_start()
)
else:
review_separator = get_line_separator()
concatenated_reviews = review_separator.join(review_list)
if use_model_token_delimiters:
concatenated_reviews = (
get_model_token_start() + concatenated_reviews + get_model_token_end()
)
# Save to disk
with open(output_text_file_name, 'w', encoding='utf8') as f:
print(concatenated_reviews, file=f)
return
def apply_workflow_for_app_id(app_id, num_days=28, use_model_token_delimiters=True):
output_text_file_name = get_txt_output_file_name(app_id)
reviews = download_recent_reviews(app_id, num_days)
review_ids = filter_reviews(reviews, app_id=app_id)
concatenate_reviews(
output_text_file_name,
reviews,
review_ids,
use_model_token_delimiters=use_model_token_delimiters,
)
return
def main(argv):
default_num_days = 28
use_model_token_delimiters = True
if len(argv) == 0:
app_id = 583950
num_days = default_num_days
print(
'No detected command-line argument. AppID automatically set to {}.'.format(
app_id,
),
)
else:
app_id = argv[0]
print(f'A command-line argument is detected. AppID set to {app_id}.')
try:
num_days = int(argv[1])
except IndexError:
num_days = default_num_days
apply_workflow_for_app_id(
app_id,
num_days=num_days,
use_model_token_delimiters=use_model_token_delimiters,
)
return
if __name__ == '__main__':
main(sys.argv[1:])