Skip to main content

Rule-based number formatting using Unicode CLDR data

Project description

Unicode RBNF

A pure Python implementation of rule based number formatting (RBNF) using the Unicode Common Locale Data Repository (CLDR).

This lets you spell out numbers for a large number of locales:

from unicode_rbnf import RbnfEngine

engine = RbnfEngine.for_language("en")
assert engine.format_number(1234).text == "one thousand two hundred thirty-four"

Different formatting purposes are supported as well, depending on the locale:

from unicode_rbnf import RbnfEngine, FormatPurpose

engine = RbnfEngine.for_language("en")
assert engine.format_number(1999, FormatPurpose.CARDINAL).text == "one thousand nine hundred ninety-nine"
assert engine.format_number(1999, FormatPurpose.YEAR).text == "nineteen ninety-nine"
assert engine.format_number(11, FormatPurpose.ORDINAL).text == "eleventh"

For locales with multiple genders, cases, etc., the different texts are accessible in the result of format_number:

from unicode_rbnf import RbnfEngine

engine = RbnfEngine.for_language("de")
print(engine.format_number(1))

Result:

FormatResult(
  text='eins',
  text_by_ruleset={
    'spellout-numbering': 'eins',
    'spellout-cardinal-neuter': 'ein',
    'spellout-cardinal-masculine': 'ein',
    'spellout-cardinal-feminine': 'eine',
    'spellout-cardinal-n': 'einen',
    'spellout-cardinal-r': 'einer',
    'spellout-cardinal-s': 'eines',
    'spellout-cardinal-m': 'einem'
  }
)

The text property of the result holds the text of the ruleset with the shortest name (least specific).

Supported locales

See: https://github.com/unicode-org/cldr/tree/release-44/common/rbnf

Engine implementation

Not all features of the RBNF engine are implemented. The following features are available:

  • Literal text (hundred)
  • Quotient substitution (<< or ←←)
  • Reminder substitution (>> or →→)
  • Optional substitution ([...])
  • Rule substituton (←%ruleset_name←)
  • Rule replacement (=%ruleset_name=)
  • Special rules:
    • Negative numbers (-x)
    • Improper fractions (x.x)
    • Not a number (NaN)
    • Infinity (Inf)

Some features that will need to be added eventually:

  • Proper fraction rules (0.x)
  • Preceding reminder substitution (>>> or →→→)
  • Number format strings (==)
  • Decimal format patterns (#,##0.00)
  • Plural replacements ($(ordinal,one{st}...))

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

unicode_rbnf-2.1.0.tar.gz (81.0 kB view details)

Uploaded Source

Built Distribution

unicode_rbnf-2.1.0-py3-none-any.whl (138.8 kB view details)

Uploaded Python 3

File details

Details for the file unicode_rbnf-2.1.0.tar.gz.

File metadata

  • Download URL: unicode_rbnf-2.1.0.tar.gz
  • Upload date:
  • Size: 81.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for unicode_rbnf-2.1.0.tar.gz
Algorithm Hash digest
SHA256 cbe723a11673c5223ffb37cd472e3f80c8854a4f5a99f5b3ff03b43d9aa60bef
MD5 bd69e71fcf46b6499fed284f16565e29
BLAKE2b-256 4700b44597da3efa1e5affb262e6e3ace72d6a25a431f0ea62fc3f34c2362534

See more details on using hashes here.

File details

Details for the file unicode_rbnf-2.1.0-py3-none-any.whl.

File metadata

  • Download URL: unicode_rbnf-2.1.0-py3-none-any.whl
  • Upload date:
  • Size: 138.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for unicode_rbnf-2.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b1d2526cc6181329c28b20a8b2dc5af8a1b5fb29e2214044b3ccf754eafc945a
MD5 51c670c5981d0056ab435c3716246481
BLAKE2b-256 17661cf84f1abc63c49a2c82ec8fbccb5aa023bb9e381d1b418f824840dd270c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page