from __future__ import annotations import argparse from pathlib import Path from typing import Any from src.data.io_utils import read_jsonl def compact(text: Any, limit: int = 240) -> str: value = " ".join(str(text or "").split()) if len(value) <= limit: return value clipped = value[:limit].rsplit(" ", 1)[0].strip() return f"{clipped}..." def render_prediction_case(row: dict[str, Any]) -> list[str]: lines = [ f"### {row.get('category', '')}: {row.get('id', '')}", f"- Claim: {row.get('claim', '')}", f"- Gold / baseline / WikiKG / alternate: `{row.get('gold', '')}` / `{row.get('baseline_prediction', '')}` / `{row.get('wikikg_prediction', '')}` / `{row.get('alternate_prediction', '')}`", f"- Verified support: `{row.get('num_verified_facts', 0)}` facts, `{row.get('num_verified_triples', 0)}` triples", ] path_summary = row.get("path_summary", {}) if path_summary: lines.append( "- Path summary: " f"max_final={path_summary.get('max_final_score', '')}, " f"max_kg_path={path_summary.get('max_kg_path_score', '')}, " f"max_provenance={path_summary.get('max_provenance_confidence', '')}" ) if row.get("top_evidence"): lines.append("- Top evidence:") for item in row["top_evidence"][:3]: lines.append(f" - [{item.get('candidate_id', '')}] {compact(item.get('text', ''))}") if row.get("top_verified_paths"): lines.append("- Top verified paths:") for item in row["top_verified_paths"][:3]: lines.append(f" - {item.get('path_text', '')}") lines.append(f" - Source: {compact(item.get('source_text', ''))}") if row.get("top_unsupported_triples"): lines.append("- Unsupported triples:") for item in row["top_unsupported_triples"][:2]: lines.append(f" - {item.get('path_text', '')} [{item.get('nli_label', '')}]") lines.append("") return lines def render_relation_case(row: dict[str, Any]) -> list[str]: lines = [ f"### {row.get('category', '')}: {row.get('id', '')}", f"- Claim: {row.get('claim', '')}", f"- Gold / baseline / WikiKG: `{row.get('gold', '')}` / `{row.get('baseline_prediction', '')}` / `{row.get('wikikg_prediction', '')}`", f"- Relation: `{row.get('relation_original', '') or row.get('relation', '')}` -> `{row.get('relation', '')}`", f"- NLI / entailment: `{row.get('nli_label', '')}` / `{row.get('entailment_score', '')}`", f"- Triple: {row.get('verbalized_triple', '')}", f"- Source: {compact(row.get('source_text', ''), limit=320)}", "", ] return lines def render_section(title: str, rows: list[dict[str, Any]], relation_mode: bool = False) -> list[str]: lines = [f"## {title}", ""] if not rows: lines.append("No cases selected.") lines.append("") return lines current_category = None for row in rows: if row.get("category") != current_category: current_category = row.get("category") lines.append(f"### Group: {current_category}") lines.append("") lines.extend(render_relation_case(row) if relation_mode else render_prediction_case(row)) return lines def main() -> None: parser = argparse.ArgumentParser() parser.add_argument("--averitec", type=Path, required=True) parser.add_argument("--healthver", type=Path, required=True) parser.add_argument("--vifactcheck", type=Path, required=True) parser.add_argument("--output", type=Path, default=Path("outputs/analysis/case_studies.md")) args = parser.parse_args() averitec_rows = read_jsonl(args.averitec) healthver_rows = read_jsonl(args.healthver) vifactcheck_rows = read_jsonl(args.vifactcheck) lines = ["# Stage 11 Case Studies", ""] lines.extend(render_section("AVeriTeC", averitec_rows)) lines.extend(render_section("HealthVer", healthver_rows, relation_mode=True)) lines.extend(render_section("ViFactCheck", vifactcheck_rows)) args.output.parent.mkdir(parents=True, exist_ok=True) args.output.write_text("\n".join(lines).strip() + "\n", encoding="utf-8") print(f"Wrote case studies to {args.output}") if __name__ == "__main__": main()