Skip to content

Fix divisor computation: use highest power of radix <= rule value - #17

Merged
synesthesiam merged 1 commit into
rhasspy:mainfrom
selimfedakar:fix/rbnf-divisor-highest-power
Nov 19, 2026
Merged

synesthesiam merged 1 commit into
rhasspy:mainfrom
selimfedakar:fix/rbnf-divisor-highest-power

Conversation

@selimfedakar

Copy link
Copy Markdown
Contributor

Fix divisor computation: use highest power of radix <= rule value

What

The divisor used to split a number across a rule (the // quotient and %% remainder substitutions) was computed from a leading-digit-normalized proxy via log(), instead of ICU's actual rule. Two user-reachable effects:

  • Wrong numbers spoken for values ≥ 10000 in affected languages. The Italian ordinal thousands rule is keyed 2001 (<<mila<<). Its divisor must be 1000, but the old code normalized 2001's leading digit to 1 (→ 1001), took ceil(log10(1001)) = 4, and divided by 10000. So format_number(123456789, ruleset_names=["spellout-ordinal-masculine"]) for it dropped a digit in the 456-thousand group and then repeated the multiplier word, speaking it as ...quarantacinquemilaseimila... instead of ...quattrocentocinquantaseimila....
  • Cache corruption, order-dependent output. Same lines: rule.value = int("1" + str(rule.value)[1:]) mutated the shared, cached rule object. A 300 rule became 100 after being used once, so after formatting 321 a subsequent format_number(350) returned the wrong spellout. Results depended on call order.

Why

In ICU RBNF the divisor for a non-special rule is NFRule::expectedExponent() — the highest power of the radix that is <= rule.value. For a rule keyed 2001 with radix 10 that is 1000, not 10000. The previous implementation approximated this with floating-point log() on a digit-normalized value, which (a) picked the wrong power whenever the base value had a non-zero digit after the first, and (b) mutated the cached rule to do so.

Fix

Compute the divisor with an integer loop and no mutation:

divisor = 1
while (divisor * rule.radix) <= rule.value:
    divisor *= rule.radix

No float log() (so exact powers like 1000 are never pushed up by float error), no write-back to rule.value. The now-dead ceil, floor, log imports are removed.

Verification

Differential vs ICU oracle

Swept 88 bundled languages against a PyICU 68.1 oracle over a value grid (22,040 cells), before and after the change:

  • Exactly 4 languages change: it, hu, vec, zu.
  • 24 cells go from wrong → exact ICU match; zero cells get worse.
  • The other 84 languages are byte-identical before and after.

Tests

  • python3 -m pytest -q → 134 passed (131 pre-existing + 3 new).
  • Reverting only unicode_rbnf/engine.py (keeping the tests) → exactly the 3 new tests fail (test_italian_ordinal_thousands, test_divisor_uses_highest_power_not_leading_digit, test_divisor_does_not_mutate_cached_rule), nothing else.

Lint / types

  • mypy clean, pylint 10.00/10 on the changed file.

Honest note: residual it/hu ordinal suffix spelling differences vs ICU still exist. They are a separate pre-existing issue in how the ordinal suffix is appended, unrelated to the divisor, and are not addressed here.

Impact

This library backs Rhasspy / Home Assistant TTS, so an incorrect spellout is read aloud to users. Before this change, any value ≥ 10000 in the affected languages spoke a wrong number (dropped digit plus a duplicated multiplier word). The cache-mutation half also made cardinal output order-dependent, so a correct result could silently become wrong after an unrelated earlier call. The fix brings the affected cells to exact ICU parity while leaving every other language byte-identical. It touches only the divisor computation, with no API change.

The rule divisor was derived from a leading-digit-normalized proxy via
float log(), and that proxy was written back onto the shared cached rule.
A rule keyed 2001 (e.g. Italian <<mila<<) therefore split on 10000 instead
of 1000, dropping a digit and repeating the multiplier word for values
>= 10000, and the cache mutation made output order-dependent.

Compute the divisor as ICU does (NFRule::expectedExponent): the highest
power of the radix that is <= the rule's base value, via an integer loop
with no mutation. Remove the now-dead ceil/floor/log imports.

Differential vs PyICU 68.1 over 88 languages: 24 cells go wrong -> exact
ICU match, zero regress, 84 languages byte-identical. Adds three tests.
@synesthesiam
synesthesiam merged commit beec5da into rhasspy:main Nov 19, 2026
1 check passed
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