Files
jarvis-models/tts/vits/text/sanskrit.py
2024-03-19 17:33:09 +08:00

63 lines
1.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import re
from indic_transliteration import sanscript
# List of (iast, ipa) pairs:
_iast_to_ipa = [(re.compile('%s' % x[0]), x[1]) for x in [
('a', 'ə'),
('ā', 'aː'),
('ī', 'iː'),
('ū', 'uː'),
('', 'ɹ`'),
('', 'ɹ`ː'),
('', 'l`'),
('', 'l`ː'),
('e', 'eː'),
('o', 'oː'),
('k', 'k⁼'),
('k⁼h', ''),
('g', 'g⁼'),
('g⁼h', ''),
('', 'ŋ'),
('c', 'ʧ⁼'),
('ʧ⁼h', 'ʧʰ'),
('j', 'ʥ⁼'),
('ʥ⁼h', 'ʥʰ'),
('ñ', 'n^'),
('', 't`⁼'),
('t`⁼h', 't`ʰ'),
('', 'd`⁼'),
('d`⁼h', 'd`ʰ'),
('', 'n`'),
('t', 't⁼'),
('t⁼h', ''),
('d', 'd⁼'),
('d⁼h', ''),
('p', 'p⁼'),
('p⁼h', ''),
('b', 'b⁼'),
('b⁼h', ''),
('y', 'j'),
('ś', 'ʃ'),
('', 's`'),
('r', 'ɾ'),
('', 'l`'),
('h', 'ɦ'),
("'", ''),
('~', '^'),
('', '^')
]]
def devanagari_to_ipa(text):
text = text.replace('', 'ओम्')
text = re.sub(r'\s*।\s*$', '.', text)
text = re.sub(r'\s*।\s*', ', ', text)
text = re.sub(r'\s*॥', '.', text)
text = sanscript.transliterate(text, sanscript.DEVANAGARI, sanscript.IAST)
for regex, replacement in _iast_to_ipa:
text = re.sub(regex, replacement, text)
text = re.sub('(.)[`ː]*ḥ', lambda x: x.group(0)
[:-1]+'h'+x.group(1)+'*', text)
return text