Return a stable user-facing label for a canonical direction key or alias.
(
direction_key: Any,
*,
lexicon: Optional[Dict[str, Any]] = None,
prefer_chinese: bool = True,
)
| 489 | |
| 490 | |
| 491 | def format_direction_label( |
| 492 | direction_key: Any, |
| 493 | *, |
| 494 | lexicon: Optional[Dict[str, Any]] = None, |
| 495 | prefer_chinese: bool = True, |
| 496 | ) -> str: |
| 497 | """Return a stable user-facing label for a canonical direction key or alias.""" |
| 498 | cleaned = str(direction_key or "").strip() |
| 499 | if not cleaned: |
| 500 | return "" |
| 501 | |
| 502 | entry = get_direction_entry(cleaned, lexicon=lexicon) or {} |
| 503 | if not entry: |
| 504 | resolved = resolve_canonical_direction(cleaned, lexicon=lexicon, include_paper_terms=True) |
| 505 | if resolved: |
| 506 | entry = resolved.get("entry") or get_direction_entry(resolved["canonical_name"], lexicon=lexicon) or {} |
| 507 | |
| 508 | if entry: |
| 509 | if prefer_chinese: |
| 510 | label = str(entry.get("name_cn") or entry.get("name") or cleaned).strip() |
| 511 | else: |
| 512 | label = str(entry.get("name") or entry.get("name_cn") or cleaned).strip() |
| 513 | if label: |
| 514 | return label |
| 515 | |
| 516 | if any("\u4e00" <= ch <= "\u9fff" for ch in cleaned): |
| 517 | return cleaned |
| 518 | return cleaned.replace("-", " ").replace("_", " ").title() |
| 519 | |
| 520 | |
| 521 | def build_bootstrap_summary( |
no test coverage detected