Skip to content

Add to_ordinal() and to_year() for Croatian#1

Merged
Jetman80 merged 1 commit into
microdevops-com:masterfrom
tihomirjauk:add-hr-ordinals-and-year
May 4, 2026
Merged

Add to_ordinal() and to_year() for Croatian#1
Jetman80 merged 1 commit into
microdevops-com:masterfrom
tihomirjauk:add-hr-ordinals-and-year

Conversation

@tihomirjauk
Copy link
Copy Markdown

Follow-up to your Croatian language implementation in this fork (PR savoirfairelinux#643 upstream at savoirfairelinux/num2words).

Per your suggestion in savoirfairelinux#643 (comment) — submitting this against your fork so the implementation is more complete when/if upstream picks it up.

What this adds

  • `to_ordinal()` — masculine nominative singular ordinals via last-cardinal-word substitution (prvi, drugi, treći, četvrti, peti, …, dvadeseti, stoti, tisućiti). Replaces the existing `raise NotImplementedError()`.
  • `to_year()` — feminine-genitive last-word ending (the form Croatians actually use before "godine"). Collapses "jedna tisuća …" → "tisuću …" for years 1000-1999. Overrides the base class default that aliases to `to_cardinal`.
  • Three lookup tables: `_ORDINAL_LAST_MASC`, `_YEAR_LAST_FEM_GEN`, `_ORDINAL_EXACT` (for exact 10^k cases like 1000 → "tisućiti").

Tests

  • Replaces the `assertRaises(NotImplementedError)` in `test_to_ordinal` with 17 real assertions covering ones (regular and irregular: prvi/drugi/treći/četvrti), tens, twenties, hundreds, exact powers of 10, and full 4-digit ordinals.
  • Adds `test_to_year` with 5 assertions for the genitive-feminine year form.
  • All 1,493 existing tests still pass (verified with `pytest tests/ --ignore=tests/test_cli.py`).

Quick verification

```python
from num2words import num2words

num2words(1986, lang='hr', to='ordinal') # 'jedna tisuća devetsto osamdeset šesti'
num2words(1986, lang='hr', to='year') # 'tisuću devetsto osamdeset šeste'
num2words(2024, lang='hr', to='year') # 'dvije tisuće dvadeset četvrte'
num2words(17, lang='hr', to='ordinal') # 'sedamnaesti'
num2words(1000, lang='hr', to='ordinal') # 'tisućiti'
```

Linguistic note

Croatian ordinals decline by gender, case, and number. This patch only handles masculine nominative singular (the unmarked default form), enough for the most common "X-th" use cases. Full case coverage like Russian's `to_ordinal(case=…, gender=…, plural=…)` is a future enhancement.

`to_year()` returns the feminine genitive form because that's what matches the most common Croatian context: "X. godine" (in the year X). The last word changes ending (-i → -e for ordinals, e.g. "šesti" → "šeste"), and "jedna tisuća" collapses to "tisuću" for years 1000-1999.

Happy to revise per any feedback.

PR savoirfairelinux#643 implements cardinals + currency for Croatian but leaves
to_ordinal raising NotImplementedError, and to_year falls through to
to_cardinal via the base class default.

This commit:
- Implements masculine-nominative-singular ordinals (prvi, drugi, ...,
  šesti, dvadeseti, stoti, tisućiti) via last-cardinal-word substitution.
- Implements to_year() with feminine-genitive ending (the form actually
  used before 'godine' in Croatian: 1986 → tisuću devetsto osamdeset
  šeste). Collapses 'jedna tisuća' → 'tisuću' for years 1000-1999.
- Adds ~25 ordinal tests + 5 year tests covering ones (regular and
  irregular), tens, twenties, hundreds, exact powers of 10, and full
  4-digit year forms.
@Jetman80 Jetman80 merged commit 1c081d9 into microdevops-com:master May 4, 2026
@Jetman80
Copy link
Copy Markdown
Member

Jetman80 commented May 4, 2026

@tihomirjauk hvala!

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