unicode-rbnf 2.0.0__tar.gz → 2.2.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (118) hide show
  1. {unicode_rbnf-2.0.0/unicode_rbnf.egg-info → unicode_rbnf-2.2.0}/PKG-INFO +10 -6
  2. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/README.md +2 -0
  3. unicode_rbnf-2.2.0/pyproject.toml +41 -0
  4. unicode_rbnf-2.2.0/tests/test_de.py +56 -0
  5. unicode_rbnf-2.2.0/tests/test_decimal_format.py +9 -0
  6. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/tests/test_es.py +1 -0
  7. unicode_rbnf-2.2.0/tests/test_load_all.py +9 -0
  8. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/__init__.py +4 -3
  9. unicode_rbnf-2.2.0/unicode_rbnf/decimal_format.py +53 -0
  10. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/engine.py +26 -5
  11. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0/unicode_rbnf.egg-info}/PKG-INFO +10 -6
  12. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf.egg-info/SOURCES.txt +5 -7
  13. unicode_rbnf-2.2.0/unicode_rbnf.egg-info/zip-safe +1 -0
  14. unicode_rbnf-2.0.0/MANIFEST.in +0 -2
  15. unicode_rbnf-2.0.0/setup.py +0 -46
  16. unicode_rbnf-2.0.0/tests/test_de.py +0 -18
  17. unicode_rbnf-2.0.0/unicode_rbnf/VERSION +0 -1
  18. unicode_rbnf-2.0.0/unicode_rbnf/rbnf/en_001.xml +0 -14
  19. unicode_rbnf-2.0.0/unicode_rbnf/rbnf/es_419.xml +0 -50
  20. unicode_rbnf-2.0.0/unicode_rbnf/rbnf/nb.xml +0 -13
  21. unicode_rbnf-2.0.0/unicode_rbnf/rbnf/root.xml +0 -724
  22. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/LICENSE.md +0 -0
  23. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/setup.cfg +0 -0
  24. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/tests/test_en.py +0 -0
  25. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/tests/test_engine.py +0 -0
  26. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/tests/test_fi.py +0 -0
  27. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/tests/test_fr.py +0 -0
  28. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/tests/test_ro.py +0 -0
  29. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/__main__.py +0 -0
  30. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/py.typed +0 -0
  31. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/af.xml +0 -0
  32. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/ak.xml +0 -0
  33. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/am.xml +0 -0
  34. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/ar.xml +0 -0
  35. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/az.xml +0 -0
  36. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/be.xml +0 -0
  37. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/bg.xml +0 -0
  38. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/bs.xml +0 -0
  39. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/ca.xml +0 -0
  40. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/ccp.xml +0 -0
  41. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/chr.xml +0 -0
  42. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/cs.xml +0 -0
  43. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/cy.xml +0 -0
  44. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/da.xml +0 -0
  45. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/de.xml +0 -0
  46. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/de_CH.xml +0 -0
  47. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/ee.xml +0 -0
  48. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/el.xml +0 -0
  49. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/en.xml +0 -0
  50. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/en_IN.xml +0 -0
  51. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/eo.xml +0 -0
  52. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/es.xml +0 -0
  53. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/et.xml +0 -0
  54. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/fa.xml +0 -0
  55. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/fa_AF.xml +0 -0
  56. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/ff.xml +0 -0
  57. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/fi.xml +0 -0
  58. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/fil.xml +0 -0
  59. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/fo.xml +0 -0
  60. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/fr.xml +0 -0
  61. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/fr_BE.xml +0 -0
  62. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/fr_CH.xml +0 -0
  63. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/ga.xml +0 -0
  64. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/he.xml +0 -0
  65. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/hi.xml +0 -0
  66. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/hr.xml +0 -0
  67. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/hu.xml +0 -0
  68. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/hy.xml +0 -0
  69. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/id.xml +0 -0
  70. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/is.xml +0 -0
  71. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/it.xml +0 -0
  72. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/ja.xml +0 -0
  73. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/ka.xml +0 -0
  74. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/kk.xml +0 -0
  75. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/kl.xml +0 -0
  76. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/km.xml +0 -0
  77. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/ko.xml +0 -0
  78. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/ky.xml +0 -0
  79. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/lb.xml +0 -0
  80. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/lo.xml +0 -0
  81. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/lrc.xml +0 -0
  82. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/lt.xml +0 -0
  83. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/lv.xml +0 -0
  84. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/mk.xml +0 -0
  85. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/ms.xml +0 -0
  86. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/mt.xml +0 -0
  87. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/my.xml +0 -0
  88. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/ne.xml +0 -0
  89. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/nl.xml +0 -0
  90. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/nn.xml +0 -0
  91. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/no.xml +0 -0
  92. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/pl.xml +0 -0
  93. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/pt.xml +0 -0
  94. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/pt_PT.xml +0 -0
  95. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/qu.xml +0 -0
  96. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/ro.xml +0 -0
  97. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/ru.xml +0 -0
  98. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/se.xml +0 -0
  99. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/sk.xml +0 -0
  100. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/sl.xml +0 -0
  101. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/sq.xml +0 -0
  102. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/sr.xml +0 -0
  103. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/sr_Latn.xml +0 -0
  104. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/su.xml +0 -0
  105. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/sv.xml +0 -0
  106. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/sw.xml +0 -0
  107. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/ta.xml +0 -0
  108. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/th.xml +0 -0
  109. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/tr.xml +0 -0
  110. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/uk.xml +0 -0
  111. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/vec.xml +0 -0
  112. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/vi.xml +0 -0
  113. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/yue.xml +0 -0
  114. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/yue_Hans.xml +0 -0
  115. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/zh.xml +0 -0
  116. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf/rbnf/zh_Hant.xml +0 -0
  117. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf.egg-info/dependency_links.txt +0 -0
  118. {unicode_rbnf-2.0.0 → unicode_rbnf-2.2.0}/unicode_rbnf.egg-info/top_level.txt +0 -0
