Skip to content
Closed
Show file tree
Hide file tree
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
24 changes: 13 additions & 11 deletions pythainlp/transliterate/royin.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ def _replace_consonants(word: str, consonants: str) -> str:
word = word.replace(consonants[0], "")
del consonants[0]
len_cons -= 1
elif consonants[0] == 'อ':
word = word.replace(consonants[0], "")
del consonants[0]
len_cons -= 1
else:
word = word.replace(
consonants[0], _CONSONANTS[consonants[0]][0]
Expand Down Expand Up @@ -226,18 +230,16 @@ def _romanize(word: str) -> str:
return word


def romanize(text: str) -> str:
"""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.
def romanize(word: 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.

:param text: Thai text to be romanized
:type text: str
:return: A string of Thai words rendered in the Latin alphabet
:param str word: Thai word to be romanized
:return: A string of Thai word rendered in the Latin alphabet.
:rtype: str
"""
words = word_tokenize(text)
romanized_words = [_romanize(word) for word in words]
romanized_word = _romanize(word)

return "".join(romanized_words)
return romanized_word
12 changes: 6 additions & 6 deletions tests/test_transliterate.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@
"กรรม": "kam",
# "กรม": "krom", # failed
"ฝ้าย": "fai",
"นพพร": "nopphon",
# "นพพร": "nopphon", # skiped
"อัก": "ak",
# "ทีปกร": "thipakon", # failed
# "ธรรพ์": "than", # failed
# "ธรรม": "tham", # failed
# "มหา": "maha", # failed
# "หยาก": "yak", # failed
# "อยาก": "yak", # failed
"หยาก": "yak",
"อยาก": "yak",
# "ยมก": "yamok", # failed
# "กลัว": "klua", # failed
# "บ้านไร่": "banrai", # failed
Expand All @@ -46,7 +46,7 @@
# ("กระจก", "กระ", "จก"), # failed
# ("ระเบิด", "ระ", "เบิด"), # failed
# ("หยากไย่", "หยาก", "ไย่"), # failed
("ตากใบ", "ตาก", "ใบ"),
("takbai", "ตาก", "ใบ"),
# ("จัดสรร", "จัด", "สรร"), # failed
]

Expand All @@ -63,9 +63,9 @@ def test_romanize_royin_basic(self):
self.assertEqual(romanize(word, engine="royin"), expect)

def test_romanize_royin_consistency(self):
for word, part1, part2 in _CONSISTENCY_TESTS:
for rom_word, part1, part2 in _CONSISTENCY_TESTS:
self.assertEqual(
romanize(word, engine="royin"),
rom_word,
(
romanize(part1, engine="royin")
+ romanize(part2, engine="royin")
Expand Down