File size: 4,249 Bytes
235fa6b | 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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | """Hand-worked tests for the evaluator. Every expected number below was
computed by hand first, then checked against the code (guide: "Check it on
examples you worked out by hand before trusting any number")."""
import sys, os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "src"))
from evaluate import evaluate, token_counts, boundary_offsets, prf # noqa
def approx(a, b, tol=1e-6):
assert abs(a - b) < tol, f"{a} != {b}"
def test_perfect_prediction_is_100():
gold = "tapas svādhyāya niratam tapasvī"
r = evaluate([(gold, gold, "tapaḥsvādhyāyanirataṃ tapasvī")])
approx(r["token_precision"], 100.0)
approx(r["token_recall"], 100.0)
approx(r["token_f1"], 100.0)
approx(r["sentence_exact_match"], 100.0)
approx(r["split_location_f1"], 100.0)
assert r["split_location_align_failures"] == 0
def test_empty_prediction_is_zero_not_crash():
r = evaluate([("", "tapas svādhyāya niratam", "tapaḥsvādhyāyanirataṃ")])
approx(r["token_precision"], 0.0)
approx(r["token_recall"], 0.0)
approx(r["token_f1"], 0.0)
approx(r["sentence_exact_match"], 0.0)
def test_empty_gold_does_not_divide_by_zero():
r = evaluate([("a b", "")])
approx(r["token_recall"], 0.0)
approx(r["token_f1"], 0.0)
def test_multiset_not_set_matching():
# gold has "ca" twice; a prediction with one "ca" must NOT get credit for
# two. Hand count: matched = {kaḥ:1, ca:1} = 2, n_pred = 2, n_gold = 3.
m, np_, ng = token_counts("kaḥ ca", "kaḥ ca ca")
assert (m, np_, ng) == (2, 2, 3), (m, np_, ng)
p, r, f = prf(m, np_, ng)
approx(p, 1.0)
approx(r, 2 / 3)
approx(f, 2 * 1.0 * (2 / 3) / (1.0 + 2 / 3))
def test_set_matching_would_have_inflated_this():
# If the evaluator used sets, this would score recall 1.0. Multiset gives
# 2/4. Guards against a silent regression to set semantics.
m, np_, ng = token_counts("ca na", "ca ca na na")
assert (m, np_, ng) == (2, 2, 4)
def test_partial_credit_hand_computed():
# gold : tapas svādhyāya niratam tapasvī (4 words)
# pred : tapas svādhyāyaniratam tapasvī (3 words)
# matched = tapas, tapasvī = 2
# precision = 2/3, recall = 2/4
m, np_, ng = token_counts(
"tapas svādhyāyaniratam tapasvī", "tapas svādhyāya niratam tapasvī")
assert (m, np_, ng) == (2, 3, 4)
p, r, _ = prf(m, np_, ng)
approx(p, 2 / 3)
approx(r, 0.5)
def test_exact_match_is_order_sensitive():
r = evaluate([("b a", "a b")])
approx(r["sentence_exact_match"], 0.0) # wrong order is not exact
approx(r["token_f1"], 100.0) # but tokens all present
def test_boundary_offsets_simple():
# sandhied "abcdef", split "abc def" -> one boundary after 3 chars
assert boundary_offsets("abc def", "abcdef") == {3}
# three words -> two boundaries
assert boundary_offsets("ab cd ef", "abcdef") == {2, 4}
# single word -> no internal boundary
assert boundary_offsets("abcdef", "abcdef") == set()
def test_split_location_ignores_sandhi_form():
# Right boundary position, wrong surface form (a "form error" in the
# guide's error taxonomy). Token F1 is penalised; split location is not.
sandhied = "ko nv asmin"
gold = "kaḥ nu asmin"
pred = "kas nu asmin"
r = evaluate([(pred, gold, sandhied)])
assert r["token_f1"] < 100.0
approx(r["split_location_f1"], 100.0)
def test_aggregate_over_several_verses():
pairs = [
("a b", "a b", "ab"), # perfect
("a", "a b", "ab"), # recall miss
]
r = evaluate(pairs)
# matched 3, pred 3, gold 4 -> P=1.0, R=0.75
approx(r["token_precision"], 100.0)
approx(r["token_recall"], 75.0)
approx(r["sentence_exact_match"], 50.0)
assert r["n"] == 2
if __name__ == "__main__":
fails = 0
for name, fn in sorted(globals().items()):
if name.startswith("test_") and callable(fn):
try:
fn()
print(f"PASS {name}")
except AssertionError as e:
fails += 1
print(f"FAIL {name}: {e}")
print()
print("ALL PASS" if not fails else f"{fails} FAILURES")
sys.exit(1 if fails else 0)
|