Add reference grader with set comparison and text normalisation

Compares answer lists against a reference as sets with precision and recall minimums; text is casefolded, NFC and whitespace normalised.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
torbenandClaude Sonnet 5.5 committed 2026-12-04 23:02:45 +01:00
1 parent a124734966
commit 9a7dc6ceb3
5 files changed
+369 -4

No files matched your search

+151 -1
View File
@@ -5,7 +5,7 @@ from pydantic import BaseModel, ConfigDict, Field, JsonValue
from chemenu.evals.example import ExampleOutput
from chemenu.evals.graders import PointerError, grade, resolve_pointer, score_case
from chemenu.evals.model import ContainsCheck, EqualsCheck, EvalCase
from chemenu.evals.model import ContainsCheck, EqualsCheck, EvalCase, ReferenceCheck, Scalar
GOOD = '{"language": "de", "entities": ["a", "b"]}'
@@ -108,3 +108,153 @@ def test_pointer_uses_field_alias() -> None:
result = score_case(case, '{"entityList": ["a"]}', _Aliased, 1.0)
assert result.error is None
assert result.passed
def _ref(
items: list[Scalar] | list[dict[str, Scalar]],
*,
where: dict[str, Scalar] | None = None,
min_precision: float = 0.0,
min_recall: float = 0.0,
path: str = "/e",
) -> ReferenceCheck:
return ReferenceCheck(
grader="reference",
path=path,
where=where or {},
items=items,
min_precision=min_precision,
min_recall=min_recall,
)
def _matched(check: ReferenceCheck, out: JsonValue) -> int:
match = grade(check, out).match
assert match is not None
return match.matched
def test_reference_counts_precision_and_recall() -> None:
out: JsonValue = {"e": ["a", "b", "x", "y"]}
result = grade(_ref(["a", "b", "c"], min_precision=0.5, min_recall=0.6), out)
assert result.label == "reference /e"
assert result.match is not None
assert (result.match.matched, result.match.actual, result.match.expected) == (2, 4, 3)
assert result.match.precision == 0.5
assert result.match.recall == pytest.approx(2 / 3)
assert result.match.missing == ("c",)
assert result.match.surplus == ("x", "y")
assert result.passed
assert result.detail == ""
failed = grade(_ref(["a", "b", "c"], min_precision=0.6, min_recall=0.7), out)
assert not failed.passed
assert failed.detail == "precision 0.500 (2/4) < 0.6; recall 0.667 (2/3) < 0.7"
def test_reference_compares_named_fields_only() -> None:
out: JsonValue = {
"e": [
{"name": "A", "kind": "x", "extra": 1},
{"name": "B", "kind": "y", "extra": 2},
{"name": "C"},
"plain",
{"name": "D", "kind": [1]},
]
}
result = grade(_ref([{"name": "a", "kind": "X"}, {"name": "Z", "kind": "x"}]), out)
assert result.match is not None
assert result.match.matched == 1
assert result.match.actual == 5
assert result.match.missing == ({"name": "Z", "kind": "x"},)
assert result.match.surplus == (
{"name": "B", "kind": "y"},
{"name": "C"},
"plain",
{"name": "D", "kind": [1]},
)
def test_reference_where_filters_entries() -> None:
out: JsonValue = {
"e": [
{"kind": "entity", "name": "a"},
{"kind": "other", "name": "b"},
"scalar",
{"name": "c"},
]
}
result = grade(_ref([{"name": "a"}], where={"kind": "ENTITY"}), out)
assert result.label == "reference /e[kind=ENTITY]"
assert result.match is not None
assert (result.match.matched, result.match.actual) == (1, 1)
two = grade(_ref([{"name": "a"}], where={"kind": "entity", "n": 1}), out)
assert two.label == "reference /e[kind=entity,n=1]"
def test_reference_normalizes_text() -> None:
out: JsonValue = {"e": ["STRASSE", "e\u0301", "a\u00a0 b", " c "]}
result = grade(_ref(["Stra\u00dfe", "\u00c9", "a b", "C"], min_precision=1, min_recall=1), out)
assert result.passed
assert _matched(_ref(["x2"]), {"e": ["x\u00b2"]}) == 0
def test_reference_numbers_and_bools_exact() -> None:
assert _matched(_ref([1]), {"e": [1.0]}) == 1
assert _matched(_ref([1]), {"e": [True]}) == 0
assert _matched(_ref([True]), {"e": [1]}) == 0
assert _matched(_ref([True]), {"e": [True]}) == 1
assert _matched(_ref([1.5]), {"e": [1.5000001]}) == 0
assert _matched(_ref([None]), {"e": [None]}) == 1
assert _matched(_ref([None]), {"e": ["", 0, False]}) == 0
def test_reference_duplicates_collapse() -> None:
result = grade(_ref(["a", "b"]), {"e": ["a", "A", "a", "x", "x"]})
assert result.match is not None
assert (result.match.matched, result.match.actual) == (1, 2)
assert result.match.surplus == ("x",)
def test_reference_empty_is_undefined() -> None:
both = grade(_ref([], min_precision=1, min_recall=1), {"e": []})
assert both.passed
assert both.match is not None
assert both.match.precision is None
assert both.match.recall is None
no_answer = grade(_ref(["a"], min_precision=1, min_recall=0.5), {"e": []})
assert not no_answer.passed
assert no_answer.match is not None
assert no_answer.match.precision is None
assert no_answer.match.recall == 0.0
assert no_answer.detail == "recall 0.000 (0/1) < 0.5"
no_ref = grade(_ref([], min_precision=0.5, min_recall=1), {"e": ["a"]})
assert not no_ref.passed
assert no_ref.match is not None
assert no_ref.match.recall is None
assert no_ref.detail == "precision 0.000 (0/1) < 0.5"
def test_reference_minimums_decide() -> None:
out: JsonValue = {"e": ["a", "x"]}
assert grade(_ref(["a", "b"], min_precision=0.5, min_recall=0.5), out).passed
assert not grade(_ref(["a", "b"], min_precision=0.51, min_recall=0.5), out).passed
assert not grade(_ref(["a", "b"], min_precision=0.5, min_recall=0.51), out).passed
def test_reference_not_a_list_fails_check() -> None:
result = grade(_ref(["a"]), {"e": "a"})
assert not result.passed
assert result.detail == "value is not a list: 'a'"
assert result.match is None
def test_reference_unfit_surplus_is_reduced() -> None:
out: JsonValue = {"e": [{"name": ["A"], "kind": "x", "note": "long"}, {"note": "only"}]}
result = grade(_ref([{"name": "a", "kind": "x"}]), out)
assert result.match is not None
assert result.match.surplus == ({"name": ["A"], "kind": "x"}, {"note": "only"})
def test_reference_detail_shows_counts() -> None:
result = grade(_ref(["a", "b", "c"], min_recall=0.667), {"e": ["a", "b"]})
assert result.detail == "recall 0.667 (2/3) < 0.667"