Add num_to_thaiword_float and fix เอ็ด rule in num_to_thaiword - #1461
Open
Warit-Yuv wants to merge 3 commits into
Open
Add num_to_thaiword_float and fix เอ็ด rule in num_to_thaiword#1461Warit-Yuv wants to merge 3 commits into
Warit-Yuv wants to merge 3 commits into
Conversation
- Fix เอ็ด rule for millions block (e.g., 101,000,000 -> หนึ่งร้อยเอ็ดล้าน) - Add num_to_thaiword_float for floating-point numbers - Refactor num_to_thaiword to process in 6-digit blocks - Add comprehensive tests for เอ็ด edge cases and float support - Update API documentation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates Thai numeral-to-text conversion utilities by fixing the “เอ็ด” rule for the millions (ล้าน) block in num_to_thaiword, and adds num_to_thaiword_float for floating-point support.
Changes:
- Refactors
num_to_thaiwordto process numbers in 6-digit blocks, correctly applying the “เอ็ด” rule within each block (fixes #1460). - Adds
num_to_thaiword_floatfor rendering decimals using “จุด” and per-digit reading after the decimal point. - Extends unit tests and API documentation to cover the new float function and “เอ็ด” edge cases.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| pythainlp/util/numtoword.py | Refactors integer conversion logic and adds num_to_thaiword_float. |
| pythainlp/util/init.py | Exposes num_to_thaiword_float from the util package. |
| tests/core/test_util.py | Adds test cases for float support and “เอ็ด” edge cases. |
| docs/api/util.rst | Documents the new num_to_thaiword_float API. |
Comment on lines
+157
to
+159
| # Very small float (scientific notation) | ||
| self.assertEqual(num_to_thaiword_float(1e-5), "ศูนย์จุดศูนย์ศูนย์ศูนย์ศูนย์หนึ่ง") | ||
| # Leading zeros after decimal |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
|
Contributor
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (4)
pythainlp/util/numtoword.py:110
- Local loop variable name
replacis unclear/abbreviated; this makes the code harder to read. Use a descriptive name likereplacement.
for search, replac in _EXCEPTIONS.items():
output = output.replace(search, replac)
pythainlp/util/numtoword.py:179
num_to_thaiword_floatcan raiseValueErrorfor non-finite floats (NaN/Inf), but the docstring only documentsTypeError. DocumentValueErroras well so callers know what to handle.
:param float number: a floating-point number to be converted to Thai text
:return: text representing the number in Thai
:rtype: str
:raises TypeError: if *number* is not a numeric type
tests/core/test_util.py:180
- The new non-finite-float handling branch (
math.isfinite->ValueError) is not covered by tests. Add assertions forinf,-inf, andnanso this behavior is locked in.
# Type error
with self.assertRaises(TypeError):
num_to_thaiword_float(None) # type: ignore[arg-type]
with self.assertRaises(TypeError):
num_to_thaiword_float("not a number") # type: ignore[arg-type]
tests/core/test_util.py:162
- Continuation lines in this multi-line
assertEqualare mis-indented, which hurts readability and may trip formatting/lint rules. Align the arguments consistently.
self.assertEqual(
num_to_thaiword_float(1.23e-10),
"ศูนย์จุดศูนย์ศูนย์ศูนย์ศูนย์ศูนย์ศูนย์ศูนย์ศูนย์ศูนย์หนึ่งสองสาม",
)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



What do these changes do
Fix the เอ็ด (one's-place) pronunciation rule in
num_to_thaiwordfor the millions block, and addnum_to_thaiword_floatfor floating-point number support.What was wrong
The original algorithm processed digits right-to-left with a flat modulo-6 approach, causing the เอ็ด rule to only check the very end of the output string. This missed cases where "หนึ่ง" appeared before "ล้าน" in the middle of the string (e.g.,
101,000,000returned"หนึ่งร้อยหนึ่งล้าน"instead of"หนึ่งร้อยเอ็ดล้าน"). Additionally, there was no function to handle floating-point numbers.How this fixes it
Rewrote
num_to_thaiwordto split the number into blocks of 6 digits from right, convert each block independently (applying the เอ็ด rule per block), then join with "ล้าน". Addednum_to_thaiword_floatwhich reusesnum_to_thaiwordfor the integer part, reads the decimal point as "จุด", and reads each digit after the decimal individually.Files changed
pythainlp/util/numtoword.pynum_to_thaiwordwith block-based algorithm; addednum_to_thaiword_floatpythainlp/util/__init__.pynum_to_thaiword_floattests/core/test_util.pynum_to_thaiword_floatdocs/api/util.rstnum_to_thaiword_floatFixes #1460
Your checklist for this pull request