Skip to content

Commit 5fa07a7

Browse files
authored
Merge pull request #1377 from phoneee/fix/empty-string-guards
fix: guard nighit, check_sara, check_marttra against empty input
2 parents 3a99c24 + be43a48 commit 5fa07a7

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

pythainlp/khavee/core.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,15 @@ def check_sara(self, word: str) -> str:
5353
sara = []
5454
countoa = 0
5555

56+
if not word:
57+
return ""
58+
5659
# In case of การันย์
5760
if "์" in word[-1]:
5861
word = word[:-2]
62+
# After removing the karun, the word may become empty (e.g. "ก์")
63+
if not word:
64+
return ""
5965

6066
# In case of สระเดี่ยว
6167
for i in word:
@@ -251,6 +257,9 @@ def check_marttra(self, word: str) -> str:
251257
word = self.handle_karun_sound_silence(word)
252258
word = remove_tonemark(word)
253259

260+
if not word:
261+
return ""
262+
254263
# Check for ำ at the end (represents "am" sound, ends with m)
255264
if word[-1] == "ำ":
256265
return "กม"

pythainlp/morpheme/word_formation.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,28 @@ def nighit(w1: str, w2: str) -> str:
3535
>>> nighit("สํ", "โยค")
3636
'สังโยค'
3737
"""
38+
if not isinstance(w1, str) or not isinstance(w2, str):
39+
raise TypeError("Both w1 and w2 must be strings.")
40+
w1 = w1.strip()
41+
w2 = w2.strip()
42+
if not w1:
43+
return w2
44+
if not w2:
45+
return w1
3846
if not str(w1).endswith("ํ") and len(w1) != 2:
3947
raise NotImplementedError(f"The function doesn't support {w1}.")
4048
list_w1 = list(w1)
4149
list_w2 = list(w2)
4250
newword = []
4351
newword.append(list_w1[0])
4452
newword.append("ั")
45-
consonant_start = [i for i in list_w2 if i in set(thai_consonants)][0]
53+
_consonants = set(thai_consonants)
54+
consonants_in_w2 = [i for i in list_w2 if i in _consonants]
55+
if not consonants_in_w2:
56+
raise ValueError(
57+
f"w2 {w2!r} contains no Thai consonants."
58+
)
59+
consonant_start = consonants_in_w2[0]
4660
if consonant_start in ["ก", "ช", "ค", "ข", "ง"]:
4761
newword.append("ง")
4862
elif consonant_start in ["จ", "ฉ", "ช", "ฌ"]:

0 commit comments

Comments
 (0)