unicode-rbnf 2.2.0__tar.gz → 2.4.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 (121) hide show
  1. {unicode_rbnf-2.2.0/unicode_rbnf.egg-info → unicode_rbnf-2.4.0}/PKG-INFO +10 -2
  2. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/pyproject.toml +11 -1
  3. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/tests/test_en.py +4 -3
  4. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/tests/test_engine.py +25 -3
  5. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/tests/test_fr.py +2 -0
  6. unicode_rbnf-2.4.0/tests/test_ko.py +8 -0
  7. unicode_rbnf-2.4.0/tests/test_pl.py +49 -0
  8. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/tests/test_ro.py +1 -1
  9. unicode_rbnf-2.4.0/tests/test_sl.py +6 -0
  10. unicode_rbnf-2.4.0/tests/test_zu.py +36 -0
  11. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/engine.py +214 -30
  12. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/de_CH.xml +2 -2
  13. unicode_rbnf-2.4.0/unicode_rbnf/rbnf/en_001.xml +14 -0
  14. unicode_rbnf-2.4.0/unicode_rbnf/rbnf/es_419.xml +50 -0
  15. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/id.xml +4 -1
  16. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/kk.xml +1 -1
  17. unicode_rbnf-2.4.0/unicode_rbnf/rbnf/nb.xml +13 -0
  18. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/ro.xml +6 -6
  19. unicode_rbnf-2.4.0/unicode_rbnf/rbnf/root.xml +724 -0
  20. unicode_rbnf-2.4.0/unicode_rbnf/rbnf/sl.xml +75 -0
  21. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/sr_Latn.xml +2 -2
  22. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/yue_Hans.xml +2 -2
  23. unicode_rbnf-2.4.0/unicode_rbnf/rbnf/zu.xml +120 -0
  24. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0/unicode_rbnf.egg-info}/PKG-INFO +10 -2
  25. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf.egg-info/SOURCES.txt +11 -1
  26. unicode_rbnf-2.4.0/unicode_rbnf.egg-info/requires.txt +8 -0
  27. unicode_rbnf-2.2.0/unicode_rbnf/rbnf/sl.xml +0 -145
  28. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/LICENSE.md +0 -0
  29. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/README.md +0 -0
  30. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/setup.cfg +0 -0
  31. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/tests/test_de.py +0 -0
  32. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/tests/test_decimal_format.py +0 -0
  33. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/tests/test_es.py +0 -0
  34. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/tests/test_fi.py +0 -0
  35. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/tests/test_load_all.py +2 -2
  36. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/__init__.py +0 -0
  37. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/__main__.py +0 -0
  38. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/decimal_format.py +0 -0
  39. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/py.typed +0 -0
  40. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/af.xml +0 -0
  41. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/ak.xml +0 -0
  42. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/am.xml +0 -0
  43. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/ar.xml +0 -0
  44. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/az.xml +0 -0
  45. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/be.xml +0 -0
  46. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/bg.xml +0 -0
  47. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/bs.xml +0 -0
  48. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/ca.xml +0 -0
  49. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/ccp.xml +0 -0
  50. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/chr.xml +0 -0
  51. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/cs.xml +0 -0
  52. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/cy.xml +0 -0
  53. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/da.xml +0 -0
  54. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/de.xml +0 -0
  55. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/ee.xml +0 -0
  56. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/el.xml +0 -0
  57. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/en.xml +0 -0
  58. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/en_IN.xml +0 -0
  59. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/eo.xml +0 -0
  60. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/es.xml +0 -0
  61. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/et.xml +0 -0
  62. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/fa.xml +0 -0
  63. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/fa_AF.xml +0 -0
  64. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/ff.xml +0 -0
  65. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/fi.xml +0 -0
  66. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/fil.xml +0 -0
  67. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/fo.xml +0 -0
  68. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/fr.xml +0 -0
  69. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/fr_BE.xml +0 -0
  70. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/fr_CH.xml +0 -0
  71. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/ga.xml +0 -0
  72. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/he.xml +0 -0
  73. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/hi.xml +0 -0
  74. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/hr.xml +0 -0
  75. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/hu.xml +0 -0
  76. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/hy.xml +0 -0
  77. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/is.xml +0 -0
  78. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/it.xml +0 -0
  79. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/ja.xml +0 -0
  80. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/ka.xml +0 -0
  81. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/kl.xml +0 -0
  82. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/km.xml +0 -0
  83. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/ko.xml +0 -0
  84. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/ky.xml +0 -0
  85. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/lb.xml +0 -0
  86. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/lo.xml +0 -0
  87. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/lrc.xml +0 -0
  88. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/lt.xml +0 -0
  89. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/lv.xml +0 -0
  90. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/mk.xml +0 -0
  91. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/ms.xml +0 -0
  92. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/mt.xml +0 -0
  93. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/my.xml +0 -0
  94. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/ne.xml +0 -0
  95. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/nl.xml +0 -0
  96. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/nn.xml +0 -0
  97. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/no.xml +0 -0
  98. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/pl.xml +0 -0
  99. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/pt.xml +0 -0
  100. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/pt_PT.xml +0 -0
  101. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/qu.xml +0 -0
  102. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/ru.xml +0 -0
  103. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/se.xml +0 -0
  104. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/sk.xml +0 -0
  105. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/sq.xml +0 -0
  106. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/sr.xml +0 -0
  107. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/su.xml +0 -0
  108. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/sv.xml +0 -0
  109. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/sw.xml +0 -0
  110. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/ta.xml +0 -0
  111. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/th.xml +0 -0
  112. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/tr.xml +0 -0
  113. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/uk.xml +0 -0
  114. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/vec.xml +0 -0
  115. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/vi.xml +0 -0
  116. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/yue.xml +0 -0
  117. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/zh.xml +0 -0
  118. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf/rbnf/zh_Hant.xml +0 -0
  119. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf.egg-info/dependency_links.txt +0 -0
  120. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf.egg-info/top_level.txt +0 -0
  121. {unicode_rbnf-2.2.0 → unicode_rbnf-2.4.0}/unicode_rbnf.egg-info/zip-safe +0 -0
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.4
2
2
  Name: unicode-rbnf
