-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatch_names.py
395 lines (333 loc) · 14.3 KB
/
match_names.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
import steampi.calendar
import steampi.text_distances
from disqualify_vote import is_a_noisy_vote
from extend_steamspy import (
get_app_name_for_problematic_app_id,
get_release_year_for_problematic_app_id,
load_extended_steamspy_database,
)
from hard_coded_matches import (
check_database_of_problematic_game_names,
find_hard_coded_app_id,
)
from igdb_match_names import (
download_igdb_local_databases,
load_igdb_local_databases,
print_igdb_matches,
transform_structure_of_matches,
)
from my_types import Ballots
def constrain_app_id_search_by_year(
dist: dict[str, float],
sorted_app_ids: list[str],
release_year: str | None,
max_num_tries_for_year: int,
year_constraint: str | None = "equality",
) -> list[str]:
filtered_sorted_app_ids = sorted_app_ids.copy()
if release_year is not None and year_constraint is not None:
first_match = filtered_sorted_app_ids[0]
dist_reference = dist[first_match]
if dist_reference > 0:
# Check release year to remove possible mismatches. For instance, with input Warhammer 2 and two choices:
# Warhammer & Warhammer II, we would only keep the game released in the target year (2017), i.e. the sequel.
is_the_first_match_released_in_a_wrong_year = True
iter_count = 0
while (
is_the_first_match_released_in_a_wrong_year
and (iter_count < max_num_tries_for_year)
and filtered_sorted_app_ids
):
first_match = filtered_sorted_app_ids[0]
try:
matched_release_year = steampi.calendar.get_release_year(
first_match,
)
except ValueError:
matched_release_year = get_release_year_for_problematic_app_id(
app_id=first_match,
)
if year_constraint == "equality":
# We want the matched release year to be equal to the target release year.
# NB: this is useful to compute the Game of the Year.
is_the_first_match_released_in_a_wrong_year = bool(
matched_release_year != int(release_year),
)
elif year_constraint == "minimum":
# We want the matched release year to be greater than or equal to the target release year.
# NB: this should be useful to compute the Game of the last Decade.
is_the_first_match_released_in_a_wrong_year = bool(
matched_release_year < int(release_year),
)
elif year_constraint == "maximum":
# We want the matched release year to be less than or equal to the target release year.
is_the_first_match_released_in_a_wrong_year = bool(
matched_release_year > int(release_year),
)
else:
# We do not want to apply any constraint.
is_the_first_match_released_in_a_wrong_year = False
if is_the_first_match_released_in_a_wrong_year:
filtered_sorted_app_ids.pop(0)
iter_count += 1
# Reset if we could not find a match released in the target year
if is_the_first_match_released_in_a_wrong_year:
filtered_sorted_app_ids = sorted_app_ids
return filtered_sorted_app_ids
def apply_hard_coded_fixes_to_app_id_search(
game_name_input: str,
filtered_sorted_app_ids: list[str],
num_closest_neighbors: int,
) -> list[str]:
closest_app_id = [find_hard_coded_app_id(game_name_input)]
if num_closest_neighbors > 1:
closest_app_id.extend(filtered_sorted_app_ids[0 : (num_closest_neighbors - 1)])
return closest_app_id
def get_default_distance_cut_off_for_difflib() -> float:
# Reference: https://docs.python.org/3/library/difflib.html
similarity_cut_off = 0.6
return 1 - similarity_cut_off
def find_closest_app_id(
game_name_input: str,
steamspy_database: dict,
release_year: str | None = None,
num_closest_neighbors: int = 1,
max_num_tries_for_year: int = 2,
*,
use_levenshtein_distance: bool = True,
year_constraint: str = "equality",
is_steamspy_api_paginated: bool = True,
) -> tuple[list[str], list[int]]:
if use_levenshtein_distance:
# n is not used by Levenshtein distance.
n = None
else:
# n is only used by difflib.
# NB: difflib may not return as many neighbors as requested, because difflib relies on a similarity cut-off.
n = num_closest_neighbors + max_num_tries_for_year
(sorted_app_ids, dist) = steampi.text_distances.find_most_similar_game_names(
game_name_input,
steamspy_database,
use_levenshtein_distance=use_levenshtein_distance,
n=n,
)
filtered_sorted_app_ids = sorted_app_ids
if release_year is not None and year_constraint is not None:
filtered_sorted_app_ids = constrain_app_id_search_by_year(
dist,
sorted_app_ids,
release_year,
max_num_tries_for_year,
year_constraint=year_constraint,
)
closest_app_id = filtered_sorted_app_ids[0:num_closest_neighbors]
if check_database_of_problematic_game_names(game_name_input):
closest_app_id = apply_hard_coded_fixes_to_app_id_search(
game_name_input,
filtered_sorted_app_ids,
num_closest_neighbors,
)
if not use_levenshtein_distance or is_steamspy_api_paginated:
# With difflib, computations are more expensive than with Levenshtein distance, therefore dist only contains
# distances for a few entries. So, we set the distance to 0.4 (default cut-off) for all the other entries.
#
# Edit: moreover, due to the pagination recently adopted by SteamSpy API, dist misses many entries nowadays.
for app_id in closest_app_id:
if app_id not in dist:
dist[app_id] = get_default_distance_cut_off_for_difflib()
closest_distance = [dist[app_id] for app_id in closest_app_id]
return closest_app_id, closest_distance
def precompute_matches(
raw_votes: dict,
release_year: str | None = None,
num_closest_neighbors: int = 3,
max_num_tries_for_year: int = 2,
*,
use_levenshtein_distance: bool = True,
year_constraint: str = "equality",
goty_field: str = "goty_preferences",
is_steamspy_api_paginated: bool = True,
) -> dict:
seen_game_names = set()
matches = {}
steamspy_database = load_extended_steamspy_database()
for voter in raw_votes:
for raw_name in raw_votes[voter][goty_field].values():
if raw_name not in seen_game_names:
seen_game_names.add(raw_name)
if not is_a_noisy_vote(raw_name):
(closest_app_id, closest_distance) = find_closest_app_id(
raw_name,
steamspy_database,
release_year,
num_closest_neighbors,
max_num_tries_for_year,
use_levenshtein_distance=use_levenshtein_distance,
year_constraint=year_constraint,
is_steamspy_api_paginated=is_steamspy_api_paginated,
)
# Due to the pagination recently adopted by SteamSpy API, dist misses many entries nowadays.
if is_steamspy_api_paginated:
for app_id in closest_app_id:
if app_id not in steamspy_database:
steamspy_database[app_id] = {}
steamspy_database[app_id]["name"] = (
get_app_name_for_problematic_app_id(app_id)
)
element = {
"input_name": raw_name,
"matched_appID": closest_app_id,
"matched_name": [
steamspy_database[appID]["name"] for appID in closest_app_id
],
"match_distance": closest_distance,
}
matches[raw_name] = element
return matches
def display_matches(matches: dict, *, print_after_sort: bool = True) -> None:
# Index of the neighbor used to sort keys of the matches dictionary
neighbor_reference_index = 0
if print_after_sort:
sorted_keys = sorted(
matches.keys(),
key=lambda x: matches[x]["match_distance"][neighbor_reference_index]
/ (1 + len(matches[x]["input_name"])),
)
else:
sorted_keys = list(matches.keys())
for game in sorted_keys:
element = matches[game]
dist_reference = element["match_distance"][neighbor_reference_index]
game_name = element["input_name"]
if dist_reference > 0 or check_database_of_problematic_game_names(game_name):
print(
"\n"
+ game_name
+ " ("
+ "length:"
+ str(len(game_name))
+ ")"
+ " ---> ",
end="",
)
for neighbor_index in range(len(element["match_distance"])):
dist = element["match_distance"][neighbor_index]
print(
element["matched_name"][neighbor_index]
+ " (appID: "
+ element["matched_appID"][neighbor_index]
+ " ; "
+ "distance:"
+ str(dist)
+ ")",
end="\t",
)
print()
def normalize_votes(
raw_votes: dict,
matches: dict,
goty_field: str = "goty_preferences",
) -> dict:
# Index of the first neighbor
neighbor_reference_index = 0
normalized_votes: Ballots = {}
for voter_name in raw_votes:
normalized_votes[voter_name] = {}
normalized_votes[voter_name]["ballots"] = {}
normalized_votes[voter_name]["distances"] = {}
for position, game_name in raw_votes[voter_name][goty_field].items():
if game_name in matches:
# Display game name before error due to absence of any matched IGDB ID, in order to make it easier to
# incrementally and manually add hard-coded matches:
if len(matches[game_name]["matched_appID"]) == 0:
print(f"[Warning] no match found for {game_name}")
normalized_votes[voter_name]["ballots"][position] = matches[game_name][
"matched_appID"
][neighbor_reference_index]
normalized_votes[voter_name]["distances"][position] = matches[
game_name
]["match_distance"][neighbor_reference_index]
else:
normalized_votes[voter_name]["ballots"][position] = None
normalized_votes[voter_name]["distances"][position] = None
return normalized_votes
def standardize_ballots(
ballots: Ballots,
release_year: str | None,
*,
print_after_sort: bool = True,
use_igdb: bool = False,
retrieve_igdb_data_from_scratch: bool = True,
apply_hard_coded_extension_and_fixes: bool = True,
use_levenshtein_distance: bool = True,
extend_previous_databases: bool = True,
must_be_available_on_pc: bool = True,
must_be_a_game: bool = True,
goty_field: str = "goty_preferences",
year_constraint: str = "equality",
print_matches: bool = True,
verbose: bool = False,
) -> tuple[dict, dict]:
if use_igdb:
# Using IGDB
if retrieve_igdb_data_from_scratch:
# By default, we extend the previous databases. If you do not want to, delete them before running the code.
igdb_match_database, igdb_local_database = download_igdb_local_databases(
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=goty_field,
year_constraint=year_constraint,
verbose=verbose,
)
else:
igdb_match_database, igdb_local_database = load_igdb_local_databases(
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=goty_field,
year_constraint=year_constraint,
verbose=verbose,
)
if print_matches:
print_igdb_matches(
igdb_match_database,
igdb_local_database,
constrained_release_year=release_year,
year_constraint=year_constraint,
)
matches = transform_structure_of_matches(
igdb_match_database,
igdb_local_database,
)
else:
# Using SteamSpy
matches = precompute_matches(
ballots,
release_year=release_year,
num_closest_neighbors=3,
max_num_tries_for_year=2,
use_levenshtein_distance=use_levenshtein_distance,
year_constraint=year_constraint,
goty_field=goty_field,
)
if print_matches:
display_matches(matches, print_after_sort=print_after_sort)
standardized_ballots = normalize_votes(ballots, matches, goty_field=goty_field)
return standardized_ballots, matches
if __name__ == "__main__":
from load_ballots import get_ballot_file_name, load_ballots
ballot_year = "2018"
input_filename = get_ballot_file_name(ballot_year)
use_levenshtein_distance = True
ballots = load_ballots(input_filename)
(standardized_ballots, matches) = standardize_ballots(
ballots,
release_year=ballot_year,
use_levenshtein_distance=use_levenshtein_distance,
)