File size: 3,106 Bytes
bc3f388 | 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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | """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(" ", "")
|