Skip to content

Commit 1ba6204

Browse files
authored
Merge pull request #1406 from bact/test-pr-1377
Add ValueError tests for nighit (PR 1377)
2 parents 5fa07a7 + 6ca49ce commit 1ba6204

4 files changed

Lines changed: 29 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ and this project adheres to
1717
- Full release notes: <https://github.com/PyThaiNLP/pythainlp/releases>
1818
- Commit history: <https://github.com/PyThaiNLP/pythainlp/compare/v5.3.3...v5.3.4>
1919

20+
## [Unreleased]
21+
22+
## Changed
23+
24+
- Improve guardrails in `check_sara()` and `nighit()`
25+
2026
## [5.3.4] - 2026-04-02
2127

2228
### Fixed

pythainlp/khavee/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ def check_sara(self, word: str) -> str:
223223
sara.append("เอือ")
224224

225225
if not sara:
226-
return "Can't find Sara in this word"
226+
return ""
227227

228228
return sara[0]
229229

tests/core/test_khavee.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,3 +258,14 @@ def test_เอือ_sara(self):
258258

259259
def test_returns_string(self):
260260
self.assertIsInstance(self.kv.check_sara("เริง"), str)
261+
262+
def test_empty_string_returns_empty(self):
263+
self.assertEqual(self.kv.check_sara(""), "")
264+
265+
def test_empty_string_after_removing_karun_returns_empty(self):
266+
self.assertEqual(self.kv.check_sara("ก์"), "")
267+
268+
def test_empty_string_after_removing_tone_marks_returns_empty(self):
269+
self.assertEqual(
270+
self.kv.check_sara("\u0e48"), ""
271+
) # The string contains only Thai Mai Ek tone mark

tests/core/test_morpheme.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,26 @@
1010
class MorphemeTestCase(unittest.TestCase):
1111
def test_nighit(self):
1212
self.assertEqual(nighit("สํ", "คีต"), "สังคีต")
13+
self.assertEqual(
14+
nighit("สํ", "คีต "), "สังคีต"
15+
) # w2 has trailing space, should still work
16+
self.assertEqual(
17+
nighit("สํ ", "คีต"), "สังคีต"
18+
) # w1 has trailing space, should still work
1319
self.assertEqual(nighit("สํ", "จร"), "สัญจร")
1420
self.assertEqual(nighit("สํ", "ฐาน"), "สัณฐาน")
1521
self.assertEqual(nighit("สํ", "นิษฐาน"), "สันนิษฐาน")
1622
self.assertEqual(nighit("สํ", "ปทา"), "สัมปทา")
1723
self.assertEqual(nighit("สํ", "โยค"), "สังโยค")
24+
self.assertEqual(nighit("", "คีต"), "คีต") # w1 is empty, should return w2
25+
self.assertEqual(nighit("สํ", ""), "สํ") # w2 is empty, should return w1
26+
1827
with self.assertRaises(NotImplementedError):
1928
nighit("abc", "คีต") # w1 does not end with ํ and len > 2
2029
with self.assertRaises(NotImplementedError):
2130
nighit("สํ", "มาร") # consonant ม is not in any supported group
31+
with self.assertRaises(ValueError):
32+
nighit("สํ", "123") # w2 does not contain any Thai consonant
2233

2334
def test_is_native_thai(self):
2435
self.assertFalse(is_native_thai(None)) # type: ignore[arg-type]

0 commit comments

Comments
 (0)