@@ -1,12 +1,12 @@
1
1
  Metadata-Version: 2.1
2
- Name: unicode_rbnf
3
- Version: 2.0.0
2
+ Name: unicode-rbnf
3
+ Version: 2.2.0
4
4
  Summary: Rule-based number formatting using Unicode CLDR data
5
- Home-page: http://github.com/rhasspy/unicode-rbnf
6
- Author: Michael Hansen
7
- Author-email: mike@rhasspy.org
5
+ Author-email: Michael Hansen <mike@rhasspy.org>
8
6
  License: MIT
9
- Keywords: rbnf unicode number format
7
+ Project-URL: Source Code, https://github.com/rhasspy/unicode-rbnf
8
+ Keywords: rbnf,unicode,number,format
9
+ Platform: any
10
10
  Classifier: Development Status :: 3 - Alpha
11
11
  Classifier: Intended Audience :: Developers
12
12
  Classifier: Topic :: Text Processing :: Linguistic
@@ -16,6 +16,8 @@ Classifier: Programming Language :: Python :: 3.9
16
16
  Classifier: Programming Language :: Python :: 3.10
17
17
  Classifier: Programming Language :: Python :: 3.11
18
18
  Classifier: Programming Language :: Python :: 3.12
19
+ Classifier: Programming Language :: Python :: 3.13
20
+ Requires-Python: >=3.8.0
19
21
  Description-Content-Type: text/markdown
20
22
  License-File: LICENSE.md
21
23
 
@@ -97,3 +99,5 @@ Some features that will need to be added eventually:
97
99
  * Proper fraction rules (`0.x`)
98
100
  * Preceding reminder substitution (`>>>` or `→→→`)
99
101
  * Number format strings (`==`)
102
+ * Decimal format patterns (`#,##0.00`)
103
+ * Plural replacements (`$(ordinal,one{st}...)`)
@@ -76,3 +76,5 @@ Some features that will need to be added eventually:
76
76
  * Proper fraction rules (`0.x`)
77
77
  * Preceding reminder substitution (`>>>` or `→→→`)
78
78
  * Number format strings (`==`)
79
+ * Decimal format patterns (`#,##0.00`)
80
+ * Plural replacements (`$(ordinal,one{st}...)`)
@@ -0,0 +1,41 @@
1
+ [build-system]
2
+ requires = ["setuptools>=62.3"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "unicode-rbnf"
7
+ version = "2.2.0"
8
+ license = {text = "MIT"}
9
+ description = "Rule-based number formatting using Unicode CLDR data"
10
+ readme = "README.md"
11
+ authors = [
12
+ {name = "Michael Hansen", email = "mike@rhasspy.org"}
13
+ ]
14
+ keywords = ["rbnf", "unicode", "number", "format"]
15
+ classifiers = [
16
+ "Development Status :: 3 - Alpha",
17
+ "Intended Audience :: Developers",
18
+ "Topic :: Text Processing :: Linguistic",
19
+ "License :: OSI Approved :: MIT License",
20
+ "Programming Language :: Python :: 3.8",
21
+ "Programming Language :: Python :: 3.9",
22
+ "Programming Language :: Python :: 3.10",
23
+ "Programming Language :: Python :: 3.11",
24
+ "Programming Language :: Python :: 3.12",
25
+ "Programming Language :: Python :: 3.13",
26
+ ]
27
+ requires-python = ">=3.8.0"
28
+
29
+ [project.urls]
30
+ "Source Code" = "https://github.com/rhasspy/unicode-rbnf"
31
+
32
+ [tool.setuptools]
33
+ platforms = ["any"]
34
+ zip-safe = true
35
+ include-package-data = true
36
+
37
+ [tool.setuptools.packages.find]
38
+ include = ["unicode_rbnf"]
39
+
40
+ [tool.setuptools.package-data]
41
+ unicode_rbnf = ["rbnf/*.xml"]
@@ -0,0 +1,56 @@
1
+ from unicode_rbnf import RbnfEngine
2
+
3
+
4
+ def test_german():
5
+ engine = RbnfEngine.for_language("de")
6
+ assert engine.format_number(1).text == "eins"
7
+ assert engine.format_number(2).text == "zwei"
8
+ assert engine.format_number(3).text == "drei"
9
+ assert engine.format_number(4).text == "vier"
10
+ assert engine.format_number(5).text == "fünf"
11
+ assert engine.format_number(6).text == "sechs"
12
+ assert engine.format_number(7).text == "sieben"
13
+ assert engine.format_number(8).text == "acht"
14
+ assert engine.format_number(9).text == "neun"
15
+ assert engine.format_number(10).text == "zehn"
16
+ assert engine.format_number(11).text == "elf"
17
+ assert engine.format_number(12).text == "zwölf"
18
+ assert engine.format_number(13).text == "dreizehn"
19
+ assert engine.format_number(14).text == "vierzehn"
20
+ assert engine.format_number(15).text == "fünfzehn"
21
+ assert engine.format_number(16).text == "sechzehn"
22
+ assert engine.format_number(17).text == "siebzehn"
23
+ assert engine.format_number(18).text == "achtzehn"
24
+ assert engine.format_number(19).text == "neunzehn"
25
+ assert engine.format_number(20).text == "zwanzig"
26
+ assert engine.format_number(21).text == "einundzwanzig"
27
+ assert engine.format_number(22).text == "zweiundzwanzig"
28
+ assert engine.format_number(23).text == "dreiundzwanzig"
29
+ assert engine.format_number(24).text == "vierundzwanzig"
30
+ assert engine.format_number(25).text == "fünfundzwanzig"
31
+ assert engine.format_number(26).text == "sechsundzwanzig"
32
+ assert engine.format_number(27).text == "siebenundzwanzig"
33
+ assert engine.format_number(28).text == "achtundzwanzig"
34
+ assert engine.format_number(29).text == "neunundzwanzig"
35
+ assert engine.format_number(30).text == "dreißig"
36
+ assert engine.format_number(32).text == "zweiunddreißig"
37
+ assert engine.format_number(100).text == "einhundert"
38
+ assert engine.format_number(101).text == "einhunderteins"
39
+ assert engine.format_number(120).text == "einhundertzwanzig"
40
+ assert engine.format_number(121).text == "einhunderteinundzwanzig"
41
+ assert engine.format_number(200).text == "zweihundert"
42
+ assert engine.format_number(1000).text == "eintausend"
43
+ assert engine.format_number(1001).text == "eintausendeins"
44
+ assert engine.format_number(1100).text == "eintausendeinhundert"
45
+ assert engine.format_number(1234).text == "eintausendzweihundertvierunddreißig"
46
+
47
+ # All genders, cases
48
+ assert set(engine.format_number(1).text_by_ruleset.values()) == {
49
+ "ein",
50
+ "eins",
51
+ "eine",
52
+ "eines",
53
+ "einer",
54
+ "einem",
55
+ "einen",
56
+ }
@@ -0,0 +1,9 @@
1
+ from unicode_rbnf.decimal_format import format_decimal
2
+
3
+
4
+ def test_format_decimal() -> None:
5
+ assert format_decimal(12345.6789, "#,##0.00") == "12,345.68"
6
+ assert format_decimal(5, "0000.00") == "0005.00"
7
+ assert format_decimal(12345.6, "#,##0.0#") == "12,345.6"
8
+ assert format_decimal(0.1, "#,##0.00") == "0.10"
9
+ assert format_decimal(12345, "#,##0") == "12,345"
@@ -6,6 +6,7 @@ def test_german():
6
6
  assert engine.format_number(5).text == "cinco"
7
7
  assert engine.format_number(2).text == "dos"
8
8
  assert engine.format_number(5.2).text == "cinco coma dos"
9
+ assert engine.format_number(21).text == "veintiuno"
9
10
 
10
11
  # All genders
11
12
  assert set(engine.format_number(1).text_by_ruleset.values()) == {
@@ -0,0 +1,9 @@
1
+ from unicode_rbnf import RbnfEngine
2
+
3
+ import pytest
4
+
5
+
6
+ @pytest.mark.parametrize("language", RbnfEngine.get_supported_languages())
7
+ def test_load_language(language: str):
8
+ engine = RbnfEngine.for_language(language)
9
+ assert engine.format_number(0).text
@@ -1,10 +1,11 @@
1
- from pathlib import Path
1
+ """Rule-based number formatting using Unicode CLDR data."""
2
+
3
+ import importlib.metadata
2
4
 
3
5
  from .engine import FormatOptions, FormatPurpose, FormatResult, RbnfEngine
4
6
 
5
- _DIR = Path(__file__).parent
7
+ __version__ = importlib.metadata.version("unicode_rbnf")
6
8
 
7
- __version__ = (_DIR / "VERSION").read_text(encoding="utf-8").strip()
8
9
 
9
10
  __all__ = [
10
11
  "__version__",
@@ -0,0 +1,53 @@
1
+ """Handle decimal formatting.
2
+
3
+ See: https://unicode-org.github.io/icu-docs/apidoc/released/icu4c/classicu_1_1DecimalFormat.html
4
+ """
5
+
6
+ from decimal import Decimal
7
+ from typing import Union
8
+
9
+
10
+ def format_decimal(value: Union[int, float, str, Decimal], pattern: str) -> str:
11
+ """Format a number according to a simplified ICU DecimalFormat pattern."""
12
+ # Split the pattern into integer and fractional parts
13
+ if "." in pattern:
14
+ integer_part, fractional_part = pattern.split(".")
15
+ else:
16
+ integer_part, fractional_part = pattern, ""
17
+
18
+ # Determine grouping (e.g., thousands separator)
19
+ grouping = "," in integer_part
20
+ min_integer_digits = integer_part.replace(",", "").count("0")
21
+
22
+ # Determine the number of decimal places
23
+ min_fraction_digits = fractional_part.count("0")
24
+ max_fraction_digits = len(fractional_part)
25
+
26
+ # Round the number to the maximum fractional digits
27
+ format_str = f"{{:.{max_fraction_digits}f}}"
28
+ rounded_value = format_str.format(value)
29
+
30
+ # Split the rounded value into integer and fractional parts
31
+ if fractional_part:
32
+ integer_value, fractional_value = rounded_value.split(".")
33
+ fractional_value = fractional_value[:max_fraction_digits].rstrip("0")
34
+ else:
35
+ integer_value, fractional_value = rounded_value, ""
36
+
37
+ # Apply integer padding
38
+ if len(integer_value) < min_integer_digits:
39
+ integer_value = integer_value.zfill(min_integer_digits)
40
+
41
+ # Apply grouping
42
+ if grouping:
43
+ # pylint: disable=consider-using-f-string
44
+ integer_value = "{:,}".format(int(integer_value))
45
+
46
+ # Combine integer and fractional parts
47
+ if min_fraction_digits > 0:
48
+ fractional_value = fractional_value.ljust(min_fraction_digits, "0")
49
+ formatted_number = f"{integer_value}.{fractional_value}"
50
+ else:
51
+ formatted_number = integer_value
52
+
53
+ return formatted_number
@@ -1,4 +1,5 @@
1
1
  import logging
2
+ import re
2
3
  from abc import ABC
3
4
  from bisect import bisect_left
4
5
  from dataclasses import dataclass, field
@@ -110,6 +111,9 @@ class SubRulePart(RbnfRulePart):
110
111
  ruleset_name: Optional[str] = None
111
112
  """Ruleset name to use during substitution (None for current ruleset name)."""
112
113
 
114
+ format_pattern: Optional[str] = None
115
+ """DecimalFormat pattern (e.g., #,##0.00)."""
116
+
113
117
 
114
118
  @dataclass
115
119
  class ReplaceRulePart(RbnfRulePart):
@@ -292,6 +296,17 @@ class RbnfRule:
292
296
 
293
297
  assert isinstance(part, TextRulePart)
294
298
  part.text += c
299
+ elif c in ("#", "0", ",", "."):
300
+ # decimal format pattern (e.g., #,##0.00)
301
+ assert isinstance(part, SubRulePart)
302
+ assert state in (
303
+ ParseState.SUB_REMAINDER,
304
+ ParseState.SUB_QUOTIENT,
305
+ ), state
306
+ if part.format_pattern is None:
307
+ part.format_pattern = ""
308
+
309
+ part.format_pattern += c
295
310
  else:
296
311
  raise ValueError(f"Got {c} in {state}")
297
312
 
@@ -459,7 +474,7 @@ class RbnfEngine:
459
474
  raise ValueError("Missing identity/language element")
460
475
 
461
476
  language = lang_elem.attrib["type"]
462
- if language != self.language:
477
+ if (language != self.language) and (not self.language.startswith(language)):
463
478
  raise ValueError(f"Expected language {self.language}, got {language}")
464
479
 
465
480
  for group_elem in root.findall("rbnf//ruleset"):
@@ -473,7 +488,7 @@ class RbnfEngine:
473
488
  continue
474
489
 
475
490
  value_str = rule_elem.attrib["value"]
476
- radix = int(rule_elem.attrib.get("radix", 10))
491
+ radix = int(re.sub(r"[^0-9]+", "", rule_elem.attrib.get("radix", "10")))
477
492
 
478
493
  self.add_rule(
479
494
  value_str,
@@ -584,8 +599,12 @@ class RbnfEngine:
584
599
  if part.text:
585
600
  yield part.text
586
601
  elif isinstance(part, SubRulePart):
587
- if (part.type == SubType.QUOTIENT) and (q > 0):
588
- if (q == 0) and (part.ruleset_name is None):
602
+ sub_part: SubRulePart = part
603
+
604
+ if part.type == SubType.QUOTIENT:
605
+ if (q == 0) and (
606
+ sub_part.is_optional or (part.ruleset_name is None)
607
+ ):
589
608
  # Rulesets can use quotients of zero
590
609
  continue
591
610
 
@@ -599,7 +618,9 @@ class RbnfEngine:
599
618
  if part.text_after:
600
619
  yield part.text_after
601
620
  elif part.type == SubType.REMAINDER:
602
- if (r == 0) and (part.ruleset_name is None):
621
+ if (r == 0) and (
622
+ sub_part.is_optional or (part.ruleset_name is None)
623
+ ):
603
624
  # Rulesets can use remainders of zero
604
625
  continue
605
626
 
@@ -1,12 +1,12 @@
1
1
  Metadata-Version: 2.1
2
- Name: unicode_rbnf
3
- Version: 2.0.0
2
+ Name: unicode-rbnf
3
+ Version: 2.2.0
4
4
  Summary: Rule-based number formatting using Unicode CLDR data
5
- Home-page: http://github.com/rhasspy/unicode-rbnf
6
- Author: Michael Hansen
7
- Author-email: mike@rhasspy.org
5
+ Author-email: Michael Hansen <mike@rhasspy.org>
8
6
  License: MIT
9
- Keywords: rbnf unicode number format
7
+ Project-URL: Source Code, https://github.com/rhasspy/unicode-rbnf
8
+ Keywords: rbnf,unicode,number,format
9
+ Platform: any
10
10
  Classifier: Development Status :: 3 - Alpha
11
11
  Classifier: Intended Audience :: Developers
12
12
  Classifier: Topic :: Text Processing :: Linguistic
@@ -16,6 +16,8 @@ Classifier: Programming Language :: Python :: 3.9
16
16
  Classifier: Programming Language :: Python :: 3.10
17
17
  Classifier: Programming Language :: Python :: 3.11
18
18
  Classifier: Programming Language :: Python :: 3.12
19
+ Classifier: Programming Language :: Python :: 3.13
20
+ Requires-Python: >=3.8.0
19
21
  Description-Content-Type: text/markdown
20
22
  License-File: LICENSE.md
21
23
 
@@ -97,3 +99,5 @@ Some features that will need to be added eventually:
97
99
  * Proper fraction rules (`0.x`)
98
100
  * Preceding reminder substitution (`>>>` or `→→→`)
99
101
  * Number format strings (`==`)
102
+ * Decimal format patterns (`#,##0.00`)
103
+ * Plural replacements (`$(ordinal,one{st}...)`)
@@ -1,24 +1,26 @@
1
1
  LICENSE.md
2
- MANIFEST.in
3
2
  README.md
3
+ pyproject.toml
4
4
  setup.cfg
5
- setup.py
6
5
  tests/test_de.py
6
+ tests/test_decimal_format.py
7
7
  tests/test_en.py
8
8
  tests/test_engine.py
9
9
  tests/test_es.py
10
10
  tests/test_fi.py
11
11
  tests/test_fr.py
12
+ tests/test_load_all.py
12
13
  tests/test_ro.py
13
- unicode_rbnf/VERSION
14
14
  unicode_rbnf/__init__.py
15
15
  unicode_rbnf/__main__.py
16
+ unicode_rbnf/decimal_format.py
16
17
  unicode_rbnf/engine.py
17
18
  unicode_rbnf/py.typed
18
19
  unicode_rbnf.egg-info/PKG-INFO
19
20
  unicode_rbnf.egg-info/SOURCES.txt
20
21
  unicode_rbnf.egg-info/dependency_links.txt
21
22
  unicode_rbnf.egg-info/top_level.txt
23
+ unicode_rbnf.egg-info/zip-safe
22
24
  unicode_rbnf/rbnf/af.xml
23
25
  unicode_rbnf/rbnf/ak.xml
24
26
  unicode_rbnf/rbnf/am.xml
@@ -38,11 +40,9 @@ unicode_rbnf/rbnf/de_CH.xml
38
40
  unicode_rbnf/rbnf/ee.xml
39
41
  unicode_rbnf/rbnf/el.xml
40
42
  unicode_rbnf/rbnf/en.xml
41
- unicode_rbnf/rbnf/en_001.xml
42
43
  unicode_rbnf/rbnf/en_IN.xml
43
44
  unicode_rbnf/rbnf/eo.xml
44
45
  unicode_rbnf/rbnf/es.xml
45
- unicode_rbnf/rbnf/es_419.xml
46
46
  unicode_rbnf/rbnf/et.xml
47
47
  unicode_rbnf/rbnf/fa.xml
48
48
  unicode_rbnf/rbnf/fa_AF.xml
@@ -78,7 +78,6 @@ unicode_rbnf/rbnf/mk.xml
78
78
  unicode_rbnf/rbnf/ms.xml
79
79
  unicode_rbnf/rbnf/mt.xml
80
80
  unicode_rbnf/rbnf/my.xml
81
- unicode_rbnf/rbnf/nb.xml
82
81
  unicode_rbnf/rbnf/ne.xml
83
82
  unicode_rbnf/rbnf/nl.xml
84
83
  unicode_rbnf/rbnf/nn.xml
@@ -88,7 +87,6 @@ unicode_rbnf/rbnf/pt.xml
88
87
  unicode_rbnf/rbnf/pt_PT.xml
89
88
  unicode_rbnf/rbnf/qu.xml
90
89
  unicode_rbnf/rbnf/ro.xml
91
- unicode_rbnf/rbnf/root.xml
92
90
  unicode_rbnf/rbnf/ru.xml
93
91
  unicode_rbnf/rbnf/se.xml
94
92
  unicode_rbnf/rbnf/sk.xml
@@ -1,2 +0,0 @@
1
- include LICENSE.md
2
- include unicode_rbnf/rbnf/*.xml
@@ -1,46 +0,0 @@
1
- #!/usr/bin/env python3
2
- from pathlib import Path
3
-
4
- import setuptools
5
- from setuptools import setup
6
-
7
- module_name = "unicode_rbnf"
8
- this_dir = Path(__file__).parent
9
- module_dir = this_dir / module_name
10
- data_files = list((module_dir / "rbnf").glob("*.xml"))
11
-
12
- version_path = module_dir / "VERSION"
13
- data_files.append(version_path)
14
- version = version_path.read_text(encoding="utf-8").strip()
15
-
16
- long_description = (this_dir / "README.md").read_text()
17
-
18
- # -----------------------------------------------------------------------------
19
-
20
- setup(
21
- name=module_name,
22
- version=version,
23
- description="Rule-based number formatting using Unicode CLDR data",
24
- long_description=long_description,
25
- long_description_content_type="text/markdown",
26
- url="http://github.com/rhasspy/unicode-rbnf",
27
- author="Michael Hansen",
28
- author_email="mike@rhasspy.org",
29
- license="MIT",
30
- packages=setuptools.find_packages(),
31
- package_data={
32
- module_name: [str(p.relative_to(module_dir)) for p in data_files] + ["py.typed"]
33
- },
34
- classifiers=[
35
- "Development Status :: 3 - Alpha",
36
- "Intended Audience :: Developers",
37
- "Topic :: Text Processing :: Linguistic",
38
- "License :: OSI Approved :: MIT License",
39
- "Programming Language :: Python :: 3.8",
40
- "Programming Language :: Python :: 3.9",
41
- "Programming Language :: Python :: 3.10",
42
- "Programming Language :: Python :: 3.11",
43
- "Programming Language :: Python :: 3.12",
44
- ],
45
- keywords="rbnf unicode number format",
46
- )
@@ -1,18 +0,0 @@
1
- from unicode_rbnf import RbnfEngine
2
-
3
-
4
- def test_german():
5
- engine = RbnfEngine.for_language("de")
6
- assert engine.format_number(13).text == "dreizehn"
7
- assert engine.format_number(32).text == "zweiunddreißig"
8
-
9
- # All genders, cases
10
- assert set(engine.format_number(1).text_by_ruleset.values()) == {
11
- "ein",
12
- "eins",
13
- "eine",
14
- "eines",
15
- "einer",
16
- "einem",
17
- "einen",
18
- }
@@ -1 +0,0 @@
1
- 2.0.0
@@ -1,14 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8" ?>
2
- <!DOCTYPE ldml SYSTEM "../../common/dtd/ldml.dtd">
3
- <!--
4
- Copyright © 1991-2015 Unicode, Inc.
5
- CLDR data files are interpreted according to the LDML specification (http://unicode.org/reports/tr35/)
6
- For terms of use, see http://www.unicode.org/copyright.html
7
- -->
8
- <ldml>
9
- <identity>
10
- <version number="$Revision$"/>
11
- <language type="en"/>
12
- <territory type="001"/>
13
- </identity>
14
- </ldml>
@@ -1,50 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8" ?>
2
- <!DOCTYPE ldml SYSTEM "../../common/dtd/ldml.dtd">
3
- <!--
4
- Copyright © 1991-2014 Unicode, Inc.
5
- CLDR data files are interpreted according to the LDML specification (http://unicode.org/reports/tr35/)
6
- For terms of use, see http://www.unicode.org/copyright.html
7
- -->
8
- <ldml>
9
- <identity>
10
- <version number="$Revision$"/>
11
- <language type="es"/>
12
- <territory type="419"/>
13
- </identity>
14
- <rbnf>
15
- <rulesetGrouping type="OrdinalRules">
16
- <ruleset type="dord-mascabbrev" access="private">
17
- <rbnfrule value="0">º;</rbnfrule>
18
- <rbnfrule value="1">ᵉʳ;</rbnfrule>
19
- <rbnfrule value="2">º;</rbnfrule>
20
- <rbnfrule value="3">ᵉʳ;</rbnfrule>
21
- <rbnfrule value="4">º;</rbnfrule>
22
- <rbnfrule value="20">→→;</rbnfrule>
23
- <rbnfrule value="100">→→;</rbnfrule>
24
- </ruleset>
25
- <ruleset type="digits-ordinal-masculine-adjective">
26
- <rbnfrule value="-x">−→→;</rbnfrule>
27
- <rbnfrule value="0">=#,##0==%%dord-mascabbrev=.;</rbnfrule>
28
- </ruleset>
29
- <ruleset type="digits-ordinal-masculine">
30
- <rbnfrule value="-x">−→→;</rbnfrule>
31
- <rbnfrule value="0">=#,##0=º.;</rbnfrule>
32
- </ruleset>
33
- <ruleset type="digits-ordinal-feminine">
34
- <rbnfrule value="-x">−→→;</rbnfrule>
35
- <rbnfrule value="0">=#,##0=ª.;</rbnfrule>
36
- </ruleset>
37
- <ruleset type="digits-ordinal-masculine-plural">
38
- <rbnfrule value="-x">−→→;</rbnfrule>
39
- <rbnfrule value="0">=#,##0=ᵒˢ.;</rbnfrule>
40
- </ruleset>
41
- <ruleset type="digits-ordinal-feminine-plural">
42
- <rbnfrule value="-x">−→→;</rbnfrule>
43
- <rbnfrule value="0">=#,##0=ᵃˢ.;</rbnfrule>
44
- </ruleset>
45
- <ruleset type="digits-ordinal">
46
- <rbnfrule value="0">=%digits-ordinal-masculine=;</rbnfrule>
47
- </ruleset>
48
- </rulesetGrouping>
49
- </rbnf>
50
- </ldml>
@@ -1,13 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8" ?>
2
- <!DOCTYPE ldml SYSTEM "../../common/dtd/ldml.dtd">
3
- <!-- Copyright © 1991-2020 Unicode, Inc.
4
- For terms of use, see http://www.unicode.org/copyright.html
5
- Unicode and the Unicode Logo are registered trademarks of Unicode, Inc. in the U.S. and other countries.
6
- CLDR data files are interpreted according to the LDML specification (http://unicode.org/reports/tr35/)
7
- -->
8
- <ldml>
9
- <identity>
10
- <version number="$Revision$"/>
11
- <language type="nb"/>
12
- </identity>
13
- </ldml>