From e1fe2caf0f7c018ff1eac80d7caea49b46fd1edf Mon Sep 17 00:00:00 2001 From: Sam Dornan Date: Mon, 17 Aug 2026 17:19:57 -0500 Subject: [PATCH] fix(launchbox): keep marks the script spells with when folding MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A mark was dropped whenever it was not alphanumeric, and the accent strip before it removed every mark carrying a combining class. Between them they took the vowel signs and viramas Indic and Thai scripts spell with, so "हिन्दी गेम" folded to "हनदगम" and titles differing only in those marks collided. A mark now only goes when it sits on a Latin letter, which is the case it was meant for. Collisions across the dump are unchanged at 551 of 186505 keys, none differing in their digits, and the PlayStation 2 gain still measures 96.7%. Also build each game's dict once rather than per index write. Co-Authored-By: Claude Opus 5 --- .../metadata/launchbox_handler/utils.py | 18 +++++++++++++++--- .../scheduled/update_launchbox_metadata.py | 12 +++++++----- .../handler/metadata/test_launchbox_handler.py | 4 ++++ 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/backend/handler/metadata/launchbox_handler/utils.py b/backend/handler/metadata/launchbox_handler/utils.py index cc64d4ca6..33c8a6444 100644 --- a/backend/handler/metadata/launchbox_handler/utils.py +++ b/backend/handler/metadata/launchbox_handler/utils.py @@ -57,10 +57,22 @@ def fold_title(title: str) -> str: Letters of every script survive. Keeping only ASCII would reduce a title written in one to whatever digits it carries, collapsing "三國立志傳2" and "忍者村大战2" onto the same key. + + A mark only goes when it sits on a Latin letter, where it is an accent the + two sides may disagree about ("Astérix" against "Asterix"). Elsewhere it + spells the word: Devanagari and Thai build syllables from marks, so dropping + them would reduce "हिन्दी" to "हनद" and collide titles that differ. """ - decomposed = unicodedata.normalize("NFKD", title.casefold()) - without_accents = "".join(c for c in decomposed if not unicodedata.combining(c)) - return "".join(c for c in without_accents if c.isalnum()) + kept: list[str] = [] + for char in unicodedata.normalize("NFKD", title.casefold()): + if unicodedata.category(char).startswith("M"): + if kept and "a" <= kept[-1] <= "z": + continue + kept.append(char) + elif char.isalnum(): + kept.append(char) + + return "".join(kept) def file_name_forms(file_name: str) -> list[str]: diff --git a/backend/tasks/scheduled/update_launchbox_metadata.py b/backend/tasks/scheduled/update_launchbox_metadata.py index 98b0a3877..cbf957fb9 100644 --- a/backend/tasks/scheduled/update_launchbox_metadata.py +++ b/backend/tasks/scheduled/update_launchbox_metadata.py @@ -193,21 +193,23 @@ class UpdateLaunchboxMetadataTask(RemoteFilePullTask): and platform_elem is not None and platform_elem.text ): + platform_name = platform_elem.text.strip() + game = _element_to_dict(elem) + # Use a unique combination of name and platform as the key await writer.hset( LAUNCHBOX_METADATA_NAME_KEY, f"{name_elem.text.strip().lower()}" - f":{platform_elem.text.strip()}", - _element_to_dict(elem), + f":{platform_name}", + game, ) folded = fold_title(name_elem.text) if folded: await writer.hset( LAUNCHBOX_METADATA_FOLDED_NAME_KEY, - f"{folded}" - f":{platform_elem.text.strip()}", - _element_to_dict(elem), + f"{folded}:{platform_name}", + game, ) elif elem.tag == "GameAlternateName": diff --git a/backend/tests/handler/metadata/test_launchbox_handler.py b/backend/tests/handler/metadata/test_launchbox_handler.py index 3f94e5f62..b9d1f6057 100644 --- a/backend/tests/handler/metadata/test_launchbox_handler.py +++ b/backend/tests/handler/metadata/test_launchbox_handler.py @@ -713,6 +713,10 @@ class TestRemoteSourceGetRom: # its digits would collide with every other title numbered alike. ("三國立志傳2", "三國立志傳2"), ("Зона 51", "зона51"), + # Scripts that build syllables from marks keep them; stripping to + # alphanumerics alone would spell these titles differently. + ("हिन्दी गेम", "हिन्दीगेम"), + ("สนุกเกอร์", "สนุกเกอร์"), # Nothing comparable left, so callers must treat it as no key. (":: -- ::", ""), ],