File size: 2,867 Bytes
09da05c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
"""Run fact extraction evaluation metrics."""

from __future__ import annotations

import argparse
import json
from pathlib import Path

from adsp.monitoring.evaluation_pipeline import FactExtractionEvaluator


REPO_ROOT = Path(__file__).resolve().parents[2]


def _default_system_output() -> Path:
    primary = REPO_ROOT / "data/processed/fact_data/pages"
    if primary.exists() and any(primary.glob("*.md")):
        return primary
    fallback = REPO_ROOT / "data/evaluation/fact_extraction/system_output"
    if fallback.exists() and any(fallback.glob("*.md")):
        return fallback
    return primary


def _default_ground_truth() -> Path:
    ground_truth_dir = REPO_ROOT / "data/evaluation/fact_extraction/ground_truth"
    if ground_truth_dir.exists() and any(ground_truth_dir.glob("*.json")):
        return ground_truth_dir
    return ground_truth_dir / "facts_ground_truth.json"


def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser(description="Evaluate fact extraction accuracy.")
    parser.add_argument(
        "--ground-truth-file",
        type=Path,
        default=_default_ground_truth(),
        help="JSON file or directory containing fact extraction ground truth.",
    )
    parser.add_argument(
        "--system-output",
        type=Path,
        default=_default_system_output(),
        help="Directory or markdown file containing system output.",
    )
    parser.add_argument(
        "--output",
        type=Path,
        default=REPO_ROOT / "data/evaluation/fact_extraction/evaluation_results.json",
        help="Where to write the evaluation results JSON.",
    )
    return parser.parse_args()


def main() -> int:
    args = parse_args()
    if not args.ground_truth_file.exists():
        raise FileNotFoundError(f"Ground truth path not found: {args.ground_truth_file}")
    if args.ground_truth_file.is_dir() and not any(args.ground_truth_file.glob("*.json")):
        raise FileNotFoundError(
            f"No ground truth JSON files found in: {args.ground_truth_file}"
        )
    if not args.system_output.exists():
        raise FileNotFoundError(f"System output not found: {args.system_output}")

    evaluator = FactExtractionEvaluator(
        ground_truth_file=args.ground_truth_file,
        system_output_path=args.system_output,
    )
    results = evaluator.run_evaluation()

    print("Fact Extraction Evaluation")
    print(f"Exact Match Accuracy: {results['exact_match_accuracy']:.2%}")
    print(f"Total Values: {results['total_values']}")
    print(f"Matched Values: {results['matched_values']}")

    args.output.parent.mkdir(parents=True, exist_ok=True)
    with args.output.open("w", encoding="utf-8") as handle:
        json.dump(results, handle, indent=2)
    print(f"Results saved to {args.output}")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())