From 76bcd4917e54b451d4d7523b3d2a8bec7bcd0a12 Mon Sep 17 00:00:00 2001 From: Kevin Lu <6320810+kevinlul@users.noreply.github.com> Date: Sat, 6 Jul 2024 10:21:29 -0400 Subject: [PATCH] Prefer Master Duel zh-CN translations to OurOCG --- src/common.py | 17 +++++------------ src/job_ocgtcg.py | 32 +++++++++++++++++++++----------- 2 files changed, 26 insertions(+), 23 deletions(-) diff --git a/src/common.py b/src/common.py index 04279fc0b3..9fd9621ed5 100644 --- a/src/common.py +++ b/src/common.py @@ -186,9 +186,7 @@ def transform_image(image: str) -> List[Dict[str, str]]: return [transform_image_entry(entry) for entry in tokens] -def transform_names( - wikitext: Dict[str, str], zh_cn_fallback: Optional[str] = None -) -> Dict[str, str]: +def transform_names(wikitext: Dict[str, str]) -> Dict[str, str]: return { "en": wikitext["en_name"], "de": wikitext.get("de_name"), @@ -201,13 +199,11 @@ def transform_names( "ko": wikitext.get("ko_name"), "ko_rr": wikitext.get("ko_rr_name"), "zh-TW": wikitext.get("tc_name"), - "zh-CN": wikitext.get("sc_name") or zh_cn_fallback, + "zh-CN": wikitext.get("sc_name"), } -def transform_texts( - wikitext: Dict[str, str], zh_cn_fallback: Optional[str] = None -) -> Dict[str, str]: +def transform_texts(wikitext: Dict[str, str]) -> Dict[str, str]: return { "en": str_or_none(wikitext.get("lore")), # should never be none "de": str_or_none(wikitext.get("de_lore")), @@ -218,7 +214,7 @@ def transform_texts( "ja": str_or_none(wikitext.get("ja_lore")), "ko": str_or_none(wikitext.get("ko_lore")), "zh-TW": str_or_none(wikitext.get("tc_lore")), - "zh-CN": str_or_none(wikitext.get("sc_lore") or zh_cn_fallback), + "zh-CN": str_or_none(wikitext.get("sc_lore")), } @@ -285,10 +281,7 @@ def annotate_shared(document: Dict[str, Any], wikitext: Dict[str, str]) -> None: "ja": str_or_none(wikitext.get("ja_pendulum_effect")), "ko": str_or_none(wikitext.get("ko_pendulum_effect")), "zh-TW": str_or_none(wikitext.get("tc_pendulum_effect")), - "zh-CN": str_or_none( - wikitext.get("sc_pendulum_effect") - or wikitext.get("ourocg_pendulum") - ), + "zh-CN": str_or_none(wikitext.get("sc_pendulum_effect")), } # bonus derived fields if "ritualcard" in wikitext: diff --git a/src/job_ocgtcg.py b/src/job_ocgtcg.py index 1fcac9620f..b641f02185 100644 --- a/src/job_ocgtcg.py +++ b/src/job_ocgtcg.py @@ -28,18 +28,28 @@ def annotate_zh_cn( - yaml: YAML, logger: logging.Logger, zh_cn_dir: str, wikitext: Dict[str, str] + yaml: YAML, logger: logging.Logger, document: Dict[str, str], zh_cn_dir: str ) -> None: - password = int_or_none(wikitext.get("password") or "") + if document["name"]["zh-CN"] and document["text"]["zh-CN"]: + return + password = int_or_none(document.get("password") or "") zh_cn_path = os.path.join(zh_cn_dir, f"{password}.yaml") if os.path.isfile(zh_cn_path): logger.info(f"zh-CN: {zh_cn_path}") with open(zh_cn_path) as f: - document = yaml.load(f) - wikitext["ourocg_name"] = document["name"] - wikitext["ourocg_text"] = document["text"] - if document.get("pendulum"): - wikitext["ourocg_pendulum"] = document["pendulum"] + zh_cn = yaml.load(f) + if not document["name"]["zh-CN"]: + document["name"]["zh-CN"] = zh_cn["name"] + if not document["text"]["zh-CN"]: + document["text"]["zh-CN"] = LiteralScalarString(zh_cn["text"]) + if ( + document.get("pendulum_effect") + and not document["pendulum_effect"]["zh-CN"] + and zh_cn.get("pendulum") + ): + document["pendulum_effect"]["zh-CN"] = LiteralScalarString( + zh_cn["pendulum"] + ) def transform_structure( @@ -74,8 +84,8 @@ def transform_structure( document = { "konami_id": konami_id, "password": password, - "name": transform_names(wikitext, wikitext.get("ourocg_name")), - "text": transform_texts(wikitext, wikitext.get("ourocg_text")), + "name": transform_names(wikitext), + "text": transform_texts(wikitext), } annotate_shared(document, wikitext) if wikitext.get("image"): @@ -414,8 +424,6 @@ def job( logger.info(f"Skip: {filepath}") continue properties["yugipedia_page_id"] = page_id - if zh_cn_dir: - annotate_zh_cn(yaml, logger, zh_cn_dir, properties) document = transform_structure(logger, properties) if document: annotate_limit_regulation(document, tcg_vector, ocg_vector) @@ -427,6 +435,8 @@ def job( annotate_assignments(document, assignments) if ko_override: override_ko(logger, document, ko_override) + if zh_cn_dir: + annotate_zh_cn(yaml, logger, document, zh_cn_dir) write_output(yaml, logger, document) if return_results: results.append(document)