feat(kg): add markdown export format for knowledge graph
Some checks failed
tests / core (macos-latest, py3.11) (push) Has been cancelled
tests / core (macos-latest, py3.12) (push) Has been cancelled
tests / core (macos-latest, py3.13) (push) Has been cancelled
tests / core (ubuntu-latest, py3.11) (push) Has been cancelled
tests / core (ubuntu-latest, py3.12) (push) Has been cancelled
tests / core (ubuntu-latest, py3.13) (push) Has been cancelled
tests / doctor CLI smoke test (push) Has been cancelled

- Add kg_ocr/export/markdown_exporter.py with export_markdown()
- Cluster entity co-occurrences using Louvain community detection
- Wire -f markdown into `ocr-pipeline kg export` CLI command with KG_REPORT.md default
- Add unit and CLI tests in tests/test_graph.py
- Document markdown export in README.md
- Untrack pycache binaries and ignore graphify-out and KG_REPORT.md
This commit is contained in:
2026-09-18 16:40:21 +02:00
parent c5bf5d2b0c
commit 05aed7366d
12 changed files with 416 additions and 60 deletions

View File

@@ -6,7 +6,7 @@ import pytest
nx = pytest.importorskip("networkx", reason="kg extra not installed")
from kg_ocr.export import export_graphml, export_json, load_json # noqa: E402
from kg_ocr.export import export_graphml, export_json, export_markdown, load_json # noqa: E402
from kg_ocr.graph import ( # noqa: E402
build_from_directory,
build_graph,
@@ -202,6 +202,50 @@ def test_kg_cli_build_and_stats(chunk_dir: Path, tmp_path: Path) -> None:
assert result.exit_code == 0, result.output
assert out_graphml.is_file()
out_md = tmp_path / "g_report.md"
result = runner.invoke(app, ["kg", "export", str(save), "-f", "markdown", "-o", str(out_md)])
assert result.exit_code == 0, result.output
assert out_md.is_file()
content = out_md.read_text(encoding="utf-8")
assert "## Top Entities" in content
assert "## Top Co-occurrences" in content
assert "## Top Citations" in content
assert "## Concept Clusters" in content
assert "## Anomalies" in content
assert "BRCA1" in content
assert "10.1038/nature12345" in content
def test_export_markdown(chunk_dir: Path, tmp_path: Path) -> None:
graph = build_from_directory(chunk_dir)
save_json = tmp_path / "graph.json"
export_json(graph, save_json)
out_md = tmp_path / "report.md"
# Test with string paths as per signature requirement
written = export_markdown(str(save_json), str(out_md))
assert written == out_md
assert out_md.is_file()
text = out_md.read_text(encoding="utf-8")
assert "# Knowledge Graph Report" in text
assert "## Summary" in text
assert "## Top Entities" in text
assert "## Top Co-occurrences" in text
assert "## Top Citations" in text
assert "## Concept Clusters" in text
assert "## Anomalies" in text
assert "BRCA1" in text
assert "PARP" in text
assert "10.1038/nature12345" in text
assert "low_confidence" in text
# Also test passing in-memory graph directly
out_direct = tmp_path / "report_direct.md"
export_markdown(graph, out_direct)
assert out_direct.is_file()
assert "## Top Entities" in out_direct.read_text(encoding="utf-8")
def test_chunks_for_entity(chunk_dir: Path) -> None:
from kg_ocr.graph import chunks_for_entity