| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- """CLI:默认构建生产图谱;显式 JSON 配置保留历史模板构建。
- 用法: uv run python scripts/build_graph.py [config.json] [--clear]
- """
- from __future__ import annotations
- import argparse
- import json
- import sys
- from pathlib import Path
- def main() -> None:
- sys.stdout.reconfigure(encoding="utf-8")
- if len(sys.argv) == 1 or sys.argv[1].startswith("--"):
- from build_production_graph import main as production_main
- raise SystemExit(production_main())
- ap = argparse.ArgumentParser()
- ap.add_argument("config", nargs="?", default="data/config_example.json")
- ap.add_argument("--clear", action="store_true", help="先清空图再构建")
- args = ap.parse_args()
- config_path = Path(args.config)
- if not config_path.exists():
- print(f"配置文件不存在: {config_path}")
- sys.exit(1)
- config = json.loads(config_path.read_text(encoding="utf-8"))
- from step2_graph_building.graph import build_knowledge_graph
- result = build_knowledge_graph(config, clear_first=args.clear)
- print("ok:", result["ok"])
- print("读取记录数:", result.get("records"))
- print("构建记录数:", result.get("built"))
- print("图谱统计:", result.get("graph"))
- if result.get("issues"):
- print("\n校验问题:")
- for i in result["issues"]:
- print(" -", i)
- if result.get("warnings"):
- print("\n构建警告:")
- for w in result["warnings"]:
- print(" -", w)
- if not result["ok"]:
- sys.exit(1)
- if __name__ == "__main__":
- main()
|