File size: 897 Bytes
e4b9a7b | 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 | # -*- coding: utf-8 -*-
# SPDX-FileCopyrightText: 2016-2025 PyThaiNLP Project
# SPDX-FileType: SOURCE
# SPDX-License-Identifier: Apache-2.0
try:
from tltk.nlp import g2p, th2ipa, th2roman
except ImportError:
raise ImportError("Not found tltk! Please install tltk by pip install tltk")
def romanize(text: str) -> str:
"""
Transliterating thai text to the Latin alphabet using tltk.
:param str text: Thai text to be romanized
:return: A string of Thai words rendered in the Latin alphabet.
:rtype: str
"""
_temp = th2roman(text)
return _temp[: _temp.rfind(" <s/>")].replace("<s/>", "")
def tltk_g2p(text: str) -> str:
_temp = g2p(text).split("<tr/>")[1].replace("|<s/>", "").replace("|", " ")
return _temp.replace("<s/>", "")
def tltk_ipa(text: str) -> str:
_temp = th2ipa(text)
return _temp[: _temp.rfind(" <s/>")].replace("<s/>", "")
|