from __future__ import annotations import argparse from collections import Counter, defaultdict from pathlib import Path from typing import Any from src.data.io_utils import read_jsonl, write_csv DEFAULT_LABEL_ORDER = ["SUPPORTS", "REFUTES", "NEI", "CONFLICTING"] def infer_split(path: Path, rows: list[dict[str, Any]]) -> str: splits = sorted({str(row.get("split")) for row in rows if row.get("split")}) if len(splits) == 1: return splits[0] stem = path.stem if stem.startswith("claims_"): return stem.removeprefix("claims_") return stem def distribution_rows(input_path: Path, label_order: list[str]) -> list[dict[str, Any]]: rows = read_jsonl(input_path) grouped: dict[str, list[dict[str, Any]]] = defaultdict(list) fallback_split = infer_split(input_path, rows) for row in rows: grouped[str(row.get("split") or fallback_split)].append(row) output_rows: list[dict[str, Any]] = [] for split, split_rows in sorted(grouped.items()): counts = Counter(str(row.get("label") or "UNLABELED") for row in split_rows) labels = list(label_order) labels.extend(label for label in sorted(counts) if label not in labels) total = len(split_rows) out: dict[str, Any] = {"Split": split, "Total": total} for label in labels: out[label] = counts.get(label, 0) for label in labels: out[f"{label}_pct"] = round(counts.get(label, 0) / max(1, total) * 100, 4) output_rows.append(out) return output_rows def main() -> None: parser = argparse.ArgumentParser() parser.add_argument("--input", type=Path, required=True) parser.add_argument("--output", type=Path, required=True) parser.add_argument("--label-order", nargs="*", default=DEFAULT_LABEL_ORDER) args = parser.parse_args() rows = distribution_rows(args.input, args.label_order) write_csv(args.output, rows) print(f"Wrote {len(rows)} label distribution rows to {args.output}") if __name__ == "__main__": main()