turkic-ocr-code / tests_r5eval /test_scorelib_j2_unchanged.py
treoa's picture
Release 1
24aaa76
Raw History Blame Contribute Delete
4.14 kB
"""Unit tests for scorelib.py. usage: python -I test_scorelib.py PATH_TO_SCORELIB_PY"""
import importlib.util
import random
import sys
spec = importlib.util.spec_from_file_location("scorelib", sys.argv[1])
sl = importlib.util.module_from_spec(spec)
spec.loader.exec_module(sl)
def lev_full(a, b):
"""Independent full-matrix Levenshtein (memoised recursion over indices)."""
import functools
sys.setrecursionlimit(10000)
@functools.lru_cache(maxsize=None)
def f(i, j):
if i == 0:
return j
if j == 0:
return i
return min(f(i - 1, j) + 1, f(i, j - 1) + 1, f(i - 1, j - 1) + (a[i - 1] != b[j - 1]))
return f(len(a), len(b))
assert sl.align("kitten", "sitting")[0] == 3
assert sl.align("", "abc") == (3, 0, 0, 3)
assert sl.align("abc", "") == (3, 0, 3, 0)
assert sl.align("abc", "abc") == (0, 0, 0, 0)
d, s, dl, ins = sl.align("қазақ", "казак")
assert d == 2 and s == 2 and dl == 0 and ins == 0, (d, s, dl, ins)
# tie-break: ref "ab" hyp "b" -> one deletion; ref "b" hyp "ab" -> one insertion
assert sl.align("ab", "b") == (1, 0, 1, 0)
assert sl.align("b", "ab") == (1, 0, 0, 1)
# substitution preferred over delete+insert: "ab" vs "ba" costs 2 either way (2 subs vs del+ins)
assert sl.align("ab", "ba") == (2, 2, 0, 0), sl.align("ab", "ba")
random.seed(1)
alphabet = "аәбвгғдеёжзийкқлмнңоөпрстуұүфхһцчшщъыіьэюяabc 1"
for _ in range(3000):
a = "".join(random.choice(alphabet) for _ in range(random.randint(0, 12)))
b = "".join(random.choice(alphabet) for _ in range(random.randint(0, 16)))
d, s, dl, ins = sl.align(a, b)
assert d == lev_full(a, b), (a, b)
assert s + dl + ins == d
# lengths: len(b) - len(a) = ins - del
assert len(b) - len(a) == ins - dl, (a, b, s, dl, ins)
# wrappers and normalisation
assert sl.prep_output("```text\nсалықты\n```") == "салықты"
assert sl.prep_output("<text><loc_10><loc_20>Еңбек</text>") == "Еңбек"
assert sl.prep_output(" а\n б ") == "а б"
assert sl.prep_output("слово<|im_end|>") == "слово"
assert sl.prep_output("## Office announcement") == "Office announcement"
assert sl.prep_output("# 1") == "1"
assert sl.prep_output("слово # 5") == "слово # 5" # a # inside the line is text
assert sl.prep_output("图片中的文本内容是:Kreinopen") == "Kreinopen"
assert sl.prep_output("图片中的文本内容是: 1") == "1"
assert sl.prep_output("$\\text{Клеин орн.}$") == "Клеин орн."
assert sl.prep_output("$\\mathcal{H}$") == "$\\mathcal{H}$" # other LaTeX is not touched
assert sl.prep_output("字词") == "字词" # no colon: kept, counts as an error and as other script
assert sl.prep_ref(" й ") == "й" # NFC: и + breve -> й
# loop
assert sl.has_loop("abc", "abcdabcdabcdabcd") is True
assert sl.has_loop("abcdabcdabcd", "abcdabcdabcd") is False # run occurs in the reference
assert sl.has_loop("abc", "aaaa") is False # block of 1 repeated, not 4+
assert sl.has_loop("abc", "aaaaaaaaaaaa") is True # block "aaaa" x 3
assert sl.has_loop("x", "слово слово слово слово") is True
assert sl.has_loop("x", "ab ab ab") is False # block "ab " is 3 chars
# scripts
assert sl.other_script("слово") is False and sl.other_script("word1") is False
assert sl.other_script("слово字") is True and sl.other_script("كلمة") is True
# crop score and HR3
r = sl.score_crop("слово", "слово слово слово слово слово")
assert r["hr3"] is True and r["loop"] is True and r["ref_ge5"] is True
r = sl.score_crop("1", "11")
assert r["hr3"] is False and r["cer"] == 1.0
r = sl.score_crop("слово", "")
assert r["empty"] is True and r["cer"] == 1.0 and r["dist"] == 5
agg = sl.aggregate([sl.score_crop("слово", "слово"), sl.score_crop("ab", "abc")])
assert abs(agg["cer_pooled"] - 1 / 7) < 1e-12 and agg["exact"] == 0.5 and abs(agg["hr2_ins_per_ref_char"] - 1 / 7) < 1e-12
print("all scorelib tests passed")