| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- #!/usr/bin/env python3
- """
- Merge web-UI LLM output (NDJSON) back into categories.json.
- The LLM outputs one JSON object per line:
- {"id": 51, "description": "...", "examples": ["...", "..."]}
- This script applies only the missing fields — already-populated fields are untouched.
- Usage:
- python -m bom_assistant.suppliers.lcsc.apply_enrich_output < enrich_output.ndjson
- # or
- python -m bom_assistant.suppliers.lcsc.apply_enrich_output enrich_output.ndjson
- """
- from __future__ import annotations
- import json
- import sys
- from pathlib import Path
- _CATS_PATH = Path(__file__).parent / "categories.json"
- def main() -> None:
- if len(sys.argv) > 1:
- lines = Path(sys.argv[1]).read_text(encoding="utf-8").splitlines()
- else:
- lines = sys.stdin.read().splitlines()
- cats = json.loads(_CATS_PATH.read_text(encoding="utf-8"))
- # build id → (cat_idx, sub_idx) index
- idx: dict[int, tuple[int, int]] = {}
- for ci, cat in enumerate(cats):
- for si, sub in enumerate(cat["subcategories"]):
- idx[sub["id"]] = (ci, si)
- applied = 0
- skipped = 0
- for line in lines:
- line = line.strip()
- if not line:
- continue
- try:
- entry = json.loads(line)
- except json.JSONDecodeError:
- print(f"warn: bad JSON line: {line[:80]!r}", file=sys.stderr)
- skipped += 1
- continue
- sub_id = entry.get("id")
- if sub_id not in idx:
- print(f"warn: unknown id {sub_id}", file=sys.stderr)
- skipped += 1
- continue
- ci, si = idx[sub_id]
- sub = cats[ci]["subcategories"][si]
- changed = False
- if entry.get("description") and not sub.get("description"):
- sub["description"] = entry["description"]
- changed = True
- if entry.get("examples") and not sub.get("examples"):
- sub["examples"] = entry["examples"]
- changed = True
- if changed:
- applied += 1
- _CATS_PATH.write_text(
- json.dumps(cats, indent=2, ensure_ascii=False), encoding="utf-8"
- )
- total = sum(len(c["subcategories"]) for c in cats)
- with_desc = sum(1 for c in cats for s in c["subcategories"] if s.get("description"))
- with_ex = sum(1 for c in cats for s in c["subcategories"] if s.get("examples"))
- print(f"Applied {applied} updates ({skipped} skipped). Saved categories.json.")
- print(f"Coverage: {with_desc}/{total} descriptions, {with_ex}/{total} examples")
- if __name__ == "__main__":
- main()
|