Download tests/test_evaluate.py from AjayRangoji/Translation_Guided_Sandhi_Splitting_Experiment_Guide: direct link, hf CLI and curl.
- Browser
- Download file 4.25 kB
-
https://huggingface.co/datasets/AjayRangoji/Translation_Guided_Sandhi_Splitting_Experiment_Guide/resolve/main/tests/test_evaluate.py
- Command line
-
hf download hf://datasets/AjayRangoji/Translation_Guided_Sandhi_Splitting_Experiment_Guide/tests/test_evaluate.py
-
curl -L -o test_evaluate.py https://huggingface.co/datasets/AjayRangoji/Translation_Guided_Sandhi_Splitting_Experiment_Guide/resolve/main/tests/test_evaluate.py
4.25 kB
| """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) | |