3
- Version: 2.2.0
3
+ Version: 2.4.0
4
4
  Summary: Rule-based number formatting using Unicode CLDR data
5
5
  Author-email: Michael Hansen <mike@rhasspy.org>
6
6
  License: MIT
@@ -20,6 +20,14 @@ Classifier: Programming Language :: Python :: 3.13
20
20
  Requires-Python: >=3.8.0
21
21
  Description-Content-Type: text/markdown
22
22
  License-File: LICENSE.md
23
+ Provides-Extra: dev
24
+ Requires-Dist: black==24.8.0; extra == "dev"
25
+ Requires-Dist: flake8==7.1.1; extra == "dev"
26
+ Requires-Dist: mypy==1.14.0; extra == "dev"
27
+ Requires-Dist: pylint==3.2.7; extra == "dev"
28
+ Requires-Dist: pytest==8.3.4; extra == "dev"
29
+ Requires-Dist: build==1.2.2; extra == "dev"
30
+ Dynamic: license-file
23
31
 
24
32
  # Unicode RBNF
25
33
 
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "unicode-rbnf"
7
- version = "2.2.0"
7
+ version = "2.4.0"
8
8
  license = {text = "MIT"}
9
9
  description = "Rule-based number formatting using Unicode CLDR data"
10
10
  readme = "README.md"
@@ -29,6 +29,16 @@ requires-python = ">=3.8.0"
29
29
  [project.urls]
30
30
  "Source Code" = "https://github.com/rhasspy/unicode-rbnf"
31
31
 
32
+ [project.optional-dependencies]
33
+ dev = [
34
+ "black==24.8.0",
35
+ "flake8==7.1.1",
36
+ "mypy==1.14.0",
37
+ "pylint==3.2.7",
38
+ "pytest==8.3.4",
39
+ "build==1.2.2",
40
+ ]
41
+
32
42
  [tool.setuptools]
33
43
  platforms = ["any"]
34
44
  zip-safe = true
@@ -1,4 +1,4 @@
1
- from unicode_rbnf import RbnfEngine, FormatPurpose
1
+ from unicode_rbnf import FormatPurpose, RbnfEngine
2
2
 
3
3
 
4
4
  def test_english():
@@ -32,11 +32,12 @@ def test_english():
32
32
  # Special rules
33
33
  assert engine.format_number(-1).text == "minus one"
34
34
  assert engine.format_number(float("nan")).text == "not a number"
35
- assert engine.format_number(float("inf")).text == "infinite"
35
+ assert engine.format_number(float("inf")).text == "infinity"
36
36
 
37
37
  # Fractions
