Skip to content

Recurse into rule 0 for zero-valued non-optional substitutions - #20

Open
selimfedakar wants to merge 1 commit into
rhasspy:mainfrom
selimfedakar:fix/zero-valued-nonoptional-substitution-recursion
Open

selimfedakar wants to merge 1 commit into
rhasspy:mainfrom
selimfedakar:fix/zero-valued-nonoptional-substitution-recursion

Conversation

@selimfedakar

Copy link
Copy Markdown
Contributor

Recurse into rule 0 for zero-valued non-optional substitutions

Problem

When a substitution evaluates to 0, unicode-rbnf can silently drop a scale word
that ICU speaks. This diverges from ICU's RBNF for languages that define an exact
rule for 0 in the ruleset a bare substitution recurses into:

Language Number Before After (ICU)
ga 20000 fiche fiche míle
lt 100000 šimtas šimtas tūkstančių
lt 100001 šimtas vienas šimtas tūkstančių vienas

The scale words míle (Irish "thousand") and tūkstančių (Lithuanian "thousand")
were being dropped whenever the trailing quotient/remainder was exactly 0.

Root cause

In RbnfEngine.iter_format_number (unicode_rbnf/engine.py), the quotient (<</←←)
and remainder (>>/→→) handlers skip a substitution whose value is 0 when it is a
bare substitution — one that recurses into the current ruleset (part.ruleset_name is None)
— and non-optional. The skip was unconditional for that case.

ICU does not skip unconditionally: it recurses into the ruleset's exact rule for 0
and emits that rule's text when such a rule exists; it emits nothing only when there
is no rule for 0. Rulesets like Irish and Lithuanian thousands define an exact rule 0
precisely so the scale word is spoken, so the unconditional skip lost it.

Fix

Narrow the skip condition at both sites so a bare, non-optional, value-0 substitution
is dropped only when the ruleset has no exact rule for 0:

(part.ruleset_name is None) and (not r_digits) and (0 not in ruleset.numeric_rules)

When 0 in ruleset.numeric_rules, the substitution is no longer skipped and the
existing recursion runs, so rule 0 emits its text (matching ICU).

Scope is deliberately narrow:

  • Optional [...] groups are untouched — an optional integer part is still left out
    of e.g. 0.5.
  • Named-ruleset substitutions are untouched.
  • Rulesets with no rule 0 (e.g. English thousands/hundreds) keep dropping the zero
    substitution — no spurious zero-word is introduced.

Verification

  • Added tests/test_zero_valued_substitution.py covering the ga/lt scale-word
    cases plus an English trailing-zero parity assertion (20 → "twenty",
    1000 → "one thousand", 100 → "one hundred") that must not gain a zero-word.
  • Reverting only the engine change makes the ga/lt assertions fail while the
    English parity assertion still passes, confirming the tests exercise this path and
    the English behavior is unchanged.
  • An ICU differential sweep across ga/lt/hu/ak shows improvements only and
    zero regressions on the zero-valued-substitution emission path.
  • The existing suite is green.

This PR claims only the zero-valued-substitution emission path. It does not claim
rounding-mode parity with ICU.

A bare (recurse-into-current-ruleset), non-optional substitution whose
value is 0 was unconditionally skipped. ICU instead recurses into the
ruleset's exact rule for 0 and emits its text when such a rule exists,
dropping it only when there is no rule 0. The unconditional skip silently
lost scale words (Irish "míle", Lithuanian "tūkstančių").

Guard the skip with `0 not in ruleset.numeric_rules` at both the quotient
and remainder sites. Optional [...] groups and named-ruleset substitutions
are unchanged.

This branch has not been deployed

No deployments
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.

1 participant