Skip to content

Commit fea0717

Browse files
Copilotbact
andcommitted
Address PR review: fix morphy pos, bahttext None rejection, tltk error message, add tests
Co-authored-by: bact <128572+bact@users.noreply.github.com> Agent-Logs-Url: https://github.com/PyThaiNLP/pythainlp/sessions/4404ee59-fa2b-4a99-9de3-e477cf5b2668
1 parent 44d9765 commit fea0717

5 files changed

Lines changed: 18 additions & 10 deletions

File tree

pythainlp/corpus/wordnet.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ def morphy(form: str, pos: Optional[str] = None) -> str:
428428
>>> morphy("calculated")
429429
'calculate'
430430
"""
431-
return cast(str, wordnet.morphy(form, pos=None))
431+
return cast(str, wordnet.morphy(form, pos=pos))
432432

433433

434434
def custom_lemmas(tab_file: Union[str, IO[str]], lang: str) -> None:

pythainlp/tag/tltk.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
def pos_tag(words: list[str], corpus: str = "tnc") -> list[tuple[str, str]]:
2121
if corpus != "tnc":
22-
raise ValueError(f"tltk not support {0} corpus.")
22+
raise ValueError(f"tltk not support {corpus!r} corpus.")
2323
return cast(list[tuple[str, str]], nlp.pos_tag_wordlist(words))
2424

2525

pythainlp/util/numtoword.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,19 @@
3030
_EXCEPTIONS: dict[str, str] = {"หนึ่งสิบ": "สิบ", "สองสิบ": "ยี่สิบ", "สิบหนึ่ง": "สิบเอ็ด"}
3131

3232

33-
def bahttext(number: Optional[float]) -> str:
33+
def bahttext(number: float) -> str:
3434
"""Converts a number to Thai text and adds
3535
a suffix "บาท" (Baht).
3636
The precision will be fixed at two decimal places (0.00)
3737
to fit "สตางค์" (Satang) unit.
38-
This function works similarly to the `BAHTTEXT` function in Microsoft Excel.
38+
This function works similarly to the ``BAHTTEXT`` function in Microsoft Excel.
3939
40-
:param Optional[float] number: number to be converted into Thai Baht
41-
currency format
40+
:param float number: number to be converted into Thai Baht currency format
4241
:return: text representing the amount of money in the format
4342
of Thai currency
4443
:rtype: str
44+
:raises TypeError: if *number* is not a numeric type
45+
4546
:Example:
4647
::
4748
@@ -56,11 +57,14 @@ def bahttext(number: Optional[float]) -> str:
5657
bahttext(200)
5758
# output: สองร้อยบาทถ้วน
5859
"""
60+
if not isinstance(number, (int, float)):
61+
raise TypeError(
62+
f"number must be a numeric type, not {type(number).__name__!r}"
63+
)
64+
5965
ret = ""
6066

61-
if number is None:
62-
pass
63-
elif number == 0:
67+
if number == 0:
6468
ret = "ศูนย์บาทถ้วน"
6569
else:
6670
num_int_str, num_dec_str = f"{number:.2f}".split(".")

tests/core/test_util.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ def test_number(self):
123123
)
124124
self.assertEqual(bahttext(116), "หนึ่งร้อยสิบหกบาทถ้วน")
125125
self.assertEqual(bahttext(0), "ศูนย์บาทถ้วน")
126-
self.assertEqual(bahttext(None), "")
126+
with self.assertRaises(TypeError):
127+
bahttext(None) # type: ignore[arg-type]
127128
# Edge cases: negative number
128129
self.assertEqual(bahttext(-100), "ลบหนึ่งร้อยบาทถ้วน")
129130
self.assertEqual(bahttext(-50.50), "ลบห้าสิบบาทห้าสิบสตางค์")

tests/extra/testx_corpus.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ def test_wordnet(self):
2727
self.assertIsNotNone(wordnet.lemma("cat.n.01.cat"))
2828

2929
self.assertEqual(wordnet.morphy("dogs"), "dog")
30+
# morphy with pos=None (default) and with explicit pos
31+
self.assertEqual(wordnet.morphy("dogs", pos=None), "dog")
32+
self.assertEqual(wordnet.morphy("dogs", pos="n"), "dog")
3033

3134
bird = wordnet.synset("bird.n.01")
3235
mouse = wordnet.synset("mouse.n.01")

0 commit comments

Comments
 (0)