Skip to content

Commit a57c13e

Browse files
committed
More test cases
1 parent 9be9b21 commit a57c13e

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

pythainlp/util/__init__.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
"arabic_digit_to_thai_digit",
88
"bahttext",
99
"collate",
10+
"countthai",
1011
"delete_tone",
12+
"deletetone", # Will deprecated after version 2.1
1113
"digit_to_text",
1214
"eng_to_thai",
1315
"find_keyword",
14-
"countthai",
1516
"is_native_thai",
1617
"isthai",
1718
"isthaichar",
@@ -22,11 +23,12 @@
2223
"reign_year_to_ad",
2324
"text_to_arabic_digit",
2425
"text_to_thai_digit",
26+
"thai_digit_to_arabic_digit",
2527
"thai_strftime",
28+
"thai_time",
2629
"thai_to_eng",
27-
"thai_digit_to_arabic_digit",
30+
"thaicheck", # Will deprecated after version 2.1
2831
"thaiword_to_num",
29-
"thai_time",
3032
]
3133

3234

@@ -41,9 +43,9 @@
4143
)
4244
from .keyboard import eng_to_thai, thai_to_eng
4345
from .keywords import find_keyword, rank
44-
from .normalize import delete_tone, normalize
46+
from .normalize import delete_tone, deletetone, normalize
4547
from .numtoword import bahttext, num_to_thaiword
4648
from .thai import countthai, isthai, isthaichar
47-
from .thaiwordcheck import is_native_thai
48-
from .wordtonum import thaiword_to_num
4949
from .thai_time import thai_time
50+
from .thaiwordcheck import is_native_thai, thaicheck
51+
from .wordtonum import thaiword_to_num

pythainlp/util/thaiwordcheck.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def is_native_thai(word: str) -> bool:
111111
chs = re.findall(_TH_CONSONANTS_PATTERN, word)
112112
if not chs:
113113
return False
114-
114+
115115
# If there's only one Thai consonant -> it can be a native Thai
116116
if len(chs) == 1:
117117
return True

tests/test_util.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
import unittest
55
from collections import Counter
66

7+
from pythainlp.tokenize import word_tokenize
78
from pythainlp.util import (
89
arabic_digit_to_thai_digit,
910
bahttext,
1011
collate,
1112
countthai,
1213
delete_tone,
14+
deletetone,
1315
digit_to_text,
1416
eng_to_thai,
1517
find_keyword,
@@ -25,11 +27,11 @@
2527
text_to_thai_digit,
2628
thai_digit_to_arabic_digit,
2729
thai_strftime,
30+
thai_time,
2831
thai_to_eng,
32+
thaicheck,
2933
thaiword_to_num,
30-
thai_time,
3134
)
32-
from pythainlp.tokenize import word_tokenize
3335

3436

3537
class TestUtilPackage(unittest.TestCase):
@@ -144,6 +146,12 @@ def test_date(self):
144146

145147
def test_thai_strftime(self):
146148
date = datetime.datetime(1976, 10, 6, 1, 40)
149+
self.assertEqual(thai_strftime(date, "%d"), "06")
150+
self.assertEqual(thai_strftime(date, "%-d"), "6")
151+
self.assertEqual(thai_strftime(date, "%-d", True), "๖")
152+
self.assertEqual(thai_strftime(date, "%%"), "%")
153+
self.assertEqual(thai_strftime(date, "%-"), "-")
154+
self.assertEqual(thai_strftime(date, "%P"), "P")
147155
self.assertEqual(thai_strftime(date, "%c"), "พ 6 ต.ค. 01:40:00 2519")
148156
self.assertEqual(
149157
thai_strftime(date, "%c", True), "พ ๖ ต.ค. ๐๑:๔๐:๐๐ ๒๕๑๙"
@@ -155,7 +163,7 @@ def test_thai_strftime(self):
155163
"วันพุธที่ 06 ตุลาคม พ.ศ. 2519 เวลา 01:40น. (พ 06-ต.ค.-19) % %",
156164
)
157165
self.assertIsNotNone(
158-
thai_strftime(date, "%A%a%B%b%C%c%D%F%G%g%v%X%x%Y%y%+")
166+
thai_strftime(date, "%A%a%B%b%C%c%D%F%G%g%v%X%x%Y%y%+%%")
159167
)
160168

161169
# ### pythainlp.util.thai_time
@@ -169,6 +177,10 @@ def test_thai_time(self):
169177
self.assertEqual(
170178
thai_time(datetime.time(12, 3, 0)), "สิบสองนาฬิกาสามนาที"
171179
)
180+
self.assertEqual(
181+
thai_time(datetime.time(12, 3, 1)),
182+
"สิบสองนาฬิกาสามนาทีหนึ่งวินาที",
183+
)
172184
self.assertEqual(
173185
thai_time(
174186
datetime.datetime(2014, 5, 22, 12, 3, 0), precision="second"
@@ -208,6 +220,7 @@ def test_thai_time(self):
208220
def test_delete_tone(self):
209221
self.assertEqual(delete_tone("จิ้น"), "จิน")
210222
self.assertEqual(delete_tone("เก๋า"), "เกา")
223+
self.assertEqual(delete_tone("จิ้น"), deletetone("จิ้น"))
211224

212225
def test_normalize(self):
213226
self.assertEqual(normalize("เเปลก"), "แปลก")
@@ -235,6 +248,7 @@ def test_isthai(self):
235248
self.assertEqual(isthai("(ต.ค.)", ignore_chars=".()"), True)
236249

237250
def test_is_native_thai(self):
251+
self.assertEqual(is_native_thai("เลข"), thaicheck("เลข"))
238252
self.assertEqual(is_native_thai(None), False)
239253
self.assertEqual(is_native_thai(""), False)
240254
self.assertEqual(is_native_thai("116"), False)

0 commit comments

Comments
 (0)