38
- assert engine.format_number(3.14).text == "three point fourteen"
38
+ assert engine.format_number(3.14).text == "three point one four"
39
39
  assert engine.format_number("5.3").text == "five point three"
40
+ assert engine.format_number(0.0123).text == "zero point zero one two three"
40
41
 
41
42
  # Ordinals
42
43
  assert engine.format_number(20, FormatPurpose.ORDINAL).text == "twentieth"
@@ -1,10 +1,13 @@
1
+ import pytest
2
+
1
3
  from unicode_rbnf.engine import (
4
+ FormatResult,
5
+ NoRuleForNumberError,
6
+ RbnfEngine,
2
7
  RbnfRule,
3
- TextRulePart,
4
8
  SubRulePart,
5
9
  SubType,
6
- RbnfEngine,
7
- FormatResult,
10
+ TextRulePart,
8
11
  )
9
12
 
10
13
 
@@ -65,6 +68,7 @@ def test_format_number():
65
68
 
66
69
  assert engine.format_number(222) == FormatResult(
67
70
  text="two hundred twenty-two",
71
+ text_ruleset="spellout-cardinal",
68
72
  text_by_ruleset={"spellout-cardinal": "two hundred twenty-two"},
69
73
  )
70
74
 
@@ -78,3 +82,21 @@ def test_zero_rules():
78
82
  assert (
79
83
  engine.format_number(1, ruleset_names=["ruleset_1"]).text == "abc efgone hijdef"
80
84
  )
85
+
86
+
87
+ def test_no_rules_for_number():
88
+ engine = RbnfEngine("en")
89
+
90
+ with pytest.raises(NoRuleForNumberError):
91
+ engine.format_number(1, ruleset_names=["does_not_exist"])
92
+
93
+
94
+ def test_parse_remainder_whitespace():
95
+ assert RbnfRule.parse(12.34, "←← x →→→;") == RbnfRule(
96
+ 12,
97
+ parts=[
98
+ SubRulePart(SubType.QUOTIENT),
99
+ TextRulePart(" x"),
100
+ SubRulePart(SubType.REMAINDER, text_before=" "),
101
+ ],
102
+ )
@@ -10,3 +10,5 @@ def test_french():
10
10
  "un",
11
11
  "une",
12
12
  }
13
+
14
+ assert engine.format_number(2.5).text == "deux virgule cinq"
@@ -0,0 +1,8 @@
1
+ from unicode_rbnf import RbnfEngine
2
+
3
+
4
+ def test_korean():
5
+ engine = RbnfEngine.for_language("ko")
6
+
7
+ assert engine.format_number(20.03).text == "이십점영삼"
8
+ assert engine.format_number(20.0123).text == "이십점영일이삼"
@@ -0,0 +1,49 @@
1
+ from unicode_rbnf import FormatPurpose, RbnfEngine
2
+
3
+
4
+ def test_polish():
5
+ engine = RbnfEngine.for_language("pl")
6
+
7
+ assert engine.format_number(7).text == "siedem"
8
+ assert engine.format_number(15).text == "piętnaście"
9
+ assert engine.format_number(42).text == "czterdzieści dwa"
10
+ assert engine.format_number(100).text == "sto"
11
+ assert engine.format_number(143).text == "sto czterdzieści trzy"
12
+ assert engine.format_number(1000).text == "tysiąc"
13
+ assert engine.format_number(1234).text == "tysiąc dwieście trzydzieści cztery"
14
+ assert engine.format_number(3144).text == "trzy tysiące sto czterdzieści cztery"
15
+ assert engine.format_number(10000).text == "dziesięć tysięcy"
16
+ assert (
17
+ engine.format_number(83145).text
18
+ == "osiemdziesiąt trzy tysiące sto czterdzieści pięć"
19
+ )
20
+
21
+ assert engine.format_number(100000).text == "sto tysięcy"
22
+ assert (
23
+ engine.format_number(683146).text
24
+ == "sześćset osiemdziesiąt trzy tysiące sto czterdzieści sześć"
25
+ )
26
+
27
+ assert engine.format_number(1000000).text == "milion"
28
+ assert engine.format_number(10000000).text == "dziesięć milionów"
29
+ assert engine.format_number(100000000).text == "sto milionów"
30
+ assert engine.format_number(1000000000).text == "miliard"
31
+
32
+ # Special rules
33
+ assert engine.format_number(-1).text == "minus jeden"
34
+ assert engine.format_number(-14).text == "minus czternaście"
35
+ # assert engine.format_number(-14.6).text == "minus czternaście przecinek sześć"
36
+ # assert engine.format_number(-14.89).text == "minus czternaście przecinek osiemdziesiąt dziewięć"
37
+
38
+ # assert engine.format_number(float("nan")).text == "nie jest liczbą"
39
+ # assert engine.format_number(float("inf")).text == "nieskończona"
40
+
41
+ # Fractions
42
+ # assert engine.format_number(3.14).text == "trzy przecinek czternaście"
43
+ # assert engine.format_number("5.3").text == "pięć przecinek trzy"
44
+
45
+ # Years
46
+ assert (
47
+ engine.format_number(1999, FormatPurpose.YEAR).text
48
+ == "tysiąc dziewięćset dziewięćdziesiąt dziewięć"
49
+ )
@@ -3,4 +3,4 @@ from unicode_rbnf import RbnfEngine
3
3
 
