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
)
- Negative numbers (
Some features that will need to be added eventually:
- Proper fraction rules (
0.x
) - Preceding reminder substitution (
>>>
or→→→
) - Number format strings (
==
)
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
Built Distribution
File details
Details for the file unicode_rbnf-2.0.0.tar.gz
.
File metadata
- Download URL: unicode_rbnf-2.0.0.tar.gz
- Upload date:
- Size: 84.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b9e031fcbe3268a0921eda91c5cc2cb337e39ac9f2e5c3d1cd4ce29b64b4fb69 |
|
MD5 | 86ce860f35a6d1ab3244943f49185350 |
|
BLAKE2b-256 | 3fa5eff0e2cced32285b3102ea99b4a8490d7baf1490c577d9f1d2d7ea3566d0 |
Provenance
File details
Details for the file unicode_rbnf-2.0.0-py3-none-any.whl
.
File metadata
- Download URL: unicode_rbnf-2.0.0-py3-none-any.whl
- Upload date:
- Size: 137.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 24f8bd8e5f8720429db5e2281ef25de32d4d1094cc506e604c807c1a8b758ae0 |
|
MD5 | 120ff6da0a9cfdc5523be4d4eb173d17 |
|
BLAKE2b-256 | 86eae5a41ce0c0b6a2acd368c7da907a824ae65cbc9b60487d7b444dc350abc0 |