ek.canonicalize#
Versioned, composable canonicalization applied before scoring.
Under-specified normalization is the single biggest source of bogus evaluation
scores – it can swing results by tens of points, and a reused English normalizer
catastrophically corrupts Indic/abugida scripts by stripping Unicode Mark-class
characters. So canonicalization is a first-class, versioned component here, not
a metric detail: a Canonicalizer is an ordered pipeline of small named
steps with a stable version you can persist alongside a gold set.
Steps are plain str -> str functions registered under the "normalizers"
namespace, so a schema can name a per-field normalizer (via
normalizer) and third parties can add their own.
Example
>>> canon = Canonicalizer(["nfc", "lower", "collapse_whitespace"])
>>> canon(" Héllo WORLD ")
'héllo world'
>>> canon.version # stable hash of the step names
'nfc+lower+collapse_whitespace'
Always audit before folding a non-Latin script – mark_class_count() reports
how many combining marks a string carries, the signal that aggressive folding
would destroy meaning:
>>> mark_class_count("e\u0301") # 'e' + combining acute
1
Functions
Collapse runs of whitespace to a single space and strip the ends. |
|
A conservative default: NFC -> casefold -> collapse whitespace. |
|
|
Casefold to lowercase (full Unicode casefold). |
Number of Unicode Mark-class (combining) characters in |
|
|
Unicode NFC normalization (compose); the safe default. |
|
Unicode NFKC normalization (compatibility compose); lossy -- use with care. |
|
Coerce a |
|
Strip leading/trailing whitespace. |
Remove Unicode punctuation characters (category starting with |
Classes
|
An ordered, named, versioned pipeline of |
- class ek.canonicalize.Canonicalizer(steps=(), *, version='', audit_marks=False)[source]#
Bases:
objectAn ordered, named, versioned pipeline of
str -> strsteps.- Parameters:
steps (
Sequence[Union[str,Callable[[str],str]]]) – Step references – names registered under"normalizers"or plain callables. Applied left to right.version (
str) – Override the auto-derived version string (the+-joined names).audit_marks (
bool) – If true, warn when a step would run on text carrying combining marks (the cross-script damage guard).
- ek.canonicalize.collapse_whitespace(s)[source]#
Collapse runs of whitespace to a single space and strip the ends.
- Return type:
- ek.canonicalize.default_canonicalizer()[source]#
A conservative default: NFC -> casefold -> collapse whitespace.
- Return type:
- ek.canonicalize.mark_class_count(s)[source]#
Number of Unicode Mark-class (combining) characters in
s.A non-zero count on a non-Latin script is a warning sign that case/diacritic folding will corrupt meaning (the Indic-script catastrophe). Use it to gate or audit a canonicalizer before trusting cross-script scores.
- Return type:
- ek.canonicalize.nfkc(s)[source]#
Unicode NFKC normalization (compatibility compose); lossy – use with care.
- Return type:
- ek.canonicalize.resolve_canonicalizer(normalize)[source]#
Coerce a
normalizeargument to a callable canonicalizer (orNone).Accepts
None, a registered normalizer name, a callable, a sequence of steps, or an existingCanonicalizer.- Return type:
Union[Canonicalizer,Callable[[str],str],None]