4
4
  def test_romanian():
5
5
  engine = RbnfEngine.for_language("ro")
6
- assert engine.format_number(-100).text == "minus una sută"
6
+ assert engine.format_number(-100).text == "minus o sută"
@@ -0,0 +1,6 @@
1
+ from unicode_rbnf import RbnfEngine
2
+
3
+
4
+ def test_slovenian():
5
+ engine = RbnfEngine.for_language("sl")
6
+ assert engine.format_number(2000).text == "dva tisoč"
@@ -0,0 +1,36 @@
1
+ """Tests for Zulu."""
2
+
3
+ from unicode_rbnf import RbnfEngine
4
+
5
+
6
+ def test_zulu():
7
+ engine = RbnfEngine.for_language("zu")
8
+
9
+ assert engine.format_number(3).text == "kuthathu"
10
+ assert engine.format_number(15).text == "ishumi nanhlanu"
11
+ assert engine.format_number(21).text == "amashumi amabili nanye"
12
+ assert engine.format_number(47).text == "amashumi amane nesikhombisa"
13
+ assert engine.format_number(123).text == "ikhulu namashumi amabili nantathu"
14
+ assert (
15
+ engine.format_number(999).text
16
+ == "amakhulu ayisishiyagalolunye namashumi ayisishiyagalolunye nesishiyagalolunye"
17
+ )
18
+ assert engine.format_number(4000).text == "izinkulungwane ezine"
19
+ assert (
20
+ engine.format_number(8192).text
21
+ == "izinkulungwane eziyisishiyagalombili nekhulu namashumi ayisishiyagalolunye nambili"
22
+ )
23
+
24
+ assert (
25
+ engine.format_number(15123).text
26
+ == "izinkulungwane eziyishumi nanhlanu nekhulu namashumi amabili nantathu"
27
+ )
28
+
29
+ # # 3.5 million
30
+ # assert (
31
+ # engine.format_number(3500000).text
32
+ # == "izigidi ezintathu namakhulu ayishihlanu ezinkulungwane"
33
+ # )
34
+
35
+ # # 12 million
36
+ # assert engine.format_number(12000000).text == "izigidi eziyishumi ezimbili"
@@ -1,3 +1,8 @@
1
+ """Python implementation of Rule-Based Number Formatting (RBNF) engine from ICU.
2
+
3
+ Uses XML files from the CLDR: https://cldr.unicode.org
4
+ """
5
+
1
6
  import logging
2
7
  import re
3
8
  from abc import ABC
@@ -5,9 +10,9 @@ from bisect import bisect_left
5
10
  from dataclasses import dataclass, field
6
11
  from decimal import Decimal
7
12
  from enum import Enum, IntFlag, auto
8
- from math import ceil, floor, isinf, isnan, log, modf
13
+ from math import ceil, floor, isinf, isnan, log
9
14
  from pathlib import Path
10
- from typing import Dict, Final, Iterable, List, Optional, Union
15
+ from typing import Dict, Final, Iterable, List, Optional, Set, Tuple, Union
11
16
  from xml.etree import ElementTree as et
12
17
 
13
18
  DEFAULT_TOLERANCE: Final = 1e-8
@@ -16,6 +21,9 @@ SKIP_RULESETS: Final = {"lenient-parse"}
16
21
  _LANG_DIR = Path(__file__).parent / "rbnf"
17
22
  _LOGGER = logging.getLogger()
18
23
 
24
+ # Don't load these XML files
25
+ _EXCLUDED_XML_NAMES: Set[str] = {"root", "es_419", "en_001", "nb"}
26
+
19
27
 
