feat: sb verses extracted
This commit is contained in:
+40
-9
@@ -86,18 +86,49 @@ def normalize_devanagari_anusvara(text: str) -> str:
|
||||
|
||||
|
||||
def join_virama_words(text: str) -> str:
|
||||
"""Join Devanagari words where the previous word ends with virāma (्)
|
||||
and the next word starts with a consonant.
|
||||
"""Join Devanagari words where the previous word ends with virāma (्).
|
||||
|
||||
In standard Devanagari verse rendering, a word ending in a halant
|
||||
(virāma) connects to the following consonant-initial word without a space.
|
||||
e.g., श्रीर् विजयो → श्रीर्विजयो
|
||||
Handles both virāma+consonant and virāma+vowel cases.
|
||||
In Devanagari script, a halant (virāma) consonant always absorbs the
|
||||
following letter — whether consonant or vowel — without a space.
|
||||
|
||||
But when followed by a vowel, the space is preserved:
|
||||
e.g., किम् अकुर्वत → किम् अकुर्वत (stays separate)
|
||||
virāma + consonant: श्रीर् विजयो → श्रीर्विजयो
|
||||
virāma + vowel: किम् अकुर्वत → किमकुर्वत
|
||||
तैर् अलातायुधैः → तैरलातायुधैः
|
||||
माम् एकं → मामेकं
|
||||
|
||||
Uses [ \\t]+ instead of \\s+ to avoid joining across newlines (line breaks
|
||||
separate paired pādas and must be preserved).
|
||||
"""
|
||||
# Devanagari consonants: क (U+0915) through ह (U+0939)
|
||||
return re.sub(r"्\s+([क-ह])", r"्\1", text)
|
||||
# Join virāma + space + consonant (क U+0915 through ह U+0939)
|
||||
text = re.sub(r"्[ \t]+([क-ह])", r"्\1", text)
|
||||
|
||||
# Join virāma + space + independent vowel (अ U+0905 through औ U+0914, plus ॠ ॡ)
|
||||
# Drop the virāma and replace the independent vowel with its dependent mātrā form.
|
||||
_VOWEL_TO_MATRA = {
|
||||
"अ": "", # inherent vowel — just drop virāma and the अ
|
||||
"आ": "ा",
|
||||
"इ": "ि",
|
||||
"ई": "ी",
|
||||
"उ": "ु",
|
||||
"ऊ": "ू",
|
||||
"ऋ": "ृ",
|
||||
"ॠ": "ॄ",
|
||||
"ऌ": "ॢ",
|
||||
"ॡ": "ॣ",
|
||||
"ए": "े",
|
||||
"ऐ": "ै",
|
||||
"ओ": "ो",
|
||||
"औ": "ौ",
|
||||
}
|
||||
|
||||
def _virama_vowel_replace(m):
|
||||
vowel = m.group(1)
|
||||
matra = _VOWEL_TO_MATRA.get(vowel, vowel)
|
||||
return matra
|
||||
|
||||
text = re.sub(r"्[ \t]+([अ-औॠॡ])", _virama_vowel_replace, text)
|
||||
return text
|
||||
|
||||
|
||||
def iast_to_devanagari(iast_text: str) -> str:
|
||||
|
||||
+46
-2
@@ -85,8 +85,52 @@ def normalize_devanagari_anusvara(text: str) -> str:
|
||||
|
||||
|
||||
def join_virama_words(text: str) -> str:
|
||||
"""Join Devanagari words where the previous word ends with virāma."""
|
||||
return re.sub(r"्\s+([क-ह])", r"्\1", text)
|
||||
"""Join Devanagari words where the previous word ends with virāma.
|
||||
|
||||
Handles both virāma+consonant and virāma+vowel cases.
|
||||
In Devanagari script, a halant (virāma) consonant always absorbs the
|
||||
following letter — whether consonant or vowel — without a space.
|
||||
|
||||
virāma + consonant: श्रीर् विजयो → श्रीर्विजयो
|
||||
virāma + vowel: तैर् अलातायुधैः → तैरलातायुधैः
|
||||
"""
|
||||
# Join virāma + space + consonant (क U+0915 through ह U+0939)
|
||||
# Use [ \t]+ instead of \s+ to avoid joining across newlines (line breaks
|
||||
# separate paired pādas and must be preserved).
|
||||
text = re.sub(r"्[ \t]+([क-ह])", r"्\1", text)
|
||||
# Join virāma + space + independent vowel (अ U+0905 through औ U+0914, plus ॠ ॡ)
|
||||
# The transliterator will have produced virāma on the consonant and a separate
|
||||
# independent vowel glyph. We need to:
|
||||
# 1. Drop the virāma (the consonant takes the vowel)
|
||||
# 2. Replace the independent vowel with its dependent mātrā form
|
||||
# However, a simpler approach: just remove the space and let the
|
||||
# virāma+independent-vowel stay — this is visually wrong.
|
||||
# Instead, we do the proper mapping:
|
||||
_VOWEL_TO_MATRA = {
|
||||
"अ": "", # inherent vowel — just drop virāma and the अ
|
||||
"आ": "ा",
|
||||
"इ": "ि",
|
||||
"ई": "ी",
|
||||
"उ": "ु",
|
||||
"ऊ": "ू",
|
||||
"ऋ": "ृ",
|
||||
"ॠ": "ॄ",
|
||||
"ऌ": "ॢ",
|
||||
"ॡ": "ॣ",
|
||||
"ए": "े",
|
||||
"ऐ": "ै",
|
||||
"ओ": "ो",
|
||||
"औ": "ौ",
|
||||
}
|
||||
|
||||
def _virama_vowel_replace(m):
|
||||
vowel = m.group(1)
|
||||
matra = _VOWEL_TO_MATRA.get(vowel, vowel)
|
||||
# Remove the virāma (्) and replace independent vowel with mātrā
|
||||
return matra
|
||||
|
||||
text = re.sub(r"्[ \t]+([अ-औॠॡ])", _virama_vowel_replace, text)
|
||||
return text
|
||||
|
||||
|
||||
def iast_to_devanagari(iast_text: str) -> str:
|
||||
|
||||
Reference in New Issue
Block a user