Upload pronunciation.py with huggingface_hub
Browse files- pronunciation.py +87 -0
pronunciation.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
pronunciation.py
|
| 3 |
+
----------------
|
| 4 |
+
Phonetic lexicon and pronunciation normalization for difficult Vedic, Sanskrit,
|
| 5 |
+
and historical terminology when synthesizing speech via Kokoro-ONNX.
|
| 6 |
+
|
| 7 |
+
Ensures proper pronunciation without confusing espeak-ng/phonemizer in English mode.
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
import re
|
| 11 |
+
from typing import Dict, Optional
|
| 12 |
+
|
| 13 |
+
# Core Vedic & Sanskrit phonetic replacement lexicon for Kokoro (en-gb/en-us)
|
| 14 |
+
VEDIC_PRONUNCIATION_MAP: Dict[str, str] = {
|
| 15 |
+
# Diacritic and IAST variations
|
| 16 |
+
r"\bṚṣis?\b": "Rishis",
|
| 17 |
+
r"\bRsis\b": "Rishis",
|
| 18 |
+
r"\bRsi\b": "Rishi",
|
| 19 |
+
r"\bṚta\b": "Ree-tuh",
|
| 20 |
+
r"\bRta\b": "Ree-tuh",
|
| 21 |
+
r"\bEkam Sat\b": "Ay-kum Sut",
|
| 22 |
+
r"\bTad Ekam\b": "Tud Ay-kum",
|
| 23 |
+
r"\bEkam\b": "Ay-kum",
|
| 24 |
+
r"\bNasadiya Sukta\b": "Nah-suh-dee-yuh Sook-tuh",
|
| 25 |
+
r"\bNāsadīya Sūkta\b": "Nah-suh-dee-yuh Sook-tuh",
|
| 26 |
+
r"\bVisvakarman\b": "Vish-vuh-kur-mun",
|
| 27 |
+
r"\bViśvakarman\b": "Vish-vuh-kur-mun",
|
| 28 |
+
r"\bPrajapati\b": "Pruh-jah-puh-tee",
|
| 29 |
+
r"\bPrajāpati\b": "Pruh-jah-puh-tee",
|
| 30 |
+
r"\bAgni\b": "Ugg-nee",
|
| 31 |
+
r"\bVaruna\b": "Vuh-roo-nuh",
|
| 32 |
+
r"\bVaruṇa\b": "Vuh-roo-nuh",
|
| 33 |
+
r"\bVayu\b": "Vah-yoo",
|
| 34 |
+
r"\bVāyu\b": "Vah-yoo",
|
| 35 |
+
r"\bVisnu\b": "Vishnu",
|
| 36 |
+
r"\bViṣṇu\b": "Vishnu",
|
| 37 |
+
r"\bBrihad-Aranyaka\b": "Bree-hud Ah-run-yuh-kuh",
|
| 38 |
+
r"\bBṛhadāraṇyaka\b": "Bree-hud Ah-run-yuh-kuh",
|
| 39 |
+
r"\bUpanishads\b": "Oo-pun-ee-shuds",
|
| 40 |
+
r"\bUpanishad\b": "Oo-pun-ee-shud",
|
| 41 |
+
r"\bUpaniṣads?\b": "Oo-pun-ee-shud",
|
| 42 |
+
r"\bYajnavalkya\b": "Yaj-nuh-val-kya",
|
| 43 |
+
r"\bYājñavalkya\b": "Yaj-nuh-val-kya",
|
| 44 |
+
r"\bmahiman\b": "muh-hee-mun",
|
| 45 |
+
r"\bBrahman\b": "Brah-mun",
|
| 46 |
+
r"\bBrahma\b": "Brah-muh",
|
| 47 |
+
r"\bAtman\b": "Aht-mun",
|
| 48 |
+
r"\bĀtman\b": "Aht-mun",
|
| 49 |
+
r"\bChandogya\b": "Chahn-dohg-yuh",
|
| 50 |
+
r"\bChāndogya\b": "Chahn-dohg-yuh",
|
| 51 |
+
r"\bKatha\b": "Kuh-tuh",
|
| 52 |
+
r"\bKaṭha\b": "Kuh-tuh",
|
| 53 |
+
r"\bMaitri\b": "My-tree",
|
| 54 |
+
r"\bMaitrī\b": "My-tree",
|
| 55 |
+
r"\bRig Veda\b": "Rig Vay-duh",
|
| 56 |
+
r"\bRigveda\b": "Rig Vay-duh",
|
| 57 |
+
r"\bṚgveda\b": "Rig Vay-duh",
|
| 58 |
+
r"\bVedas\b": "Vay-duhs",
|
| 59 |
+
r"\bVeda\b": "Vay-duh",
|
| 60 |
+
r"\bVedic\b": "Vay-dik",
|
| 61 |
+
r"\bSamsara\b": "Sum-sah-ruh",
|
| 62 |
+
r"\bSaṃsāra\b": "Sum-sah-ruh",
|
| 63 |
+
r"\bMoksha\b": "Mohk-shuh",
|
| 64 |
+
r"\bMokṣa\b": "Mohk-shuh",
|
| 65 |
+
r"\bMaya\b": "Mah-yah",
|
| 66 |
+
r"\bMāyā\b": "Mah-yah",
|
| 67 |
+
r"\bDharma\b": "Dhur-muh",
|
| 68 |
+
r"\bKarma\b": "Kur-muh",
|
| 69 |
+
r"\bSoma\b": "Soh-muh",
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
def apply_pronunciation_lexicon(
|
| 74 |
+
text: str, custom_map: Optional[Dict[str, str]] = None
|
| 75 |
+
) -> str:
|
| 76 |
+
"""
|
| 77 |
+
Replaces ancient and difficult terms with their phonetic equivalents for TTS.
|
| 78 |
+
"""
|
| 79 |
+
active_map = VEDIC_PRONUNCIATION_MAP.copy()
|
| 80 |
+
if custom_map:
|
| 81 |
+
active_map.update(custom_map)
|
| 82 |
+
|
| 83 |
+
result = text
|
| 84 |
+
for pattern, replacement in active_map.items():
|
| 85 |
+
result = re.sub(pattern, replacement, result, flags=re.IGNORECASE)
|
| 86 |
+
|
| 87 |
+
return result
|