20
28
  class FormatOptions(IntFlag):
21
29
  """Extra options for formatting."""
@@ -49,11 +57,21 @@ class FormatPurpose(Enum):
49
57
  return FormatPurpose.UNKNOWN
50
58
 
51
59
 
60
+ _DEFAULT_RULESETS: Dict[Tuple[str, FormatPurpose], str] = {
61
+ ("ko", FormatPurpose.CARDINAL): "spellout-cardinal-sinokorean",
62
+ }
63
+
64
+
52
65
  @dataclass
53
66
  class FormatResult:
67
+ """Result of formatting a number."""
68
+
54
69
  text: str
55
70
  """Formatted text from shortest ruleset name."""
56
71
 
72
+ text_ruleset: str
73
+ """Ruleset that was used for text."""
74
+
57
75
  text_by_ruleset: Dict[str, str]
58
76
  """Formatted text for each ruleset."""
59
77
 
@@ -63,17 +81,30 @@ class RbnfError(Exception):
63
81
 
64
82
 
65
83
  class RulesetNotFoundError(RbnfError):
66
- pass
84
+ """A requested ruleset was not found."""
67
85
 
68
86
 
69
87
  class NoRuleForNumberError(RbnfError):
70
- pass
88
+ """No matching rule could be found for a number."""
71
89
 
72
90
 
73
91
  class RbnfRulePart(ABC):
74
92
  """Abstract base class for rule parts."""
75
93
 
76
94
 
95
+ class ParseState(str, Enum):
96
+ """Set of rbnf parser."""
97
+
98
+ TEXT = "text"
99
+ SUB_OPTIONAL_BEFORE = "optional_before"
100
+ SUB_OPTIONAL_AFTER = "optional_after"
101
+ SUB_PLURAL_FORMAT = "plural_format"
102
+ SUB_REMAINDER = "remainder"
103
+ SUB_QUOTIENT = "quotient"
104
+ SUB_RULESET_NAME = "sub_ruleset_name"
105
+ REPLACE_RULESET_NAME = "replace_ruleset_name"
106
+
107
+
77
108
  @dataclass
78
109
  class TextRulePart(RbnfRulePart):
79
110
  """Literal text rule part."""
@@ -123,16 +154,66 @@ class ReplaceRulePart(RbnfRulePart):
123
154
  """Name of ruleset to use."""
124
155
 
125
156
 
126
- class ParseState(str, Enum):
127
- """Set of rbnf parser."""
157
+ @dataclass
158
+ class PluralFormatPart(RbnfRulePart):
159
+ """Function with other ruleset (keep value)."""
128
160
 
129
- TEXT = "text"
130
- SUB_OPTIONAL_BEFORE = "optional_before"
131
- SUB_OPTIONAL_AFTER = "optional_after"
132
- SUB_REMAINDER = "remainder"
133
- SUB_QUOTIENT = "quotient"
134
- SUB_RULESET_NAME = "sub_ruleset_name"
135
- REPLACE_RULESET_NAME = "replace_ruleset_name"
161
+ function_name: str = ""
162
+ """Function to use for substitution."""
163
+
164
+ function_value: str = ""
165
+ """Value to use for fucntion substitution."""
166
+
167
+ previous_state: Optional[ParseState] = None
168
+ """Previous state of parser."""
169
+
170
+ previous_part: Optional[RbnfRulePart] = None
171
+ """Previous part of parser."""
172
+
173
+ def render(self, number: Union[int, float, Decimal]) -> str:
174
+ """Render function with value."""
175
+ count_zero = self.function_value.count("0")
176
+ name_value = str(number)[:-count_zero]
177
+ value = Decimal(name_value[-1:])
178
+ value_many = Decimal(name_value[-2:])
179
+
180
+ clean_fn = self.function_name.replace("cardinal,", "").replace("ordinal,", "")
181
+
182
+ zero_match = re.search(r"zero\{(.*?)\}", clean_fn)
183
+ zero_value = zero_match.group(1) if zero_match else ""
184
+ if zero_match and zero_value and value == 0:
185
+ return zero_value
186
+
187
+ one_match = re.search(r"one\{(.*?)\}", clean_fn)
188
+ one_value = one_match.group(1) if one_match else ""
189
+ if one_match and one_value and value == 1:
190
+ return one_value
191
+
192
+ two_match = re.search(r"two\{(.*?)\}", clean_fn)
193
+ two_value = two_match.group(1) if two_match else ""
194
+ if two_match and two_value and value == 2:
195
+ return two_value
196
+
197
+ few_match = re.search(r"few\{(.*?)\}", clean_fn)
198
+ few_value = few_match.group(1) if few_match else ""
199
+ if few_match and few_value and value in [2, 3, 4]:
200
+ return few_value
201
+
202
+ many_match = re.search(r"many\{(.*?)\}", clean_fn)
203
+ many_value = many_match.group(1) if many_match else ""
204
+ if (
205
+ many_match
206
+ and few_value
207
+ and value_many in [11, 12, 13, 14, 15, 16, 17, 18, 19]
208
+ ):
209
+ return few_value
210
+
211
+ other_match = re.search(r"other\{(.*?)\}", clean_fn)
212
+ other_value = other_match.group(1) if other_match else ""
213
+ if other_match and other_value:
214
+ return other_value
215
+
216
+ return many_value
136
217
 
