Download src/data/normalize.py from AjayRangoji/Translation_Guided_Sandhi_Splitting_Experiment_Guide: direct link, hf CLI and curl.
- Browser
- Download file 3.11 kB
-
https://huggingface.co/datasets/AjayRangoji/Translation_Guided_Sandhi_Splitting_Experiment_Guide/resolve/main/src/data/normalize.py
- Command line
-
hf download hf://datasets/AjayRangoji/Translation_Guided_Sandhi_Splitting_Experiment_Guide/src/data/normalize.py
-
curl -L -o normalize.py https://huggingface.co/datasets/AjayRangoji/Translation_Guided_Sandhi_Splitting_Experiment_Guide/resolve/main/src/data/normalize.py
3.11 kB
| """Normalisation to the project's single internal representation. | |
| Internal representation (DECISIONS.md D03/D15): | |
| IAST, NFC-normalised, lowercase, no dandas, no verse numbers, | |
| single spaces, stripped. | |
| Every loader must pass its text through `normalize_iast` (already-IAST sources, | |
| e.g. DCS and the chronbmm benchmarks) or `devanagari_to_iast` followed by | |
| `normalize_iast` (Devanagari sources, e.g. Itihasa). | |
| """ | |
| import re | |
| import unicodedata | |
| from indic_transliteration import sanscript | |
| from indic_transliteration.sanscript import transliterate | |
| # Dandas, avagraha, verse numerals, editorial brackets and the om sign. | |
| _DANDA = "।॥" # danda, double danda | |
| _OM = "ॐ" # devanagari om | |
| _EDITORIAL = "[](){}<>*†‡|" | |
| # Devanagari digits plus ASCII digits (verse numbers). | |
| _DIGITS = "0-9०-९" | |
| _STRIP_RE = re.compile(f"[{re.escape(_DANDA + _EDITORIAL)}{_DIGITS}]") | |
| _WS_RE = re.compile(r"\s+") | |
| def devanagari_to_iast(text: str) -> str: | |
| """Transliterate Devanagari to IAST. Input must be Devanagari.""" | |
| return transliterate(text, sanscript.DEVANAGARI, sanscript.IAST) | |
| def normalize_iast(text: str) -> str: | |
| """Canonicalise an IAST string to the internal representation. | |
| NFC first so that combining diacritics are composed consistently; a byte | |
| level model sees NFC and NFD as different inputs (D03). | |
| """ | |
| if text is None: | |
| return "" | |
| text = unicodedata.normalize("NFC", text) | |
| text = text.replace(_OM, "oṃ") | |
| text = _STRIP_RE.sub(" ", text) | |
| text = text.lower() | |
| text = _WS_RE.sub(" ", text).strip() | |
| return text | |
| def normalize_devanagari(text: str) -> str: | |
| """Devanagari -> internal IAST representation.""" | |
| if text is None: | |
| return "" | |
| text = unicodedata.normalize("NFC", text) | |
| # Strip before transliterating so dandas/numerals never become stray IAST. | |
| text = _STRIP_RE.sub(" ", text) | |
| return normalize_iast(devanagari_to_iast(text)) | |
| def normalize_english(text: str) -> str: | |
| """Light normalisation for the English side: NFC + whitespace only. | |
| Casing and punctuation are preserved here; the dictionary scorer does its | |
| own lowercasing and lemmatisation. | |
| """ | |
| if text is None: | |
| return "" | |
| return _WS_RE.sub(" ", unicodedata.normalize("NFC", text)).strip() | |
| # Nasal class folding, for MATCHING ONLY (never for model input). | |
| # Editions differ freely between anusvara and the homorganic nasal: | |
| # DCS writes "munipu<ms>gavam", Itihasa writes "munipu<n>gavam" for the same | |
| # verse. Folding them keeps such pairs above the 0.95 threshold (D04/D16). | |
| _NASAL_FOLD = str.maketrans({c: "ṃ" for c in "ṁṃṅñṇ"}) | |
| def collapse_for_match(text: str) -> str: | |
| """Key used for fuzzy overlap matching: internal IAST, spaces removed, | |
| nasals folded. | |
| Editions differ in where they put word boundaries in the sandhied text and | |
| in which nasal they print, so neither must count against similarity (D04). | |
| This key is for matching only and is never fed to the model. | |
| """ | |
| return normalize_iast(text).translate(_NASAL_FOLD).replace(" ", "") | |