| from __future__ import annotations |
|
|
| import argparse |
| import difflib |
| from collections import defaultdict |
| from pathlib import Path |
| from typing import Any |
|
|
| import yaml |
|
|
| from src.data.io_utils import read_jsonl, write_csv |
| from src.data.normalize_text import normalize_whitespace |
|
|
|
|
| def normalize_text(text: str | None) -> str: |
| return " ".join(normalize_whitespace(text).casefold().split()) |
|
|
|
|
| def fuzzy_score(a: str, b: str) -> float: |
| if not a or not b: |
| return 0.0 |
| if a in b or b in a: |
| return 1.0 |
| return difflib.SequenceMatcher(a=a, b=b).ratio() |
|
|
|
|
| def load_claim_chunks(context_chunks_path: Path) -> dict[str, list[dict[str, Any]]]: |
| by_claim: dict[str, list[dict[str, Any]]] = defaultdict(list) |
| for row in read_jsonl(context_chunks_path): |
| claim_id = normalize_whitespace((row.get("metadata") or {}).get("claim_id")) |
| if claim_id: |
| by_claim[claim_id].append(row) |
| return by_claim |
|
|
|
|
| def coverage_rows_for_split( |
| claims_path: Path, |
| context_chunks_path: Path, |
| gold_path: Path, |
| split_name: str | None, |
| threshold: float, |
| ) -> list[dict[str, Any]]: |
| claim_ids = {normalize_whitespace(row.get("claim_id")) for row in read_jsonl(claims_path)} |
| claim_chunks = load_claim_chunks(context_chunks_path) |
| rows: list[dict[str, Any]] = [] |
|
|
| for gold in read_jsonl(gold_path): |
| claim_id = normalize_whitespace(gold.get("claim_id")) |
| if claim_id not in claim_ids: |
| continue |
| evidence_text = normalize_whitespace(gold.get("text")) |
| evidence_norm = normalize_text(evidence_text) |
| best_chunk_id = "" |
| best_score = 0.0 |
| for chunk in claim_chunks.get(claim_id, []): |
| chunk_text = normalize_whitespace(chunk.get("text")) |
| score = fuzzy_score(evidence_norm, normalize_text(chunk_text)) |
| if score > best_score: |
| best_score = score |
| best_chunk_id = normalize_whitespace(chunk.get("chunk_id")) |
| rows.append( |
| { |
| "claim_id": claim_id, |
| "evidence_text": evidence_text, |
| "best_context_chunk_id": best_chunk_id, |
| "fuzzy_score": round(best_score, 6), |
| "covered": best_score >= threshold, |
| "coverage_threshold": threshold, |
| "split": split_name or normalize_whitespace(gold.get("split")), |
| } |
| ) |
| return rows |
|
|
|
|
| def rows_from_config(config_path: Path, threshold: float) -> list[dict[str, Any]]: |
| cfg = yaml.safe_load(config_path.read_text(encoding="utf-8")) |
| dataset_cfg = cfg["datasets"]["vifactcheck"] |
| gold_path = Path(dataset_cfg["gold"]) |
| context_chunks_path = Path(dataset_cfg["corpus"]) |
|
|
| rows: list[dict[str, Any]] = [] |
| for split, claims_path in dataset_cfg["queries"].items(): |
| rows.extend( |
| coverage_rows_for_split( |
| claims_path=Path(claims_path), |
| context_chunks_path=context_chunks_path, |
| gold_path=gold_path, |
| split_name=split, |
| threshold=threshold, |
| ) |
| ) |
| return rows |
|
|
|
|
| def main() -> None: |
| parser = argparse.ArgumentParser() |
| parser.add_argument("--config", type=Path, default=Path("configs/retrieval/candidate_pool.yaml")) |
| parser.add_argument("--claims", type=Path, default=None) |
| parser.add_argument("--context-chunks", type=Path, default=None) |
| parser.add_argument("--gold", type=Path, default=None) |
| parser.add_argument("--split-name", type=str, default=None) |
| parser.add_argument("--threshold", type=float, default=0.75) |
| parser.add_argument( |
| "--output", |
| type=Path, |
| default=Path("outputs/stats/vifactcheck_context_gold_coverage_report.csv"), |
| ) |
| args = parser.parse_args() |
|
|
| if args.claims or args.context_chunks or args.gold: |
| if not (args.claims and args.context_chunks and args.gold): |
| parser.error("--claims, --context-chunks, and --gold must be provided together.") |
| rows = coverage_rows_for_split( |
| claims_path=args.claims, |
| context_chunks_path=args.context_chunks, |
| gold_path=args.gold, |
| split_name=args.split_name, |
| threshold=args.threshold, |
| ) |
| else: |
| rows = rows_from_config(args.config, threshold=args.threshold) |
|
|
| write_csv(args.output, rows) |
| print(f"Wrote {len(rows)} rows to {args.output}") |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|