137
218
 
138
219
  class RbnfSpecialRule(str, Enum):
@@ -189,8 +270,13 @@ class RbnfRule:
189
270
  part: Optional[RbnfRulePart] = None
190
271
  is_sub_optional = False
191
272
  sub_text_before = ""
273
+ skip_next_char = False
274
+
275
+ for x, c in enumerate(text):
276
+ if skip_next_char:
277
+ skip_next_char = False
278
+ continue
192
279
 
193
- for c in text:
194
280
  if c == ";":
195
281
  # End of rule text
196
282
  break
@@ -199,10 +285,24 @@ class RbnfRule:
199
285
  # Placeholder
200
286
  continue
201
287
 
288
+ next_c: Optional[str] = None
289
+ if (x + 1) < len(text):
290
+ next_c = text[x + 1]
291
+
202
292
  if c in (">", "→"):
203
293
  # Divide the number by the rule's divisor and format the remainder
204
294
  if state in {ParseState.TEXT, ParseState.SUB_OPTIONAL_BEFORE}:
205
295
  state = ParseState.SUB_REMAINDER
296
+
297
+ if rule.parts and isinstance(rule.parts[-1], TextRulePart):
298
+ # Shift whitespace before arrow here.
299
+ # This is so it separates words in fractional mode.
300
+ prev_text_part: TextRulePart = rule.parts[-1]
301
+ prev_text_stripped = prev_text_part.text.rstrip()
302
+ prev_whitespace = prev_text_part.text[len(prev_text_stripped) :]
303
+ prev_text_part.text = prev_text_stripped
304
+ sub_text_before += prev_whitespace
305
+
206
306
  part = SubRulePart(
207
307
  SubType.REMAINDER,
208
308
  is_optional=is_sub_optional,
@@ -210,7 +310,14 @@ class RbnfRule:
210
310
  )
211
311
  rule.parts.append(part)
212
312
  sub_text_before = ""
313
+
213
314
  elif state in {ParseState.SUB_REMAINDER, ParseState.SUB_RULESET_NAME}:
315
+ if next_c in (">", "→"):
316
+ # Ignore final arrow in triple arrow.
317
+ # It means to render digit-by-digit, which we already do
318
+ # in "fractional" mode.
319
+ skip_next_char = True
320
+
214
321
  if is_sub_optional:
215
322
  state = ParseState.SUB_OPTIONAL_AFTER
216
323
  else:
@@ -245,6 +352,24 @@ class RbnfRule:
245
352
  pass
246
353
  else:
247
354
  raise ValueError(f"Got {c} in {state}")
355
+ elif c == "$":
356
+ # $(cardinal,plural syntax)$, $(ordinal,plural syntax)$
357
+ if text[x + 1] == "(":
358
+ _previous_state = state
359
+ _previous_part = part
360
+ state = ParseState.SUB_PLURAL_FORMAT
361
+ part = PluralFormatPart()
362
+ part.function_value = value_str
363
+ part.previous_state = _previous_state
364
+ part.previous_part = _previous_part
365
+ elif state == ParseState.SUB_PLURAL_FORMAT and text[x - 1] == ")":
366
+ assert isinstance(part, PluralFormatPart)
367
+ assert part.previous_state is not None
368
+ rule.parts.append(part)
369
+ state = part.previous_state
370
+ part = part.previous_part
371
+ else:
372
+ raise ValueError(f"Got {c} in {state} fot text: {text} (x: {x})")
248
373
  elif c == "[":
249
374
  # [optional] (start)
250
375
  if state == ParseState.TEXT:
