Skip to content

Commit 00f55ff

Browse files
authored
Add TypeError to digitconv.py (#1127)
* Add TypeError to digitconv.py Add TypeError for text in digitconv.py * Update digitconv.py * Update test_util.py * Update test_util.py * Update test_util.py * Add more TypeError * Update check type * Update type * Update type
1 parent 48d803b commit 00f55ff

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

pythainlp/util/digitconv.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def thai_digit_to_arabic_digit(text: str) -> str:
8484
# output: เป็นจำนวน 123,400.25 บาท
8585
"""
8686
if not text or not isinstance(text, str):
87-
return ""
87+
raise TypeError("The text must be str type.")
8888

8989
return text.translate(_thai_arabic_translate_table)
9090

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

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

127127
# Convert Thai numerals to Arabic ones
128128
text = text.translate(_thai_arabic_translate_table)
@@ -161,7 +161,9 @@ def text_to_arabic_digit(text: str) -> str:
161161
text_to_arabic_digit("เก้าร้อย") == ""
162162
# output: True
163163
"""
164-
if not text or text not in _spell_digit:
164+
if not isinstance(text, str):
165+
raise TypeError("The text must be str type.")
166+
elif not text or text not in _spell_digit:
165167
return ""
166168

167169
return _spell_digit[text]
@@ -197,4 +199,9 @@ def text_to_thai_digit(text: str) -> str:
197199
text_to_thai_digit("เก้าร้อย") == ""
198200
# output: True
199201
"""
202+
if not isinstance(text, str):
203+
raise TypeError("The text must be str type.")
204+
elif not text:
205+
return ""
206+
200207
return arabic_digit_to_thai_digit(text_to_arabic_digit(text))

tests/core/test_util.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -164,26 +164,34 @@ def test_number(self):
164164
self.assertEqual(
165165
arabic_digit_to_thai_digit("ไทยแลนด์ 4.0"), "ไทยแลนด์ ๔.๐"
166166
)
167-
self.assertEqual(arabic_digit_to_thai_digit(""), "")
168-
self.assertEqual(arabic_digit_to_thai_digit(None), "")
167+
with self.assertRaises(TypeError):
168+
arabic_digit_to_thai_digit("")
169+
with self.assertRaises(TypeError):
170+
arabic_digit_to_thai_digit(None)
169171

170172
self.assertEqual(
171173
thai_digit_to_arabic_digit("๔๐๔ Not Found"), "404 Not Found"
172174
)
173-
self.assertEqual(thai_digit_to_arabic_digit(""), "")
174-
self.assertEqual(thai_digit_to_arabic_digit(None), "")
175+
with self.assertRaises(TypeError):
176+
thai_digit_to_arabic_digit("")
177+
with self.assertRaises(TypeError):
178+
thai_digit_to_arabic_digit(None)
175179

176180
self.assertEqual(digit_to_text("RFC 7258"), "RFC เจ็ดสองห้าแปด")
177-
self.assertEqual(digit_to_text(""), "")
178-
self.assertEqual(digit_to_text(None), "")
181+
with self.assertRaises(TypeError):
182+
digit_to_text("")
183+
with self.assertRaises(TypeError):
184+
digit_to_text(None)
179185

180186
self.assertEqual(text_to_arabic_digit("เจ็ด"), "7")
181187
self.assertEqual(text_to_arabic_digit(""), "")
182-
self.assertEqual(text_to_arabic_digit(None), "")
188+
with self.assertRaises(TypeError):
189+
text_to_arabic_digit(None)
183190

184191
self.assertEqual(text_to_thai_digit("เก้า"), "๙")
185192
self.assertEqual(text_to_thai_digit(""), "")
186-
self.assertEqual(text_to_thai_digit(None), "")
193+
with self.assertRaises(TypeError):
194+
text_to_thai_digit(None)
187195

188196
# ### pythainlp.util.keyboard
189197

0 commit comments

Comments
 (0)