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 (
==) - Decimal format patterns (
#,##0.00) - Plural replacements (
$(ordinal,one{st}...))
Project details
Release history Release notifications | RSS feed
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file unicode_rbnf-2.4.0.tar.gz.
File metadata
- Download URL: unicode_rbnf-2.4.0.tar.gz
- Upload date:
- Size: 89.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6d2f12a7581c69ea6218ee61fafcd2da46e1f9986bdcd0964c5151f7c2a938ac
|
|
| MD5 |
c2f0d56cc18fa79f064fd58f17ecdb00
|
|
| BLAKE2b-256 |
571fd952ba97832647e608700c36b22d1c4476016076c9ed1ce74ae814bea55a
|
Provenance
The following attestation bundles were made for unicode_rbnf-2.4.0.tar.gz:
Publisher:
publish.yml on rhasspy/unicode-rbnf
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
unicode_rbnf-2.4.0.tar.gz -
Subject digest:
6d2f12a7581c69ea6218ee61fafcd2da46e1f9986bdcd0964c5151f7c2a938ac - Sigstore transparency entry: 590958380
- Sigstore integration time:
-
Permalink:
rhasspy/unicode-rbnf@e117d4b74a09b094509e836ee3294a6680be26ee -
Branch / Tag:
refs/tags/v2.4.0 - Owner: https://github.com/rhasspy
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@e117d4b74a09b094509e836ee3294a6680be26ee -
Trigger Event:
push
-
Statement type:
File details
Details for the file unicode_rbnf-2.4.0-py3-none-any.whl.
File metadata
- Download URL: unicode_rbnf-2.4.0-py3-none-any.whl
- Upload date:
- Size: 141.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0176b30ac9b7b84008d7dc0f23078055dc10d2671fdadfab5747943243e20e2d
|
|
| MD5 |
fbab40b5a03d979155b85c82faf8058c
|
|
| BLAKE2b-256 |
4b2182f5d435808cba330668a8b69efb180e3ef9739d4998e8cd0381e8c9cb23
|
Provenance
The following attestation bundles were made for unicode_rbnf-2.4.0-py3-none-any.whl:
Publisher:
publish.yml on rhasspy/unicode-rbnf
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
unicode_rbnf-2.4.0-py3-none-any.whl -
Subject digest:
0176b30ac9b7b84008d7dc0f23078055dc10d2671fdadfab5747943243e20e2d - Sigstore transparency entry: 590958385
- Sigstore integration time:
-
Permalink:
rhasspy/unicode-rbnf@e117d4b74a09b094509e836ee3294a6680be26ee -
Branch / Tag:
refs/tags/v2.4.0 - Owner: https://github.com/rhasspy
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@e117d4b74a09b094509e836ee3294a6680be26ee -
Trigger Event:
push
-
Statement type: