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
15 changes: 11 additions & 4 deletions pythainlp/util/digitconv.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def thai_digit_to_arabic_digit(text: str) -> str:
# output: เป็นจำนวน 123,400.25 บาท
"""
if not text or not isinstance(text, str):
return ""
raise TypeError("The text must be str type.")

return text.translate(_thai_arabic_translate_table)

Expand All @@ -110,7 +110,7 @@ def arabic_digit_to_thai_digit(text: str) -> str:
# output: เป็นจำนวน ๑๒๓,๔๐๐.๒๕ บาท
"""
if not text or not isinstance(text, str):
return ""
raise TypeError("The text must be str type.")

# Convert Arabic to Thai numerals
return text.translate(_arabic_thai_translate_table)
Expand All @@ -122,7 +122,7 @@ def digit_to_text(text: str) -> str:
:return: Text with digits spelled out in Thai
"""
if not text or not isinstance(text, str):
return ""
raise TypeError("The text must be str type.")

# Convert Thai numerals to Arabic ones
text = text.translate(_thai_arabic_translate_table)
Expand Down Expand Up @@ -161,7 +161,9 @@ def text_to_arabic_digit(text: str) -> str:
text_to_arabic_digit("เก้าร้อย") == ""
# output: True
"""
if not text or text not in _spell_digit:
if not isinstance(text, str):
raise TypeError("The text must be str type.")
elif not text or text not in _spell_digit:
return ""

return _spell_digit[text]
Expand Down Expand Up @@ -197,4 +199,9 @@ def text_to_thai_digit(text: str) -> str:
text_to_thai_digit("เก้าร้อย") == ""
# output: True
"""
if not isinstance(text, str):
raise TypeError("The text must be str type.")
elif not text:
return ""

return arabic_digit_to_thai_digit(text_to_arabic_digit(text))
24 changes: 16 additions & 8 deletions tests/core/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,26 +164,34 @@ def test_number(self):
self.assertEqual(
arabic_digit_to_thai_digit("ไทยแลนด์ 4.0"), "ไทยแลนด์ ๔.๐"
)
self.assertEqual(arabic_digit_to_thai_digit(""), "")
self.assertEqual(arabic_digit_to_thai_digit(None), "")
with self.assertRaises(TypeError):
arabic_digit_to_thai_digit("")
with self.assertRaises(TypeError):
arabic_digit_to_thai_digit(None)

self.assertEqual(
thai_digit_to_arabic_digit("๔๐๔ Not Found"), "404 Not Found"
)
self.assertEqual(thai_digit_to_arabic_digit(""), "")
self.assertEqual(thai_digit_to_arabic_digit(None), "")
with self.assertRaises(TypeError):
thai_digit_to_arabic_digit("")
with self.assertRaises(TypeError):
thai_digit_to_arabic_digit(None)

self.assertEqual(digit_to_text("RFC 7258"), "RFC เจ็ดสองห้าแปด")
self.assertEqual(digit_to_text(""), "")
self.assertEqual(digit_to_text(None), "")
with self.assertRaises(TypeError):
digit_to_text("")
with self.assertRaises(TypeError):
digit_to_text(None)

self.assertEqual(text_to_arabic_digit("เจ็ด"), "7")
self.assertEqual(text_to_arabic_digit(""), "")
self.assertEqual(text_to_arabic_digit(None), "")
with self.assertRaises(TypeError):
text_to_arabic_digit(None)

self.assertEqual(text_to_thai_digit("เก้า"), "๙")
self.assertEqual(text_to_thai_digit(""), "")
self.assertEqual(text_to_thai_digit(None), "")
with self.assertRaises(TypeError):
text_to_thai_digit(None)

# ### pythainlp.util.keyboard

Expand Down
Loading