File size: 815 Bytes
afecedd | 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 | """Shared unit-spelling normalisation (no data). Used by the runtime and by scripts/build.py."""
from __future__ import annotations
import re
from typing import Dict, List, Sequence, Tuple
Rule = Tuple["re.Pattern[str]", str]
def compile_rules(rules: Sequence[Dict[str, str]]) -> List[Rule]:
"""Compile rules whose replacements use JavaScript-style ``$1`` group references."""
out: List[Rule] = []
for r in rules:
repl = re.sub(r"\$(\d)", r"\\g<\1>", r["replacement"])
out.append((re.compile(r["pattern"]), repl))
return out
def unit_key(text: str, rules: Sequence[Rule]) -> str:
"""Apply the rules in order, then drop all whitespace. This is the lookup key."""
s = text.strip()
for pat, repl in rules:
s = pat.sub(repl, s)
return re.sub(r"\s+", "", s)
|