Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pythainlp/util/numtoword.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

_VALUES: list[str] = [
"",
"หนึ่ง",

Check failure on line 19 in pythainlp/util/numtoword.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Define a constant instead of duplicating this literal "หนึ่ง" 3 times.

See more on https://sonarcloud.io/project/issues?id=PyThaiNLP_pythainlp&issues=AZ05U6wskGZROQtBDT23&open=AZ05U6wskGZROQtBDT23&pullRequest=1386
"สอง",
"สาม",
"สี่",
Expand Down Expand Up @@ -121,6 +121,10 @@
for search, replac in _EXCEPTIONS.items():
output = output.replace(search, replac)

# เอ็ด rule: trailing หนึ่ง in ones place (after any place marker)
if number != 1 and number != -1 and output.endswith("หนึ่ง"):
output = output[: -len("หนึ่ง")] + "เอ็ด"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hacky but works.


if number_temp < 0:
output = "ลบ" + output

Expand Down
6 changes: 6 additions & 0 deletions tests/core/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@ def test_number(self):
self.assertEqual(num_to_thaiword(0), "ศูนย์")
self.assertEqual(num_to_thaiword(112), "หนึ่งร้อยสิบสอง")
self.assertEqual(num_to_thaiword(-273), "ลบสองร้อยเจ็ดสิบสาม")
# เอ็ด rule: ones=1 must use เอ็ด when number > 1
self.assertEqual(num_to_thaiword(1), "หนึ่ง")
Comment thread
bact marked this conversation as resolved.
self.assertEqual(num_to_thaiword(-1), "ลบหนึ่ง")
self.assertEqual(num_to_thaiword(101), "หนึ่งร้อยเอ็ด")
Comment thread
bact marked this conversation as resolved.
self.assertEqual(num_to_thaiword(1001), "หนึ่งพันเอ็ด")
self.assertEqual(num_to_thaiword(1000001), "หนึ่งล้านเอ็ด")

self.assertEqual(thaiword_to_num("ศูนย์"), 0)
self.assertEqual(thaiword_to_num("แปด"), 8)
Expand Down
Loading