@@ -279,6 +404,10 @@ class RbnfRule:
279
404
  # [... after]
280
405
  assert isinstance(part, SubRulePart)
281
406
  part.text_after += c
407
+ elif state == ParseState.SUB_PLURAL_FORMAT:
408
+ assert isinstance(part, PluralFormatPart)
409
+ if c not in ["(", ")"]:
410
+ part.function_name += c
282
411
  elif state == ParseState.SUB_RULESET_NAME:
283
412
  # %ruleset_name in << or >>
284
413
  assert isinstance(part, SubRulePart)
@@ -326,6 +455,9 @@ class RbnfRuleSet:
326
455
  special_rules: Dict[RbnfSpecialRule, RbnfRule] = field(default_factory=dict)
327
456
  """Rules keyed by special rule type."""
328
457
 
458
+ is_private: bool = False
459
+ """True if ruleset is private."""
460
+
329
461
  _sorted_numbers: Optional[List[int]] = field(default=None)
330
462
  """Sorted list of numeric_rules keys (updated on demand)."""
331
463
 
@@ -424,7 +556,13 @@ class RbnfEngine:
424
556
  @staticmethod
425
557
  def get_supported_languages() -> List[str]:
426
558
  """Return a list of supported language codes."""
427
- return sorted([f.stem for f in _LANG_DIR.glob("*.xml")])
559
+ return sorted(
560
+ [
561
+ f.stem
562
+ for f in _LANG_DIR.glob("*.xml")
563
+ if f.stem not in _EXCLUDED_XML_NAMES
564
+ ]
565
+ )
428
566
 
429
567
  @staticmethod
430
568
  def for_language(language: str) -> "RbnfEngine":
@@ -446,12 +584,13 @@ class RbnfEngine:
446
584
  rule_text: str,
447
585
  ruleset_name: str,
448
586
  radix: int = 10,
587
+ is_private: bool = False,
449
588
  ) -> Optional[RbnfRule]:
450
589
  """Manually add a rule to the engine."""
451
590
  assert ruleset_name is not None
452
591
  ruleset = self.rulesets.get(ruleset_name)
453
592
  if ruleset is None:
454
- ruleset = RbnfRuleSet(name=ruleset_name)
593
+ ruleset = RbnfRuleSet(name=ruleset_name, is_private=is_private)
455
594
  self.rulesets[ruleset_name] = ruleset
456
595
 
457
596
  rule = RbnfRule.parse(value_str, rule_text, radix=radix)
@@ -483,6 +622,8 @@ class RbnfEngine:
483
622
  _LOGGER.debug("Skipping ruleset: %s", ruleset_name)
484
623
  continue
485
624
 
625
+ is_private = group_elem.attrib.get("access") == "private"
626
+
486
627
  for rule_elem in group_elem.findall("rbnfrule"):
487
628
  if not rule_elem.text:
488
629
  continue
@@ -495,6 +636,7 @@ class RbnfEngine:
495
636
  rule_elem.text,
496
637
  ruleset_name,
497
638
  radix=radix,
639
+ is_private=is_private,
498
640
  )
499
641
 
