-
Notifications
You must be signed in to change notification settings - Fork 0
/
optional_categories.py
317 lines (238 loc) · 9.6 KB
/
optional_categories.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
from disqualify_vote import is_a_noisy_vote
from igdb_credentials import download_latest_credentials
from igdb_match_names import (
download_igdb_local_databases,
get_igdb_human_release_dates,
get_link_to_igdb_website,
load_igdb_local_databases,
)
from parsing_params import get_optional_categories
from steam_store_utils import get_link_to_store
def get_best_optional_categories():
optional_categories = [
f'best_{categorie}' for categorie in get_optional_categories()
]
return optional_categories
def get_optional_ballots(ballots, category_name):
optional_ballots = [
ballots[voter_name][category_name]
for voter_name in ballots
if category_name in ballots[voter_name]
and ballots[voter_name][category_name] is not None
and len(ballots[voter_name][category_name]) > 0
]
return optional_ballots
def filter_noise_from_optional_ballots(optional_ballots):
filtered_optional_ballots = []
for element in optional_ballots:
if not is_a_noisy_vote(element):
filtered_optional_ballots.append(element)
return filtered_optional_ballots
def get_dummy_field():
dummy_field = 'dummy_preferences'
return dummy_field
def format_optional_ballots_for_igdb_matching(optional_ballots, dummy_field=None):
if dummy_field is None:
dummy_field = get_dummy_field()
dummy_voter = 'dummy_voter'
formatted_optional_ballots = {}
formatted_optional_ballots[dummy_voter] = {}
formatted_optional_ballots[dummy_voter][dummy_field] = dict(
enumerate(optional_ballots),
)
return formatted_optional_ballots
def match_optional_ballots(
optional_ballots,
release_year=None,
use_igdb=False,
retrieve_igdb_data_from_scratch=True,
apply_hard_coded_extension_and_fixes=True,
must_be_available_on_pc=False,
must_be_a_game=False,
use_levenshtein_distance=True,
):
import steampi.calendar
from extend_steamspy import load_extended_steamspy_database
from match_names import find_closest_app_id
seen_game_names = set()
matches = {}
matched_optional_ballots = []
dummy_field = get_dummy_field()
formatted_optional_ballots = format_optional_ballots_for_igdb_matching(
optional_ballots,
dummy_field=dummy_field,
)
if use_igdb:
# Code inspired from standardize_ballots() in match_names.py
if retrieve_igdb_data_from_scratch:
# Caveat: it is mandatory to set 'extend_previous_databases' to True, because:
# i) databases are shared between GotY and optional categories, and we want to KEEP the data for GotY,
# ii) databases are shared between optional categories, and there is a loop over the optional categories.
extend_previous_databases = True
igdb_match_database, local_database = download_igdb_local_databases(
formatted_optional_ballots,
release_year=release_year,
apply_hard_coded_extension_and_fixes=apply_hard_coded_extension_and_fixes,
extend_previous_databases=extend_previous_databases,
must_be_available_on_pc=must_be_available_on_pc,
must_be_a_game=must_be_a_game,
goty_field=dummy_field,
)
else:
igdb_match_database, local_database = load_igdb_local_databases(
formatted_optional_ballots,
release_year=release_year,
apply_hard_coded_extension_and_fixes=apply_hard_coded_extension_and_fixes,
must_be_available_on_pc=must_be_available_on_pc,
must_be_a_game=must_be_a_game,
goty_field=dummy_field,
)
else:
igdb_match_database = None
local_database = load_extended_steamspy_database()
print()
for raw_name in optional_ballots:
if raw_name not in seen_game_names:
seen_game_names.add(raw_name)
if use_igdb:
# Using IGDB
# Code inspired from print_schulze_ranking() in schulze_goty.py
try:
igdb_matched_ids = igdb_match_database[raw_name]
except KeyError:
igdb_matched_ids = [None]
try:
igdb_best_matched_id = igdb_matched_ids[0]
except IndexError:
igdb_best_matched_id = None
if igdb_best_matched_id is not None:
appID = str(igdb_best_matched_id)
app_name = local_database[appID]['name']
_, app_id_release_date = get_igdb_human_release_dates(
appID,
local_database,
)
app_url = get_link_to_igdb_website(appID, local_database)
else:
appID = None
app_name = None
app_id_release_date = None
app_url = None
else:
# Using SteamSpy
(closest_appID, _) = find_closest_app_id(
raw_name,
steamspy_database=local_database,
use_levenshtein_distance=use_levenshtein_distance,
)
appID = closest_appID[0]
app_name = local_database[appID]['name']
app_id_release_date = steampi.calendar.get_release_date_as_str(appID)
app_url = get_link_to_store(appID)
if app_id_release_date is None:
app_id_release_date = 'an unknown date'
matches[raw_name] = {}
matches[raw_name]['matched_appID'] = appID
matches[raw_name]['matched_name'] = app_name
matches[raw_name]['matched_release_date'] = app_id_release_date
matches[raw_name]['matched_url'] = app_url
id_description = 'IGDB id' if use_igdb else 'AppID'
print(
'\t{} ---> {}: {}\t;\t{} ({})'.format(
raw_name,
id_description,
matches[raw_name]['matched_appID'],
matches[raw_name]['matched_name'],
matches[raw_name]['matched_release_date'],
),
)
my_str = '{} (appID: {}, released on {})'.format(
matches[raw_name]['matched_name'],
matches[raw_name]['matched_url'],
matches[raw_name]['matched_release_date'],
)
matched_optional_ballots.append(my_str)
return matched_optional_ballots
def count_optional_ballots(optional_ballots):
optional_counts = {}
for element in optional_ballots:
try:
optional_counts[element] += 1
except KeyError:
optional_counts[element] = 1
return optional_counts
def compute_ranking_based_on_optional_ballots(optional_ballots):
optional_counts = count_optional_ballots(optional_ballots)
# Reference: https://stackoverflow.com/a/37693603
ranking = sorted(
optional_counts.items(),
key=lambda x: (-x[1], x[0]),
reverse=False,
)
return ranking
def pretty_display(ranking):
print()
current_num_votes = 0
rank = 0
increment = 1
for element in ranking:
game_name = element[0]
num_votes = element[1]
if num_votes != current_num_votes:
current_num_votes = num_votes
rank += increment
increment = 1
else:
increment += 1
my_str = ' with #votes = ' if num_votes > 1 else ' with #vote = '
print(f'{rank:2} | ' + game_name.strip() + my_str + str(num_votes))
return
def display_optional_ballots(
input_filename,
filter_noise=True,
release_year=None,
use_igdb=False,
retrieve_igdb_data_from_scratch=True,
apply_hard_coded_extension_and_fixes=True,
use_levenshtein_distance=True,
):
from load_ballots import load_ballots
ballots = load_ballots(input_filename)
for category_name in get_best_optional_categories():
print('\nCategory: ' + category_name)
optional_ballots = get_optional_ballots(ballots, category_name)
if filter_noise:
optional_ballots = filter_noise_from_optional_ballots(optional_ballots)
optional_ballots = match_optional_ballots(
optional_ballots,
release_year=release_year,
use_igdb=use_igdb,
retrieve_igdb_data_from_scratch=retrieve_igdb_data_from_scratch,
apply_hard_coded_extension_and_fixes=apply_hard_coded_extension_and_fixes,
use_levenshtein_distance=use_levenshtein_distance,
)
ranking = compute_ranking_based_on_optional_ballots(optional_ballots)
pretty_display(ranking)
return True
if __name__ == '__main__':
from load_ballots import get_ballot_file_name
ballot_year = '2020'
input_filename = get_ballot_file_name(ballot_year, is_anonymized=True)
use_igdb = True
retrieve_igdb_data_from_scratch = False
apply_hard_coded_extension_and_fixes = True
use_levenshtein_distance = True
update_credentials = False
if update_credentials:
download_latest_credentials(verbose=False)
# Optional Categories of the Year
release_year = ballot_year
display_optional_ballots(
input_filename,
filter_noise=True,
release_year=release_year,
use_igdb=use_igdb,
retrieve_igdb_data_from_scratch=retrieve_igdb_data_from_scratch,
apply_hard_coded_extension_and_fixes=apply_hard_coded_extension_and_fixes,
use_levenshtein_distance=use_levenshtein_distance,
)