Skip to content

Emit digits for =<DecimalFormat pattern>= replace rules - #15

Merged
synesthesiam merged 1 commit into
rhasspy:mainfrom
selimfedakar:fix-decimal-format-replace-pattern
Nov 17, 2026
Merged

synesthesiam merged 1 commit into
rhasspy:mainfrom
selimfedakar:fix-decimal-format-replace-pattern

Conversation

@selimfedakar

Copy link
Copy Markdown
Contributor

Emit digits for =<DecimalFormat pattern>= replace rules

What

An RBNF =...= replace rule whose target is a DecimalFormat pattern (=#,##0=, =#,##,##0=, =#,##0.#=, =0=) currently raises NoRuleForNumberError instead of emitting the grouped digits. Two user-reachable effects:

  • Large numbers fall back to a decimal format in most languages. RbnfEngine.for_language("en").format_number(10**18) raises NoRuleForNumberError on main. The English spellout-cardinal rule for 1000000000000000000 is =#,##0=, so every value ≥ 1e18 fails. 86 of 87 bundled languages fail at 1e18 before this change.
  • digits-ordinal-* rulesets are entirely broken. for_language("hi").format_number(7, ruleset_names=["digits-ordinal-masculine"]) raises NoRuleForNumberError on main. These non-private rulesets (Hindi =#,##,##0=वाँ, Ewe, etc.) are the common written ordinal form and were unusable for every input.

After the fix:

  • en format_number(10**18) → 1,000,000,000,000,000,000
  • hi format_number(7, ruleset_names=["digits-ordinal-masculine"]) → 7वाँ

Why

In ICU RBNF, =pattern= means "format the number with this DecimalFormat pattern" (grouped digits), not "substitute a ruleset." The engine instead treated the pattern string as a ruleset_name and recursed into iter_format_number(..., ruleset_name="#,##0"), which has no matching ruleset → RulesetNotFoundError → the ruleset is skipped → NoRuleForNumberError. The repo already ships format_decimal (decimal_format.py, covered by test_decimal_format.py) for exactly this, but it was never wired into the engine.

Fix

In the ReplaceRulePart branch, if the replace target matches a DecimalFormat pattern (^[#0,.]+$), emit format_decimal(number, pattern); otherwise recurse as before. The regex is unambiguous: ruleset names always contain letters, and across all 87 bundled languages the only letterless replace targets are #,##,##0, #,##0, #,##0.#, and 0.

Tests

Adds test_replace_with_decimal_format_pattern plus a 1e18 assertion in test_en.py. Full suite passes (128 tests); both new assertions fail on main and pass with the fix. No regression in existing spellout output (cardinals, ordinals, fractions, years verified unchanged).

A =...= replacement whose target is a DecimalFormat pattern (=#,##0=,
=#,##,##0=, =0=) was treated as a ruleset-name replacement and looked
up as a ruleset. No such ruleset exists, so the lookup raised
RulesetNotFoundError and the number produced no output.

CLDR uses these pattern replacements as the fallback once a language
runs out of spelled-out words (English spellout-cardinal routes
1e18 and up through =#,##0=) and for digit-based ordinal rulesets
(Hindi digits-ordinal-masculine spells 7 as "7वाँ"). Every such
input failed with NoRuleForNumberError across the public API,
including when the ruleset was requested by name.

Detect a DecimalFormat pattern (only #, 0, grouping commas and a
decimal point, never a letter) and render it with the existing
format_decimal helper, which was defined and tested but never wired
into the engine, instead of recursing into a non-existent ruleset.
@synesthesiam
synesthesiam merged commit daaec64 into rhasspy:main Nov 17, 2026
1 check passed
@synesthesiam

Copy link
Copy Markdown
Member

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants