Skip to content

Commit d3fd763

Browse files
authored
Merge pull request #631 from PyThaiNLP/update-spoonerism
Spoonerism: Add supports words more 3 syllables
2 parents 9a89c82 + b8753f8 commit d3fd763

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

pythainlp/transliterate/spoonerism.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ def puan(word: str, show_pronunciation: bool = True) -> str:
1010
Thai Spoonerism
1111
1212
This function converts Thai word to spoonerism word.
13-
It only supports words with 2 to 3 syllables.
1413
1514
:param str word: Thai word to be spoonerized
1615
:param bool show_pronunciation: True (default) or False
@@ -64,10 +63,14 @@ def puan(word: str, show_pronunciation: bool = True) -> str:
6463
list_w_char[1][1], list_w_char[2][1], 1)
6564
)
6665
else: # > 3 syllables
67-
raise ValueError(
68-
"""{0} is more than 3 syllables.\n
69-
It only supports words with 2 to 3 syllables.""".format(word)
66+
_list_w.append(
67+
_list_pron[0].replace(list_w_char[0][1], list_w_char[-1][1], 1)
68+
)
69+
for i in range(1, len(list_w_char)-1):
70+
_list_w.append(_list_pron[i])
71+
_list_w.append(_list_pron[-1].replace(
72+
list_w_char[-1][1], list_w_char[0][1], 1)
7073
)
7174
if not show_pronunciation:
72-
_list_w = [i.replace("หฺ", "") for i in _list_w]
75+
_list_w = [i.replace("หฺ", "").replace('ฺ', '') for i in _list_w]
7376
return _mix_list.join(_list_w)

tests/test_transliterate.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,11 @@ def test_pronunciate(self):
154154
self.assertIsNotNone(pronunciate("jks", engine="w2p"))
155155

156156
def test_puan(self):
157+
self.assertEqual(puan("แมว"), "แมว")
157158
self.assertEqual(puan("นาริน"), "นิน-รา")
158-
self.assertEqual(puan("นาริน", False), "นินรา")
159+
self.assertEqual(puan("นาริน", show_pronunciation=False), "นินรา")
159160
self.assertEqual(puan("แสงดีนะ"), "แสง-ดะ-นี")
160-
self.assertEqual(puan("แสงดีนะ", False), "แสงดะนี")
161-
with self.assertRaises(ValueError):
162-
self.assertEqual(puan("สวัสดีครับ"), "สวัสดีครับ")
161+
self.assertEqual(puan("แสงดีนะ", show_pronunciation=False), "แสงดะนี")
162+
self.assertEqual(
163+
puan("การทำความดี", show_pronunciation=False), "ดานทำความกี"
164+
)

0 commit comments

Comments
 (0)