Mirror of lifefrom-units 1.1.0 (Zenodo doi:10.5281/zenodo.24568184, GitHub caraulani/lifefrom-units)
afecedd verified Download python/src/lifefrom_units/_rules.py from lifefrom/lifefrom-units: direct link, hf CLI and curl.
- Browser
- Download file 815 Bytes
-
https://huggingface.co/datasets/lifefrom/lifefrom-units/resolve/main/python/src/lifefrom_units/_rules.py
- Command line
-
hf download hf://datasets/lifefrom/lifefrom-units/python/src/lifefrom_units/_rules.py
-
curl -L -o _rules.py https://huggingface.co/datasets/lifefrom/lifefrom-units/resolve/main/python/src/lifefrom_units/_rules.py
815 Bytes
| """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) | |