500
642
  def format_number(
@@ -513,10 +655,11 @@ class RbnfEngine:
513
655
  if ruleset_names is None:
514
656
  # Gather all rulesets that fit the formatting purpose
515
657
  ruleset_names = [
516
- r
517
- for r in self.rulesets
518
- if FormatPurpose.from_ruleset_name(r) == purpose
519
- and ("verbose" not in r)
658
+ r_name
659
+ for r_name, r in self.rulesets.items()
660
+ if (not r.is_private)
661
+ and (FormatPurpose.from_ruleset_name(r_name) == purpose)
662
+ and ("verbose" not in r_name)
520
663
  ]
521
664
 
522
665
  if not ruleset_names:
@@ -545,10 +688,23 @@ class RbnfEngine:
545
688
  except (NoRuleForNumberError, RulesetNotFoundError):
546
689
  pass # skip ruleset
547
690
 
548
- shortest_ruleset = sorted(ruleset_names, key=len)[0]
691
+ if not number_strs:
692
+ raise NoRuleForNumberError(f"No rules were successful for {number}")
693
+
694
+ # Put spellout-numbering at the end
695
+ default_ruleset = _DEFAULT_RULESETS.get((self.language, purpose))
696
+ if (not default_ruleset) or (default_ruleset not in number_strs):
697
+ default_ruleset = "spellout-numbering"
698
+
699
+ if default_ruleset not in number_strs:
700
+ # Use ruleset with shortest length.
701
+ # Silly, but works most of the time.
702
+ default_ruleset = sorted(number_strs, key=len)[0]
549
703
 
550
704
  return FormatResult(
551
- text=number_strs[shortest_ruleset], text_by_ruleset=number_strs
705
+ text=number_strs[default_ruleset],
706
+ text_ruleset=default_ruleset,
707
+ text_by_ruleset=number_strs,
552
708
  )
553
709
 
554
710
  def iter_format_number(
@@ -575,35 +731,43 @@ class RbnfEngine:
575
731
 
576
732
  q: int = 0
577
733
  r: int = 0
734
+ r_digits: Optional[str] = None
578
735
 
579
736
  if isinstance(rule.value, RbnfSpecialRule):
580
737
  if rule.value == RbnfSpecialRule.NEGATIVE_NUMBER:
581
738
  r = int(-number)
582
739
  elif rule.value == RbnfSpecialRule.IMPROPER_FRACTION:
583
- frac_part, int_part = modf(number)
584
- q = int(int_part)
585
- r = fractional_to_int(frac_part * 10, tolerance=tolerance)
740
+ dec_num = Decimal(str(number))
741
+ q_str, r_digits = str(dec_num).split(".", maxsplit=1)
742
+ q = int(q_str)
586
743
  elif rule.value in {RbnfSpecialRule.NOT_A_NUMBER, RbnfSpecialRule.INFINITY}:
587
744
  # Should just be text substitutions
588
745
  pass
589
746
  else:
590
747
  _LOGGER.warning("Unhandled special rule: %s", rule.value)
591
748
  elif rule.value > 0:
749
+ if len(str(rule.value)) > 1 and str(rule.value)[0] != "1":
750
+ rule.value = int("1" + str(rule.value)[1:])
592
751
  power_below = rule.radix ** int(floor(log(rule.value, rule.radix)))
593
752
  power_above = rule.radix ** int(ceil(log(rule.value, rule.radix)))
594
753
  divisor = power_above if (number >= power_above) else power_below
595
- q, r = divmod(number, divisor)
754
+ q, r = divmod(int(number), divisor)
596
755
 
597
756
  for part in rule.parts:
598
757
  if isinstance(part, TextRulePart):
599
758
  if part.text:
600
759
  yield part.text
760
+ elif isinstance(part, PluralFormatPart):
761
+ if part.function_name:
762
+ yield part.render(number)
601
763
  elif isinstance(part, SubRulePart):
602
764
  sub_part: SubRulePart = part
603
765
 
604
766
  if part.type == SubType.QUOTIENT:
605
- if (q == 0) and (
606
- sub_part.is_optional or (part.ruleset_name is None)
767
+ if (
768
+ (q == 0)
769
+ and (sub_part.is_optional or (part.ruleset_name is None))
770
+ and (not r_digits)
607
771
  ):
608
772
  # Rulesets can use quotients of zero
609
773
  continue
@@ -618,19 +782,39 @@ class RbnfEngine:
618
782
  if part.text_after:
619
783
  yield part.text_after
620
784
  elif part.type == SubType.REMAINDER:
621
- if (r == 0) and (
622
- sub_part.is_optional or (part.ruleset_name is None)
785
+ if (
786
+ (r == 0)
787
+ and (sub_part.is_optional or (part.ruleset_name is None))
788
+ and (not r_digits)
623
789
  ):
624
790
  # Rulesets can use remainders of zero
625
791
  continue
626
792
 
793
+ if r_digits:
794
+ # Render digit-by-digit
795
+ for digit_str in r_digits:
796
+ digit = int(digit_str)
797
+
798
+ if part.text_before:
799
+ yield part.text_before
800
+ yield from self.iter_format_number(
801
+ digit,
802
+ ruleset_name=part.ruleset_name or ruleset_name,
803
+ tolerance=tolerance,
804
+ )
805
+ if part.text_after:
806
+ yield part.text_after
807
+ continue
808
+
627
809
  if part.text_before:
628
810
  yield part.text_before
811
+
629
812
  yield from self.iter_format_number(
630
813
  r,
631
814
  ruleset_name=part.ruleset_name or ruleset_name,
632
815
  tolerance=tolerance,
633
816
  )
817
+
634
818
  if part.text_after:
635
819
  yield part.text_after
636
820
  elif isinstance(part, ReplaceRulePart):