Skip to content

Commit c6edbf4

Browse files
committed
Soundex functions handle empty or None case
1 parent 7b18822 commit c6edbf4

File tree

4 files changed

+21
-1
lines changed

4 files changed

+21
-1
lines changed

pythainlp/soundex/lk82.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ def lk82(text):
2828
:param str text: Thai word
2929
:return: LK82 soundex
3030
"""
31+
if not text:
32+
return ""
33+
3134
res = []
3235
text = _RE_1.sub("", text) # 4.ลบวรรณยุกต์
3336
text = _RE_2.sub("", text) # 4.ลบตัวการันต์

pythainlp/soundex/metasound.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ def metasound(text, length=4):
3434
metasound("รักษ์") # 'ร100'
3535
metasound("บูรณการ", 5)) # 'บ5515'
3636
"""
37+
if not text:
38+
return ""
39+
3740
# keep only consonants and thanthakhat
3841
chars = []
3942
for ch in text:

pythainlp/soundex/udom83.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ def udom83(text):
3737
:return: Udom83 soundex
3838
"""
3939

40+
if not text:
41+
return ""
42+
4043
text = _RE_1.sub("ัน\\1", text)
4144
text = _RE_2.sub("ั\\1", text)
4245
text = _RE_3.sub("ัน\\1", text)

tests/__init__.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
from pythainlp.rank import rank
3535
from pythainlp.romanization import romanize
3636
from pythainlp.sentiment import sentiment
37-
from pythainlp.soundex import lk82, metasound, udom83
37+
from pythainlp.soundex import lk82, metasound, soundex, udom83
3838
from pythainlp.spell import correct, spell
3939
from pythainlp.summarize import summarize
4040
from pythainlp.tag import pos_tag, pos_tag_sents
@@ -233,13 +233,24 @@ def test_sentiment(self):
233233
# ### pythainlp.soundex
234234

235235
def test_soundex(self):
236+
self.assertIsNotNone(soundex("a", engine="lk82"))
237+
self.assertIsNotNone(soundex("a", engine="udom83"))
238+
self.assertIsNotNone(soundex("a", engine="metasound"))
239+
self.assertIsNotNone(soundex("a", engine="XXX"))
240+
236241
self.assertEqual(lk82("รถ"), "ร3000")
242+
self.assertIsNotNone(lk82("เกาะกูร์"))
243+
self.assertEqual(lk82(""), "")
244+
237245
self.assertEqual(udom83("รถ"), "ร800000")
246+
self.assertEqual(udom83(None), "")
247+
238248
self.assertEqual(metasound("บูรณะ"), "บ550")
239249
self.assertEqual(metasound("คน"), "ค500")
240250
self.assertEqual(metasound("คนA"), "ค500")
241251
self.assertEqual(metasound("ดา"), "ด000")
242252
self.assertEqual(metasound("รักษ์"), metasound("รัก"))
253+
self.assertEqual(metasound(""), "")
243254

244255
# ### pythainlp.spell
245256

0 commit comments

Comments
 (0)