Download scripts/build_graph.py from continuum-oss/ccrb: direct link, hf CLI and curl.
- Browser
- Download file 2.12 kB
-
https://huggingface.co/datasets/continuum-oss/ccrb/resolve/main/scripts/build_graph.py
- Command line
-
hf download hf://datasets/continuum-oss/ccrb/scripts/build_graph.py
-
curl -L -o build_graph.py https://huggingface.co/datasets/continuum-oss/ccrb/resolve/main/scripts/build_graph.py
2.12 kB
| """Build bidirectional cross-source adjacency index from data/graph/edges.jsonl.""" | |
| from __future__ import annotations | |
| import argparse | |
| import json | |
| import sys | |
| from collections import defaultdict | |
| from pathlib import Path | |
| from typing import Any | |
| _REPO_ROOT = Path(__file__).resolve().parent.parent | |
| if str(_REPO_ROOT) not in sys.path: | |
| sys.path.insert(0, str(_REPO_ROOT)) | |
| from scripts.pipeline_utils import load_jsonl | |
| def build_adjacency(edges_path: Path, output_path: Path) -> dict[str, Any]: | |
| """Build forward and reverse adjacency maps for multi-grade qrel construction.""" | |
| forward: dict[str, dict[str, list[str]]] = defaultdict(lambda: defaultdict(list)) | |
| reverse: dict[str, dict[str, list[str]]] = defaultdict(lambda: defaultdict(list)) | |
| edge_count = 0 | |
| for _ln, edge in load_jsonl(edges_path): | |
| src = str(edge.get("src", "")).strip() | |
| rel = str(edge.get("rel", "")).strip() | |
| dst = str(edge.get("dst", "")).strip() | |
| if not src or not rel or not dst: | |
| continue | |
| if dst not in forward[src][rel]: | |
| forward[src][rel].append(dst) | |
| if src not in reverse[dst][rel]: | |
| reverse[dst][rel].append(src) | |
| edge_count += 1 | |
| payload = { | |
| "edge_count": edge_count, | |
| "forward": {k: dict(v) for k, v in forward.items()}, | |
| "reverse": {k: dict(v) for k, v in reverse.items()}, | |
| } | |
| output_path.parent.mkdir(parents=True, exist_ok=True) | |
| output_path.write_text(json.dumps(payload), encoding="utf-8") | |
| return {"edge_count": edge_count, "nodes_out": len(forward), "nodes_in": len(reverse)} | |
| def main(argv: list[str] | None = None) -> int: | |
| parser = argparse.ArgumentParser(description="Build graph adjacency index from edges.jsonl.") | |
| parser.add_argument("--edges", type=Path, default=Path("data/graph/edges.jsonl")) | |
| parser.add_argument("--output", type=Path, default=Path("data/graph/adjacency.json")) | |
| args = parser.parse_args(argv) | |
| stats = build_adjacency(args.edges, args.output) | |
| print(json.dumps(stats, indent=2)) | |
| return 0 | |
| if __name__ == "__main__": | |
| sys.exit(main()) | |