From aca2200b7d047db4d05de7e1fcbe7e253ca6b94a Mon Sep 17 00:00:00 2001 From: Wannaphong Phatthiyaphaibun Date: Fri, 11 Jul 2025 12:49:32 +0700 Subject: [PATCH 1/9] Add TypeError to digitconv.py Add TypeError for text in digitconv.py --- pythainlp/util/digitconv.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pythainlp/util/digitconv.py b/pythainlp/util/digitconv.py index 76bee6091..799681c53 100644 --- a/pythainlp/util/digitconv.py +++ b/pythainlp/util/digitconv.py @@ -37,7 +37,7 @@ "1": "หนึ่ง", "2": "สอง", "3": "สาม", - "4": "สี่", + "4": "สี่",TypeError "5": "ห้า", "6": "หก", "7": "เจ็ด", @@ -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) @@ -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) @@ -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) From d39f9147c78e0ad271811d666a35b1e3e4dc1947 Mon Sep 17 00:00:00 2001 From: Wannaphong Phatthiyaphaibun Date: Wed, 13 Aug 2025 13:17:29 +0700 Subject: [PATCH 2/9] Update digitconv.py --- pythainlp/util/digitconv.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pythainlp/util/digitconv.py b/pythainlp/util/digitconv.py index 799681c53..2f316e86a 100644 --- a/pythainlp/util/digitconv.py +++ b/pythainlp/util/digitconv.py @@ -37,7 +37,7 @@ "1": "หนึ่ง", "2": "สอง", "3": "สาม", - "4": "สี่",TypeError + "4": "สี่", "5": "ห้า", "6": "หก", "7": "เจ็ด", From 23da55aedcfc0d7fdec5e86a1651ee2d97d7877c Mon Sep 17 00:00:00 2001 From: Wannaphong Phatthiyaphaibun Date: Thu, 14 Aug 2025 02:32:04 +0700 Subject: [PATCH 3/9] Update test_util.py --- tests/core/test_util.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/tests/core/test_util.py b/tests/core/test_util.py index 73fcce8b9..32e8dd2c8 100644 --- a/tests/core/test_util.py +++ b/tests/core/test_util.py @@ -164,14 +164,18 @@ 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(""), "") From 14445abced0262f803e3d4caa3147627f41e962c Mon Sep 17 00:00:00 2001 From: Wannaphong Phatthiyaphaibun Date: Thu, 14 Aug 2025 02:38:06 +0700 Subject: [PATCH 4/9] Update test_util.py --- tests/core/test_util.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/core/test_util.py b/tests/core/test_util.py index 32e8dd2c8..70dec5ce1 100644 --- a/tests/core/test_util.py +++ b/tests/core/test_util.py @@ -178,8 +178,10 @@ def test_number(self): 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(ValueError): + digit_to_text("") + with self.assertRaises(ValueError): + digit_to_text(None) self.assertEqual(text_to_arabic_digit("เจ็ด"), "7") self.assertEqual(text_to_arabic_digit(""), "") From d4f8e46c844cf9e2c89444d235187121d5e14cd1 Mon Sep 17 00:00:00 2001 From: Wannaphong Phatthiyaphaibun Date: Thu, 14 Aug 2025 02:42:00 +0700 Subject: [PATCH 5/9] Update test_util.py --- tests/core/test_util.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/core/test_util.py b/tests/core/test_util.py index 70dec5ce1..35ca44e5d 100644 --- a/tests/core/test_util.py +++ b/tests/core/test_util.py @@ -178,9 +178,9 @@ def test_number(self): thai_digit_to_arabic_digit(None) self.assertEqual(digit_to_text("RFC 7258"), "RFC เจ็ดสองห้าแปด") - with self.assertRaises(ValueError): + with self.assertRaises(TypeError): digit_to_text("") - with self.assertRaises(ValueError): + with self.assertRaises(TypeError): digit_to_text(None) self.assertEqual(text_to_arabic_digit("เจ็ด"), "7") From da9807ff0b2d3956a6193ce4eaa1a130a2383eca Mon Sep 17 00:00:00 2001 From: Wannaphong Date: Sun, 17 Aug 2025 17:31:46 +0700 Subject: [PATCH 6/9] Add more TypeError --- pythainlp/util/digitconv.py | 7 +++++-- tests/core/test_util.py | 12 ++++++++---- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/pythainlp/util/digitconv.py b/pythainlp/util/digitconv.py index 2f316e86a..32419ab59 100644 --- a/pythainlp/util/digitconv.py +++ b/pythainlp/util/digitconv.py @@ -161,8 +161,8 @@ def text_to_arabic_digit(text: str) -> str: text_to_arabic_digit("เก้าร้อย") == "" # output: True """ - if not text or text not in _spell_digit: - return "" + if not text or not isinstance(text, str): + return TypeError("The text must be str type.") return _spell_digit[text] @@ -197,4 +197,7 @@ def text_to_thai_digit(text: str) -> str: text_to_thai_digit("เก้าร้อย") == "" # output: True """ + if not text or not isinstance(text, str): + return TypeError("The text must be str type.") + return arabic_digit_to_thai_digit(text_to_arabic_digit(text)) diff --git a/tests/core/test_util.py b/tests/core/test_util.py index 35ca44e5d..fe76381a3 100644 --- a/tests/core/test_util.py +++ b/tests/core/test_util.py @@ -184,12 +184,16 @@ def test_number(self): 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("") + 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("") + with self.assertRaises(TypeError): + text_to_thai_digit(None) # ### pythainlp.util.keyboard From 569cc838f242cf5eb652abef67da27f139759a00 Mon Sep 17 00:00:00 2001 From: Wannaphong Date: Sun, 17 Aug 2025 17:40:16 +0700 Subject: [PATCH 7/9] Update check type --- pythainlp/util/digitconv.py | 4 +++- tests/core/test_util.py | 6 ++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/pythainlp/util/digitconv.py b/pythainlp/util/digitconv.py index 32419ab59..8bff6c8fe 100644 --- a/pythainlp/util/digitconv.py +++ b/pythainlp/util/digitconv.py @@ -161,8 +161,10 @@ def text_to_arabic_digit(text: str) -> str: text_to_arabic_digit("เก้าร้อย") == "" # output: True """ - if not text or not isinstance(text, str): + if not isinstance(text, str): return TypeError("The text must be str type.") + elif not text or text not in _spell_digit: + return "" return _spell_digit[text] diff --git a/tests/core/test_util.py b/tests/core/test_util.py index fe76381a3..9c46ad2aa 100644 --- a/tests/core/test_util.py +++ b/tests/core/test_util.py @@ -184,14 +184,12 @@ def test_number(self): digit_to_text(None) self.assertEqual(text_to_arabic_digit("เจ็ด"), "7") - with self.assertRaises(TypeError): - text_to_arabic_digit("") + self.assertEqual(text_to_arabic_digit(""), "") with self.assertRaises(TypeError): text_to_arabic_digit(None) self.assertEqual(text_to_thai_digit("เก้า"), "๙") - with self.assertRaises(TypeError): - text_to_thai_digit("") + self.assertEqual(text_to_thai_digit(""), "") with self.assertRaises(TypeError): text_to_thai_digit(None) From 9fba995762fd4a5b0dcb7cfee5306f15e33928e7 Mon Sep 17 00:00:00 2001 From: Wannaphong Date: Sun, 17 Aug 2025 18:00:40 +0700 Subject: [PATCH 8/9] Update type --- pythainlp/util/digitconv.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pythainlp/util/digitconv.py b/pythainlp/util/digitconv.py index 8bff6c8fe..ee7bccb96 100644 --- a/pythainlp/util/digitconv.py +++ b/pythainlp/util/digitconv.py @@ -162,7 +162,7 @@ def text_to_arabic_digit(text: str) -> str: # output: True """ if not isinstance(text, str): - return TypeError("The text must be str type.") + raise TypeError("The text must be str type.") elif not text or text not in _spell_digit: return "" @@ -200,6 +200,6 @@ def text_to_thai_digit(text: str) -> str: # output: True """ if not text or not isinstance(text, str): - return TypeError("The text must be str type.") + raise TypeError("The text must be str type.") return arabic_digit_to_thai_digit(text_to_arabic_digit(text)) From 72f75503b54a5bddccf76385e00121cd76e5e717 Mon Sep 17 00:00:00 2001 From: Wannaphong Date: Sun, 17 Aug 2025 18:26:29 +0700 Subject: [PATCH 9/9] Update type --- pythainlp/util/digitconv.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pythainlp/util/digitconv.py b/pythainlp/util/digitconv.py index ee7bccb96..c7caecc77 100644 --- a/pythainlp/util/digitconv.py +++ b/pythainlp/util/digitconv.py @@ -199,7 +199,9 @@ def text_to_thai_digit(text: str) -> str: text_to_thai_digit("เก้าร้อย") == "" # output: True """ - if not text or not isinstance(text, str): + 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))