Skip to content
Merged
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
13 changes: 8 additions & 5 deletions pythainlp/transliterate/spoonerism.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ def puan(word: str, show_pronunciation: bool = True) -> str:
Thai Spoonerism

This function converts Thai word to spoonerism word.
It only supports words with 2 to 3 syllables.

:param str word: Thai word to be spoonerized
:param bool show_pronunciation: True (default) or False
Expand Down Expand Up @@ -64,10 +63,14 @@ def puan(word: str, show_pronunciation: bool = True) -> str:
list_w_char[1][1], list_w_char[2][1], 1)
)
else: # > 3 syllables
raise ValueError(
"""{0} is more than 3 syllables.\n
It only supports words with 2 to 3 syllables.""".format(word)
_list_w.append(
_list_pron[0].replace(list_w_char[0][1], list_w_char[-1][1], 1)
)
for i in range(1, len(list_w_char)-1):
_list_w.append(_list_pron[i])
_list_w.append(_list_pron[-1].replace(
list_w_char[-1][1], list_w_char[0][1], 1)
)
if not show_pronunciation:
_list_w = [i.replace("หฺ", "") for i in _list_w]
_list_w = [i.replace("หฺ", "").replace('ฺ', '') for i in _list_w]
return _mix_list.join(_list_w)
10 changes: 6 additions & 4 deletions tests/test_transliterate.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,11 @@ def test_pronunciate(self):
self.assertIsNotNone(pronunciate("jks", engine="w2p"))

def test_puan(self):
self.assertEqual(puan("แมว"), "แมว")
self.assertEqual(puan("นาริน"), "นิน-รา")
self.assertEqual(puan("นาริน", False), "นินรา")
self.assertEqual(puan("นาริน", show_pronunciation=False), "นินรา")
self.assertEqual(puan("แสงดีนะ"), "แสง-ดะ-นี")
self.assertEqual(puan("แสงดีนะ", False), "แสงดะนี")
with self.assertRaises(ValueError):
self.assertEqual(puan("สวัสดีครับ"), "สวัสดีครับ")
self.assertEqual(puan("แสงดีนะ", show_pronunciation=False), "แสงดะนี")
self.assertEqual(
puan("การทำความดี", show_pronunciation=False), "ดานทำความกี"
)