File size: 2,078 Bytes
e9a85fb | 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 | """CER / SER as defined in the SCAM paper (arXiv 2606.15987, Sec. 4).
CER: total edit distance / total ground-truth length, summed over the whole set.
Primary unit is the *grapheme cluster* (a letter plus its combining marks,
e.g. "ⲙ̇" counts as one), matching the paper. Code-point CER is also given.
cer_nospace: CER with all spaces removed from both sides (secondary; see EXPERIMENTS.md).
SER: total line-level mismatches / number of lines (scriptio continua, so a
"word" error rate is not meaningful).
"""
import unicodedata
from textnorm import normalize
def graphemes(s: str) -> list[str]:
out: list[str] = []
for ch in s:
if out and unicodedata.combining(ch):
out[-1] += ch
else:
out.append(ch)
return out
def edit_distance(a, b) -> int:
if len(a) < len(b):
a, b = b, a
prev = list(range(len(b) + 1))
for i, ca in enumerate(a, 1):
cur = [i]
for j, cb in enumerate(b, 1):
cur.append(min(prev[j] + 1, cur[j - 1] + 1, prev[j - 1] + (ca != cb)))
prev = cur
return prev[-1]
def score(pairs):
"""pairs: iterable of (prediction, ground_truth). Returns dict of percentages."""
g_err = g_len = c_err = c_len = s_err = n = ns_err = ns_len = 0
for pred, gt in pairs:
pred, gt = normalize(pred), normalize(gt)
pg, gg = graphemes(pred), graphemes(gt)
g_err += edit_distance(pg, gg)
g_len += len(gg)
c_err += edit_distance(pred, gt)
c_len += len(gt)
s_err += pred != gt
n += 1
pn, gn = graphemes(pred.replace(" ", "")), graphemes(gt.replace(" ", ""))
ns_err += edit_distance(pn, gn)
ns_len += len(gn)
return {
"cer": 100 * g_err / max(g_len, 1),
"cer_codepoint": 100 * c_err / max(c_len, 1),
"ser": 100 * s_err / max(n, 1),
# secondary: spaces removed on both sides (SCAM marks visual gaps / some pages word spaces inconsistently)
"cer_nospace": 100 * ns_err / max(ns_len, 1),
"lines": n,
}
|