Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 20 additions & 27 deletions pythainlp/transliterate/royin.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,11 @@ def _replace_consonants(word: str, consonants: str) -> str:

if len(consonants) == 1:
return word.replace(consonants[0], _CONSONANTS[consonants[0]][0])
len_cons = len(consonants)

i = 0
len_cons = len(consonants)
while i < len_cons:
len_word = len(word)
if i == 0:
if consonants[0] == _HO_HIP:
word = word.replace(consonants[0], "")
Expand All @@ -166,16 +167,18 @@ def _replace_consonants(word: str, consonants: str) -> str:
consonants[0], _CONSONANTS[consonants[0]][0]
)
i += 1
elif consonants[i] == _RO_RUA and i == len(word) and word[i - 1] == _RO_RUA:
word = word.replace(
consonants[i], _CONSONANTS[consonants[i]][1]
)
elif consonants[i] == _RO_RUA and i < len(word):
if i + 1 == len(word) and word[i] == _RO_RUA:
elif (
i == len_word
and consonants[i] == _RO_RUA
and word[i - 1] == _RO_RUA
):
word = word.replace(consonants[i], _CONSONANTS[consonants[i]][1])
elif i < len_word and consonants[i] == _RO_RUA:
if i + 1 == len_word and word[i] == _RO_RUA:
word = word.replace(
consonants[i], _CONSONANTS[consonants[i]][1]
)
elif word[i] == _RO_RUA and i + 1 < len(word):
elif i + 1 < len_word and word[i] == _RO_RUA:
if word[i + 1] == _RO_RUA:
word = list(word)
del word[i + 1]
Expand All @@ -192,19 +195,12 @@ def _replace_consonants(word: str, consonants: str) -> str:
i += 1
else:
word = word.replace(
consonants[i],
_CONSONANTS[consonants[i]][1]
consonants[i], _CONSONANTS[consonants[i]][1]
)
i += 1
elif word[i] == _RO_RUA:
word = word.replace(
consonants[i], _CONSONANTS[consonants[i]][1]
)
i += 1
else:
word = word.replace(
consonants[i],
_CONSONANTS[consonants[i]][1]
consonants[i], _CONSONANTS[consonants[i]][1]
)
i += 1
else:
Expand All @@ -216,10 +212,6 @@ def _replace_consonants(word: str, consonants: str) -> str:

# support function for romanize()
def _romanize(word: str) -> str:
"""
:param str word: a Thai word, should have already been tokenized.
:return: Spells out how the Thai word should be pronounced.
"""
word = _replace_vowels(_normalize(word))
consonants = _RE_CONSONANT.findall(word)

Expand All @@ -235,13 +227,14 @@ def _romanize(word: str) -> str:


def romanize(text: str) -> str:
"""
Rendering Thai words in the Latin alphabet or "romanization",
using the Royal Thai General System of Transcription (RTGS),
which is the official system published by the Royal Institute of Thailand.
"""Render Thai words in Latin alphabet, using RTGS

Royal Thai General System of Transcription (RTGS),
is the official system by the Royal Institute of Thailand.

:param str text: Thai text to be romanized
:return: A string of Thai words rendered in the Latin alphabet.
:param text: Thai text to be romanized
:type text: str
:return: A string of Thai words rendered in the Latin alphabet
:rtype: str
"""
words = word_tokenize(text)
Expand Down