"""PersonDraft → people.json 的 Person(packages/protocol/src/people.ts)。""" from __future__ import annotations from collections import defaultdict from collections.abc import Sequence from typing import Any from .model import PersonDraft, Sourced from .names import name_key, slug_name STAT_KEYS: tuple[str, ...] = ( "awareness", "network", "affinity", "eloquence", "strategy", "online", "image", "wealth", "stamina", ) def slug_id(p: PersonDraft) -> str: year = p.birth_date.value[:4] if p.birth_date and p.birth_date.value else "x" return f"tw-{p.category}-{slug_name(p.name)}-{year}" def _sort_key(p: PersonDraft) -> tuple[str, ...]: """同 id 的人物排序(決定 -2、-3 的歸屬),只用資料內容,不受輸入順序影響。""" return ( name_key(p.name), p.birth_date.value or "" if p.birth_date else "", p.name, ",".join(sorted(p.roles)), ",".join(sorted(p.counties)), ",".join(sorted(p.districts)), ",".join(f"{x.org}/{x.title}" for x in p.positions), ) def assign_ids(persons: Sequence[PersonDraft]) -> list[str]: """有 Wikidata QID 就用 QID;否則用 tw-<類別>-<姓名>-<出生年或 x>。同 id 依固定排序加上 -2、-3。""" base = [p.wikidata_id.value if p.wikidata_id and p.wikidata_id.value else slug_id(p) for p in persons] groups: dict[str, list[int]] = defaultdict(list) for i, b in enumerate(base): groups[b].append(i) ids = list(base) for b, idx in groups.items(): if len(idx) > 1: for n, i in enumerate(sorted(idx, key=lambda k: _sort_key(persons[k]))): ids[i] = b if n == 0 else f"{b}-{n + 1}" if len(set(ids)) != len(ids): raise ValueError("人物 id 不唯一") return ids def _field_sources(p: PersonDraft) -> dict[str, list[str]]: out: dict[str, list[str]] = {"name": list(p.name_sources)} scalars: dict[str, Sourced | None] = { "gender": p.gender, "birthDate": p.birth_date, "party": p.party, "wikidataId": p.wikidata_id, } for field, value in scalars.items(): if value is not None and (value.value is not None or field == "party"): out[field] = list(value.source_ids) return out def _value(v: Sourced | None) -> str | None: return v.value if v else None def person_json(p: PersonDraft, person_id: str, as_of: str) -> dict[str, Any]: return { "id": person_id, "kind": "real", "category": p.category, "name": p.name, "gender": _value(p.gender), "birthDate": _value(p.birth_date), "party": _value(p.party), "wikidataId": _value(p.wikidata_id), "positions": [x.to_json() for x in p.positions], "education": [x.to_json() for x in p.education], "electionHistory": [x.to_json() for x in p.elections], "stats": dict.fromkeys(STAT_KEYS), "scoring": [], "fieldSources": _field_sources(p), "asOf": as_of, } def people_file( persons: Sequence[PersonDraft], ids: Sequence[str], protocol_version: str, as_of: str ) -> dict[str, Any]: rows = sorted(zip(ids, persons, strict=True), key=lambda t: (t[1].category, name_key(t[1].name), t[0])) return { "protocolVersion": protocol_version, "asOf": as_of, "people": [person_json(p, pid, as_of) for pid, p in rows], } def referenced_source_ids(doc: dict[str, Any]) -> set[str]: out: set[str] = set() for person in doc["people"]: for ids in person["fieldSources"].values(): out.update(ids) for key in ("positions", "education", "electionHistory"): for item in person[key]: out.update(item["sourceIds"]) return out