bnspellnum 0.1.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.
- bnspellnum-0.1.0/.gitignore +35 -0
- bnspellnum-0.1.0/LICENSE +21 -0
- bnspellnum-0.1.0/PKG-INFO +61 -0
- bnspellnum-0.1.0/README.md +34 -0
- bnspellnum-0.1.0/pyproject.toml +42 -0
- bnspellnum-0.1.0/src/bnspellnum/__init__.py +34 -0
- bnspellnum-0.1.0/src/bnspellnum/_core.py +267 -0
- bnspellnum-0.1.0/src/bnspellnum/_tables.py +29 -0
- bnspellnum-0.1.0/src/bnspellnum/py.typed +0 -0
- bnspellnum-0.1.0/tests/test_python_specific.py +46 -0
- bnspellnum-0.1.0/tests/test_vectors.py +49 -0
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# Python
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
.venv/
|
|
5
|
+
*.egg-info/
|
|
6
|
+
dist/
|
|
7
|
+
build/
|
|
8
|
+
.pytest_cache/
|
|
9
|
+
|
|
10
|
+
# JS
|
|
11
|
+
node_modules/
|
|
12
|
+
packages/js/dist/
|
|
13
|
+
coverage/
|
|
14
|
+
|
|
15
|
+
# PHP
|
|
16
|
+
packages/php/vendor/
|
|
17
|
+
packages/php/composer.lock
|
|
18
|
+
.phpunit.result.cache
|
|
19
|
+
.phpunit.cache/
|
|
20
|
+
|
|
21
|
+
# C#
|
|
22
|
+
bin/
|
|
23
|
+
obj/
|
|
24
|
+
*.user
|
|
25
|
+
TestResults/
|
|
26
|
+
|
|
27
|
+
# C++
|
|
28
|
+
packages/cpp/build/
|
|
29
|
+
cmake-build-*/
|
|
30
|
+
|
|
31
|
+
# Editors / OS
|
|
32
|
+
.idea/
|
|
33
|
+
.vscode/
|
|
34
|
+
.DS_Store
|
|
35
|
+
plan.md
|
bnspellnum-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Ediphas Labs
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: bnspellnum
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Spell out numbers in Bangla: words, currency (taka/paisa), ordinals, years and dates. Zero dependencies.
|
|
5
|
+
Project-URL: Homepage, https://github.com/ediphasLabs/bnspellnum
|
|
6
|
+
Project-URL: Issues, https://github.com/ediphasLabs/bnspellnum/issues
|
|
7
|
+
Project-URL: Specification, https://github.com/ediphasLabs/bnspellnum/blob/main/spec/SPEC.md
|
|
8
|
+
Author: Ediphas Labs
|
|
9
|
+
License: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: bangla,bengali,i18n,nlp,number-to-words,numbers,taka
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
15
|
+
Classifier: Natural Language :: Bengali
|
|
16
|
+
Classifier: Operating System :: OS Independent
|
|
17
|
+
Classifier: Programming Language :: Python :: 3
|
|
18
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
19
|
+
Classifier: Topic :: Software Development :: Internationalization
|
|
20
|
+
Classifier: Topic :: Software Development :: Localization
|
|
21
|
+
Classifier: Topic :: Text Processing :: Linguistic
|
|
22
|
+
Classifier: Typing :: Typed
|
|
23
|
+
Requires-Python: >=3.10
|
|
24
|
+
Provides-Extra: test
|
|
25
|
+
Requires-Dist: pytest>=7; extra == 'test'
|
|
26
|
+
Description-Content-Type: text/markdown
|
|
27
|
+
|
|
28
|
+
# bnspellnum for Python
|
|
29
|
+
|
|
30
|
+
Spell out numbers in Bangla: words, taka/paisa, ordinals, years and dates.
|
|
31
|
+
Zero dependencies, Python ≥ 3.10. Implements the
|
|
32
|
+
[bnspellnum spec](https://github.com/ediphasLabs/bnspellnum/blob/main/spec/SPEC.md) `1.0.0`,
|
|
33
|
+
and gives the same output as the JavaScript, PHP, C# and C++ packages.
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
pip install bnspellnum
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
import bnspellnum as bn
|
|
41
|
+
|
|
42
|
+
bn.words(1250000) # 'বারো লাখ পঞ্চাশ হাজার'
|
|
43
|
+
bn.words(200, style="formal") # 'দুইশত'
|
|
44
|
+
bn.words("-3.25") # 'ঋণাত্মক তিন দশমিক দুই পাঁচ'
|
|
45
|
+
bn.currency("1250.50", cheque=True) # 'এক হাজার দুইশো পঞ্চাশ টাকা পঞ্চাশ পয়সা মাত্র'
|
|
46
|
+
bn.ordinal(2), bn.ordinal(2, short=True) # ('দ্বিতীয়', '২য়')
|
|
47
|
+
bn.year(1971) # 'উনিশশো একাত্তর'
|
|
48
|
+
bn.date(16, 12, 1971) # 'ষোলোই ডিসেম্বর উনিশশো একাত্তর'
|
|
49
|
+
bn.date(28, 11, style="short") # '২৮শে নভেম্বর'
|
|
50
|
+
bn.to_bn_digits("2026") # '২০২৬'
|
|
51
|
+
bn.to_en_digits("১২,৫০,০০০") # '12,50,000'
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
- Accepts `int`, `str` (ASCII or Bangla digits, `,` separators, one `.`) and
|
|
55
|
+
`decimal.Decimal`. `float` raises `InvalidInputError`; pass a string instead.
|
|
56
|
+
- Errors: `InvalidInputError` and `OutOfRangeError`, both subclasses of
|
|
57
|
+
`BnSpellNumError` (itself a `ValueError`).
|
|
58
|
+
- Fully typed (`py.typed`).
|
|
59
|
+
|
|
60
|
+
Full documentation, options and spelling decisions are in the
|
|
61
|
+
[project README](https://github.com/ediphasLabs/bnspellnum#readme). MIT © Ediphas Labs.
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# bnspellnum for Python
|
|
2
|
+
|
|
3
|
+
Spell out numbers in Bangla: words, taka/paisa, ordinals, years and dates.
|
|
4
|
+
Zero dependencies, Python ≥ 3.10. Implements the
|
|
5
|
+
[bnspellnum spec](https://github.com/ediphasLabs/bnspellnum/blob/main/spec/SPEC.md) `1.0.0`,
|
|
6
|
+
and gives the same output as the JavaScript, PHP, C# and C++ packages.
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
pip install bnspellnum
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
```python
|
|
13
|
+
import bnspellnum as bn
|
|
14
|
+
|
|
15
|
+
bn.words(1250000) # 'বারো লাখ পঞ্চাশ হাজার'
|
|
16
|
+
bn.words(200, style="formal") # 'দুইশত'
|
|
17
|
+
bn.words("-3.25") # 'ঋণাত্মক তিন দশমিক দুই পাঁচ'
|
|
18
|
+
bn.currency("1250.50", cheque=True) # 'এক হাজার দুইশো পঞ্চাশ টাকা পঞ্চাশ পয়সা মাত্র'
|
|
19
|
+
bn.ordinal(2), bn.ordinal(2, short=True) # ('দ্বিতীয়', '২য়')
|
|
20
|
+
bn.year(1971) # 'উনিশশো একাত্তর'
|
|
21
|
+
bn.date(16, 12, 1971) # 'ষোলোই ডিসেম্বর উনিশশো একাত্তর'
|
|
22
|
+
bn.date(28, 11, style="short") # '২৮শে নভেম্বর'
|
|
23
|
+
bn.to_bn_digits("2026") # '২০২৬'
|
|
24
|
+
bn.to_en_digits("১২,৫০,০০০") # '12,50,000'
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
- Accepts `int`, `str` (ASCII or Bangla digits, `,` separators, one `.`) and
|
|
28
|
+
`decimal.Decimal`. `float` raises `InvalidInputError`; pass a string instead.
|
|
29
|
+
- Errors: `InvalidInputError` and `OutOfRangeError`, both subclasses of
|
|
30
|
+
`BnSpellNumError` (itself a `ValueError`).
|
|
31
|
+
- Fully typed (`py.typed`).
|
|
32
|
+
|
|
33
|
+
Full documentation, options and spelling decisions are in the
|
|
34
|
+
[project README](https://github.com/ediphasLabs/bnspellnum#readme). MIT © Ediphas Labs.
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling>=1.18"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "bnspellnum"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Spell out numbers in Bangla: words, currency (taka/paisa), ordinals, years and dates. Zero dependencies."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = { text = "MIT" }
|
|
11
|
+
authors = [{ name = "Ediphas Labs" }]
|
|
12
|
+
requires-python = ">=3.10"
|
|
13
|
+
keywords = ["bangla", "bengali", "numbers", "number-to-words", "taka", "i18n", "nlp"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Development Status :: 4 - Beta",
|
|
16
|
+
"Intended Audience :: Developers",
|
|
17
|
+
"License :: OSI Approved :: MIT License",
|
|
18
|
+
"Natural Language :: Bengali",
|
|
19
|
+
"Operating System :: OS Independent",
|
|
20
|
+
"Programming Language :: Python :: 3",
|
|
21
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
22
|
+
"Topic :: Software Development :: Internationalization",
|
|
23
|
+
"Topic :: Software Development :: Localization",
|
|
24
|
+
"Topic :: Text Processing :: Linguistic",
|
|
25
|
+
"Typing :: Typed",
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
[project.urls]
|
|
29
|
+
Homepage = "https://github.com/ediphasLabs/bnspellnum"
|
|
30
|
+
Issues = "https://github.com/ediphasLabs/bnspellnum/issues"
|
|
31
|
+
Specification = "https://github.com/ediphasLabs/bnspellnum/blob/main/spec/SPEC.md"
|
|
32
|
+
|
|
33
|
+
[project.optional-dependencies]
|
|
34
|
+
test = ["pytest>=7"]
|
|
35
|
+
|
|
36
|
+
[tool.hatch.build.targets.wheel]
|
|
37
|
+
packages = ["src/bnspellnum"]
|
|
38
|
+
|
|
39
|
+
[tool.pytest.ini_options]
|
|
40
|
+
testpaths = ["tests"]
|
|
41
|
+
addopts = "-q --doctest-modules --import-mode=importlib"
|
|
42
|
+
pythonpath = ["src"]
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"""Spell out numbers in Bangla.
|
|
2
|
+
|
|
3
|
+
>>> import bnspellnum
|
|
4
|
+
>>> bnspellnum.words(1250000)
|
|
5
|
+
'বারো লাখ পঞ্চাশ হাজার'
|
|
6
|
+
>>> bnspellnum.currency("1250.50")
|
|
7
|
+
'এক হাজার দুইশো পঞ্চাশ টাকা পঞ্চাশ পয়সা'
|
|
8
|
+
>>> bnspellnum.year(1971)
|
|
9
|
+
'উনিশশো একাত্তর'
|
|
10
|
+
>>> bnspellnum.date(28, 11)
|
|
11
|
+
'আঠাশে নভেম্বর'
|
|
12
|
+
>>> bnspellnum.to_bn_digits("2026")
|
|
13
|
+
'২০২৬'
|
|
14
|
+
"""
|
|
15
|
+
from ._core import (
|
|
16
|
+
BnSpellNumError,
|
|
17
|
+
InvalidInputError,
|
|
18
|
+
OutOfRangeError,
|
|
19
|
+
currency,
|
|
20
|
+
date,
|
|
21
|
+
ordinal,
|
|
22
|
+
to_bn_digits,
|
|
23
|
+
to_en_digits,
|
|
24
|
+
words,
|
|
25
|
+
year,
|
|
26
|
+
)
|
|
27
|
+
from ._tables import SPEC_VERSION
|
|
28
|
+
|
|
29
|
+
__version__ = "0.1.0"
|
|
30
|
+
|
|
31
|
+
__all__ = [
|
|
32
|
+
"BnSpellNumError", "InvalidInputError", "OutOfRangeError", "SPEC_VERSION", "__version__",
|
|
33
|
+
"currency", "date", "ordinal", "to_bn_digits", "to_en_digits", "words", "year",
|
|
34
|
+
]
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
"""bnspellnum core. A direct port of spec/SPEC.md; keep it structurally parallel
|
|
2
|
+
with the other language ports."""
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import re
|
|
6
|
+
from decimal import Decimal
|
|
7
|
+
from typing import Optional, Tuple, Union
|
|
8
|
+
|
|
9
|
+
from . import _tables as T
|
|
10
|
+
|
|
11
|
+
Number = Union[int, str, Decimal]
|
|
12
|
+
|
|
13
|
+
__all__ = [
|
|
14
|
+
"BnSpellNumError", "InvalidInputError", "OutOfRangeError",
|
|
15
|
+
"words", "ordinal", "year", "date", "currency", "to_bn_digits", "to_en_digits",
|
|
16
|
+
]
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class BnSpellNumError(ValueError):
|
|
20
|
+
"""Base class for every error raised by bnspellnum."""
|
|
21
|
+
|
|
22
|
+
code = "Error"
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class InvalidInputError(BnSpellNumError):
|
|
26
|
+
"""The input is malformed (bad type, bad string, unknown option...)."""
|
|
27
|
+
|
|
28
|
+
code = "InvalidInput"
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class OutOfRangeError(BnSpellNumError):
|
|
32
|
+
"""The input is well-formed but outside the allowed range."""
|
|
33
|
+
|
|
34
|
+
code = "OutOfRange"
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
# --------------------------------------------------------------------------- #
|
|
38
|
+
# Helpers
|
|
39
|
+
# --------------------------------------------------------------------------- #
|
|
40
|
+
_NUMBER_RE = re.compile(r"-?[0-9]+(?:,[0-9]+)*(?:\.[0-9]+)?")
|
|
41
|
+
_WS = " \t\n\r\f\v"
|
|
42
|
+
_BN_TO_EN = {ord(bn): en for bn, en in zip(T.BN_DIGITS, "0123456789")}
|
|
43
|
+
_EN_TO_BN = {ord(en): bn for en, bn in zip("0123456789", T.BN_DIGITS)}
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def _parse_number(value: object) -> Tuple[bool, str, Optional[str]]:
|
|
47
|
+
"""Return (negative, int_digits, frac_digits) per SPEC §2."""
|
|
48
|
+
if isinstance(value, bool):
|
|
49
|
+
raise InvalidInputError("booleans are not numbers")
|
|
50
|
+
if isinstance(value, int):
|
|
51
|
+
s = str(value)
|
|
52
|
+
elif isinstance(value, Decimal):
|
|
53
|
+
if not value.is_finite():
|
|
54
|
+
raise InvalidInputError(f"non-finite Decimal: {value}")
|
|
55
|
+
s = format(value, "f")
|
|
56
|
+
elif isinstance(value, str):
|
|
57
|
+
s = value.strip(_WS).translate(_BN_TO_EN)
|
|
58
|
+
elif isinstance(value, float):
|
|
59
|
+
raise InvalidInputError("floats are not accepted; pass a str or Decimal for exact values")
|
|
60
|
+
else:
|
|
61
|
+
raise InvalidInputError(f"unsupported input type: {type(value).__name__}")
|
|
62
|
+
|
|
63
|
+
if not _NUMBER_RE.fullmatch(s):
|
|
64
|
+
raise InvalidInputError(f"not a valid number: {value!r}")
|
|
65
|
+
negative = s.startswith("-")
|
|
66
|
+
if negative:
|
|
67
|
+
s = s[1:]
|
|
68
|
+
s = s.replace(",", "")
|
|
69
|
+
int_part, _, frac = s.partition(".")
|
|
70
|
+
int_part = int_part.lstrip("0") or "0"
|
|
71
|
+
frac_digits = frac if "." in s else None
|
|
72
|
+
if int_part == "0" and (frac_digits is None or frac_digits.strip("0") == ""):
|
|
73
|
+
negative = False
|
|
74
|
+
return negative, int_part, frac_digits
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def _parse_integer(value: object) -> Tuple[bool, str]:
|
|
78
|
+
negative, digits, frac = _parse_number(value)
|
|
79
|
+
if frac is not None:
|
|
80
|
+
raise InvalidInputError(f"expected an integer: {value!r}")
|
|
81
|
+
return negative, digits
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def _small_int(digits: str) -> int:
|
|
85
|
+
"""Convert a digit string we only need to compare against small bounds."""
|
|
86
|
+
return int(digits) if len(digits) <= 9 else 10**9
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def _option(name: str, value: str, allowed: Tuple[str, ...]) -> str:
|
|
90
|
+
if value not in allowed:
|
|
91
|
+
raise InvalidInputError(f"{name} must be one of {allowed}, got {value!r}")
|
|
92
|
+
return value
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def _cardinal(d: str, table: Tuple[str, ...], hundred: str) -> str:
|
|
96
|
+
"""SPEC §4. ``d`` is a digit string with no leading zeros."""
|
|
97
|
+
if len(d) <= 2:
|
|
98
|
+
return table[int(d)]
|
|
99
|
+
parts = []
|
|
100
|
+
if len(d) > 7:
|
|
101
|
+
parts += [_cardinal(d[:-7], table, hundred), T.CRORE]
|
|
102
|
+
d = d[-7:]
|
|
103
|
+
r = int(d)
|
|
104
|
+
if r >= 100000:
|
|
105
|
+
parts += [table[r // 100000], T.LAKH]
|
|
106
|
+
r %= 100000
|
|
107
|
+
if r >= 1000:
|
|
108
|
+
parts += [table[r // 1000], T.THOUSAND]
|
|
109
|
+
r %= 1000
|
|
110
|
+
if r >= 100:
|
|
111
|
+
parts.append(table[r // 100] + hundred)
|
|
112
|
+
r %= 100
|
|
113
|
+
if r > 0:
|
|
114
|
+
parts.append(table[r])
|
|
115
|
+
return " ".join(parts)
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def _style_tables(style: str) -> Tuple[Tuple[str, ...], str]:
|
|
119
|
+
_option("style", style, ("colloquial", "formal"))
|
|
120
|
+
if style == "formal":
|
|
121
|
+
return T.NUMBERS_FORMAL, T.HUNDRED_FORMAL
|
|
122
|
+
return T.NUMBERS, T.HUNDRED_COLLOQUIAL
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def _increment(d: str) -> str:
|
|
126
|
+
"""Add one to a non-negative digit string."""
|
|
127
|
+
digits = list(d)
|
|
128
|
+
i = len(digits) - 1
|
|
129
|
+
while i >= 0:
|
|
130
|
+
if digits[i] == "9":
|
|
131
|
+
digits[i] = "0"
|
|
132
|
+
i -= 1
|
|
133
|
+
else:
|
|
134
|
+
digits[i] = chr(ord(digits[i]) + 1)
|
|
135
|
+
return "".join(digits)
|
|
136
|
+
return "1" + "".join(digits)
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
# --------------------------------------------------------------------------- #
|
|
140
|
+
# Public API
|
|
141
|
+
# --------------------------------------------------------------------------- #
|
|
142
|
+
def words(n: Number, style: str = "colloquial", negative_style: str = "formal") -> str:
|
|
143
|
+
"""Spell a number (integer or decimal) in Bangla words.
|
|
144
|
+
|
|
145
|
+
>>> words(1250000)
|
|
146
|
+
'বারো লাখ পঞ্চাশ হাজার'
|
|
147
|
+
>>> words("3.25")
|
|
148
|
+
'তিন দশমিক দুই পাঁচ'
|
|
149
|
+
"""
|
|
150
|
+
table, hundred = _style_tables(style)
|
|
151
|
+
_option("negative_style", negative_style, ("formal", "colloquial"))
|
|
152
|
+
negative, digits, frac = _parse_number(n)
|
|
153
|
+
out = _cardinal(digits, table, hundred)
|
|
154
|
+
if frac is not None:
|
|
155
|
+
out += " " + T.POINT + " " + " ".join(table[int(c)] for c in frac)
|
|
156
|
+
if negative:
|
|
157
|
+
neg = T.NEGATIVE_FORMAL if negative_style == "formal" else T.NEGATIVE_COLLOQUIAL
|
|
158
|
+
out = neg + " " + out
|
|
159
|
+
return out
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def ordinal(n: Union[int, str], short: bool = False) -> str:
|
|
163
|
+
"""Ordinal form: প্রথম, দ্বিতীয় ... এগারোতম. ``short=True`` gives ১ম, ২য় ... ১১তম."""
|
|
164
|
+
negative, digits = _parse_integer(n)
|
|
165
|
+
if negative or digits == "0":
|
|
166
|
+
raise OutOfRangeError(f"ordinal needs n >= 1, got {n!r}")
|
|
167
|
+
k = _small_int(digits)
|
|
168
|
+
if k <= 10:
|
|
169
|
+
return (T.ORDINALS_SHORT if short else T.ORDINALS)[k - 1]
|
|
170
|
+
if short:
|
|
171
|
+
return to_bn_digits(digits) + T.ORDINAL_SUFFIX
|
|
172
|
+
return _cardinal(digits, T.NUMBERS, T.HUNDRED_COLLOQUIAL) + T.ORDINAL_SUFFIX
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
def year(n: Union[int, str], mode: str = "auto") -> str:
|
|
176
|
+
"""Read a year the way it is spoken: 1971 → উনিশশো একাত্তর, 2026 → দুই হাজার ছাব্বিশ."""
|
|
177
|
+
_option("mode", mode, ("auto", "cardinal"))
|
|
178
|
+
negative, digits = _parse_integer(n)
|
|
179
|
+
if negative:
|
|
180
|
+
raise OutOfRangeError(f"year must not be negative, got {n!r}")
|
|
181
|
+
return _year(digits, mode)
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
def _year(digits: str, mode: str) -> str:
|
|
185
|
+
if mode == "auto" and len(digits) == 4 and digits[0] == "1" and digits[1] != "0":
|
|
186
|
+
hi, lo = int(digits[:2]), int(digits[2:])
|
|
187
|
+
out = T.NUMBERS[hi] + T.HUNDRED_COLLOQUIAL
|
|
188
|
+
return out + " " + T.NUMBERS[lo] if lo > 0 else out
|
|
189
|
+
return _cardinal(digits, T.NUMBERS, T.HUNDRED_COLLOQUIAL)
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
def date(day: Union[int, str], month: Union[int, str],
|
|
193
|
+
year: Optional[Union[int, str]] = None, style: str = "spoken") -> str:
|
|
194
|
+
"""Bangla date: date(28, 11) → আঠাশে নভেম্বর; style="short" → ২৮শে নভেম্বর."""
|
|
195
|
+
_option("style", style, ("spoken", "short"))
|
|
196
|
+
year_ = year
|
|
197
|
+
d_neg, d_digits = _parse_integer(day)
|
|
198
|
+
m_neg, m_digits = _parse_integer(month)
|
|
199
|
+
m = -1 if m_neg else _small_int(m_digits)
|
|
200
|
+
if not 1 <= m <= 12:
|
|
201
|
+
raise OutOfRangeError(f"month must be 1-12, got {month!r}")
|
|
202
|
+
y_digits = None
|
|
203
|
+
if year_ is not None:
|
|
204
|
+
y_neg, y_digits = _parse_integer(year_)
|
|
205
|
+
if y_neg or y_digits == "0":
|
|
206
|
+
raise OutOfRangeError(f"year must be >= 1, got {year_!r}")
|
|
207
|
+
d = -1 if d_neg else _small_int(d_digits)
|
|
208
|
+
max_day = T.MONTH_DAYS[m - 1]
|
|
209
|
+
if m == 2 and y_digits is not None:
|
|
210
|
+
y = int(y_digits[-4:])
|
|
211
|
+
if not (y % 4 == 0 and (y % 100 != 0 or y % 400 == 0)):
|
|
212
|
+
max_day = 28
|
|
213
|
+
if not 1 <= d <= max_day:
|
|
214
|
+
raise OutOfRangeError(f"day {day!r} is not valid for month {m}")
|
|
215
|
+
|
|
216
|
+
if style == "short":
|
|
217
|
+
out = to_bn_digits(str(d)) + T.DAY_SUFFIX_SHORT[d]
|
|
218
|
+
elif d <= 4:
|
|
219
|
+
out = T.SPOKEN_DAYS[d]
|
|
220
|
+
else:
|
|
221
|
+
out = T.NUMBERS[d] + T.DAY_SUFFIX_SPOKEN[d]
|
|
222
|
+
out += " " + T.MONTHS[m - 1]
|
|
223
|
+
if y_digits is not None:
|
|
224
|
+
out += " " + (to_bn_digits(y_digits) if style == "short" else _year(y_digits, "auto"))
|
|
225
|
+
return out
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
def currency(amount: Number, cheque: bool = False, style: str = "colloquial") -> str:
|
|
229
|
+
"""Taka and paisa: currency("1250.50") → এক হাজার দুইশো পঞ্চাশ টাকা পঞ্চাশ পয়সা."""
|
|
230
|
+
table, hundred = _style_tables(style)
|
|
231
|
+
negative, taka, frac = _parse_number(amount)
|
|
232
|
+
if negative:
|
|
233
|
+
raise OutOfRangeError(f"currency amount must not be negative, got {amount!r}")
|
|
234
|
+
f = (frac or "").ljust(2, "0")
|
|
235
|
+
paisa = int(f[:2])
|
|
236
|
+
if len(f) > 2 and f[2] >= "5":
|
|
237
|
+
paisa += 1
|
|
238
|
+
if paisa == 100:
|
|
239
|
+
paisa = 0
|
|
240
|
+
taka = _increment(taka)
|
|
241
|
+
if taka != "0" or paisa == 0:
|
|
242
|
+
out = _cardinal(taka, table, hundred) + " " + T.TAKA
|
|
243
|
+
if paisa > 0:
|
|
244
|
+
out += " " + table[paisa] + " " + T.PAISA
|
|
245
|
+
else:
|
|
246
|
+
out = table[paisa] + " " + T.PAISA
|
|
247
|
+
if cheque:
|
|
248
|
+
out += " " + T.CHEQUE_SUFFIX
|
|
249
|
+
return out
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
def to_bn_digits(s: Union[str, int]) -> str:
|
|
253
|
+
"""Replace ASCII digits 0-9 with Bangla digits ০-৯; everything else is untouched."""
|
|
254
|
+
return _as_text(s).translate(_EN_TO_BN)
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
def to_en_digits(s: Union[str, int]) -> str:
|
|
258
|
+
"""Replace Bangla digits ০-৯ with ASCII digits 0-9; everything else is untouched."""
|
|
259
|
+
return _as_text(s).translate(_BN_TO_EN)
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
def _as_text(s: object) -> str:
|
|
263
|
+
if isinstance(s, str):
|
|
264
|
+
return s
|
|
265
|
+
if isinstance(s, int) and not isinstance(s, bool):
|
|
266
|
+
return str(s)
|
|
267
|
+
raise InvalidInputError(f"expected str or int, got {type(s).__name__}")
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# AUTO-GENERATED from spec/data by codegen/generate.py — do not edit.
|
|
2
|
+
# fmt: off
|
|
3
|
+
from typing import Tuple
|
|
4
|
+
|
|
5
|
+
SPEC_VERSION = "1.0.0"
|
|
6
|
+
|
|
7
|
+
NUMBERS: Tuple[str, ...] = ("শূন্য", "এক", "দুই", "তিন", "চার", "পাঁচ", "ছয়", "সাত", "আট", "নয়", "দশ", "এগারো", "বারো", "তেরো", "চৌদ্দ", "পনেরো", "ষোলো", "সতেরো", "আঠারো", "উনিশ", "বিশ", "একুশ", "বাইশ", "তেইশ", "চব্বিশ", "পঁচিশ", "ছাব্বিশ", "সাতাশ", "আঠাশ", "উনত্রিশ", "ত্রিশ", "একত্রিশ", "বত্রিশ", "তেত্রিশ", "চৌত্রিশ", "পঁয়ত্রিশ", "ছত্রিশ", "সাঁইত্রিশ", "আটত্রিশ", "উনচল্লিশ", "চল্লিশ", "একচল্লিশ", "বিয়াল্লিশ", "তেতাল্লিশ", "চুয়াল্লিশ", "পঁয়তাল্লিশ", "ছেচল্লিশ", "সাতচল্লিশ", "আটচল্লিশ", "উনপঞ্চাশ", "পঞ্চাশ", "একান্ন", "বাহান্ন", "তিপ্পান্ন", "চুয়ান্ন", "পঞ্চান্ন", "ছাপ্পান্ন", "সাতান্ন", "আটান্ন", "উনষাট", "ষাট", "একষট্টি", "বাষট্টি", "তেষট্টি", "চৌষট্টি", "পঁয়ষট্টি", "ছেষট্টি", "সাতষট্টি", "আটষট্টি", "উনসত্তর", "সত্তর", "একাত্তর", "বাহাত্তর", "তিয়াত্তর", "চুয়াত্তর", "পঁচাত্তর", "ছিয়াত্তর", "সাতাত্তর", "আটাত্তর", "উনআশি", "আশি", "একাশি", "বিরাশি", "তিরাশি", "চুরাশি", "পঁচাশি", "ছিয়াশি", "সাতাশি", "অষ্টআশি", "উননব্বই", "নব্বই", "একানব্বই", "বিরানব্বই", "তিরানব্বই", "চুরানব্বই", "পঁচানব্বই", "ছিয়ানব্বই", "সাতানব্বই", "আটানব্বই", "নিরানব্বই",)
|
|
8
|
+
NUMBERS_FORMAL: Tuple[str, ...] = ("শূন্য", "এক", "দুই", "তিন", "চার", "পাঁচ", "ছয়", "সাত", "আট", "নয়", "দশ", "এগারো", "বারো", "তেরো", "চৌদ্দ", "পনেরো", "ষোলো", "সতেরো", "আঠারো", "উনিশ", "বিশ", "একুশ", "বাইশ", "তেইশ", "চব্বিশ", "পঁচিশ", "ছাব্বিশ", "সাতাশ", "আঠাশ", "উনত্রিশ", "ত্রিশ", "একত্রিশ", "বত্রিশ", "তেত্রিশ", "চৌত্রিশ", "পঁয়ত্রিশ", "ছত্রিশ", "সাঁইত্রিশ", "আটত্রিশ", "উনচল্লিশ", "চল্লিশ", "একচল্লিশ", "বিয়াল্লিশ", "তেতাল্লিশ", "চুয়াল্লিশ", "পঁয়তাল্লিশ", "ছেচল্লিশ", "সাতচল্লিশ", "আটচল্লিশ", "উনপঞ্চাশ", "পঞ্চাশ", "একান্ন", "বাহান্ন", "তিপ্পান্ন", "চুয়ান্ন", "পঞ্চান্ন", "ছাপ্পান্ন", "সাতান্ন", "আটান্ন", "উনষাট", "ষাট", "একষট্টি", "বাষট্টি", "তেষট্টি", "চৌষট্টি", "পঁয়ষট্টি", "ছেষট্টি", "সাতষট্টি", "আটষট্টি", "উনসত্তর", "সত্তর", "একাত্তর", "বাহাত্তর", "তিয়াত্তর", "চুয়াত্তর", "পঁচাত্তর", "ছিয়াত্তর", "সাতাত্তর", "আটাত্তর", "উনআশি", "আশি", "একাশি", "বিরাশি", "তিরাশি", "চুরাশি", "পঁচাশি", "ছিয়াশি", "সাতাশি", "অষ্টআশি", "উননব্বই", "নব্বই", "একানব্বই", "বিরানব্বই", "তিরানব্বই", "চুরানব্বই", "পঁচানব্বই", "ছিয়ানব্বই", "সাতানব্বই", "আটানব্বই", "নিরানব্বই",)
|
|
9
|
+
HUNDRED_COLLOQUIAL = "শো"
|
|
10
|
+
HUNDRED_FORMAL = "শত"
|
|
11
|
+
THOUSAND = "হাজার"
|
|
12
|
+
LAKH = "লাখ"
|
|
13
|
+
CRORE = "কোটি"
|
|
14
|
+
ZERO = "শূন্য"
|
|
15
|
+
POINT = "দশমিক"
|
|
16
|
+
NEGATIVE_FORMAL = "ঋণাত্মক"
|
|
17
|
+
NEGATIVE_COLLOQUIAL = "মাইনাস"
|
|
18
|
+
BN_DIGITS: Tuple[str, ...] = ("০", "১", "২", "৩", "৪", "৫", "৬", "৭", "৮", "৯",)
|
|
19
|
+
ORDINALS: Tuple[str, ...] = ("প্রথম", "দ্বিতীয়", "তৃতীয়", "চতুর্থ", "পঞ্চম", "ষষ্ঠ", "সপ্তম", "অষ্টম", "নবম", "দশম",)
|
|
20
|
+
ORDINALS_SHORT: Tuple[str, ...] = ("১ম", "২য়", "৩য়", "৪র্থ", "৫ম", "৬ষ্ঠ", "৭ম", "৮ম", "৯ম", "১০ম",)
|
|
21
|
+
ORDINAL_SUFFIX = "তম"
|
|
22
|
+
MONTHS: Tuple[str, ...] = ("জানুয়ারি", "ফেব্রুয়ারি", "মার্চ", "এপ্রিল", "মে", "জুন", "জুলাই", "আগস্ট", "সেপ্টেম্বর", "অক্টোবর", "নভেম্বর", "ডিসেম্বর",)
|
|
23
|
+
MONTH_DAYS: Tuple[int, ...] = (31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31,)
|
|
24
|
+
SPOKEN_DAYS: Tuple[str, ...] = ("", "পহেলা", "দোসরা", "তেসরা", "চৌঠা",)
|
|
25
|
+
DAY_SUFFIX_SHORT: Tuple[str, ...] = ("", "লা", "রা", "রা", "ঠা", "ই", "ই", "ই", "ই", "ই", "ই", "ই", "ই", "ই", "ই", "ই", "ই", "ই", "ই", "শে", "শে", "শে", "শে", "শে", "শে", "শে", "শে", "শে", "শে", "শে", "শে", "শে",)
|
|
26
|
+
DAY_SUFFIX_SPOKEN: Tuple[str, ...] = ("", "", "", "", "", "ই", "ই", "ই", "ই", "ই", "ই", "ই", "ই", "ই", "ই", "ই", "ই", "ই", "ই", "ে", "ে", "ে", "ে", "ে", "ে", "ে", "ে", "ে", "ে", "ে", "ে", "ে",)
|
|
27
|
+
TAKA = "টাকা"
|
|
28
|
+
PAISA = "পয়সা"
|
|
29
|
+
CHEQUE_SUFFIX = "মাত্র"
|
|
File without changes
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"""Python-only behaviour that the shared vectors cannot express."""
|
|
2
|
+
from decimal import Decimal
|
|
3
|
+
|
|
4
|
+
import pytest
|
|
5
|
+
|
|
6
|
+
import bnspellnum as b
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def test_decimal_inputs_are_exact():
|
|
10
|
+
assert b.currency(Decimal("1250.50")) == "এক হাজার দুইশো পঞ্চাশ টাকা পঞ্চাশ পয়সা"
|
|
11
|
+
assert b.words(Decimal("3.25")) == "তিন দশমিক দুই পাঁচ"
|
|
12
|
+
assert b.words(Decimal("1E+3")) == "এক হাজার"
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@pytest.mark.parametrize("bad", [1.5, 3.0, True, None, [1], Decimal("NaN"), Decimal("Infinity")])
|
|
16
|
+
def test_rejected_types(bad):
|
|
17
|
+
with pytest.raises(b.InvalidInputError):
|
|
18
|
+
b.words(bad)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def test_float_currency_rejected():
|
|
22
|
+
with pytest.raises(b.InvalidInputError):
|
|
23
|
+
b.currency(0.1 + 0.2)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def test_huge_python_int():
|
|
27
|
+
assert b.words(10**21) == "এক কোটি কোটি কোটি"
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def test_errors_share_a_base_and_are_value_errors():
|
|
31
|
+
for exc in (b.InvalidInputError, b.OutOfRangeError):
|
|
32
|
+
assert issubclass(exc, b.BnSpellNumError)
|
|
33
|
+
assert issubclass(exc, ValueError)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def test_digits_accept_int():
|
|
37
|
+
assert b.to_bn_digits(2026) == "২০২৬"
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def test_keyword_year_in_date():
|
|
41
|
+
assert b.date(16, 12, year=1971) == "ষোলোই ডিসেম্বর উনিশশো একাত্তর"
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def test_version():
|
|
45
|
+
assert b.SPEC_VERSION
|
|
46
|
+
assert b.__version__
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"""Run every shared conformance vector in spec/vectors against the Python port."""
|
|
2
|
+
import json
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
|
|
5
|
+
import pytest
|
|
6
|
+
|
|
7
|
+
import bnspellnum as b
|
|
8
|
+
|
|
9
|
+
VECTORS = Path(__file__).resolve().parents[3] / "spec" / "vectors"
|
|
10
|
+
ERRORS = {"InvalidInput": b.InvalidInputError, "OutOfRange": b.OutOfRangeError}
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def _cases():
|
|
14
|
+
for path in sorted(VECTORS.glob("*.json")):
|
|
15
|
+
data = json.loads(path.read_text(encoding="utf-8"))
|
|
16
|
+
for i, case in enumerate(data["cases"]):
|
|
17
|
+
fn = case.get("function", data["function"])
|
|
18
|
+
yield pytest.param(fn, case, id=f"{path.stem}[{i}]-{fn}({case['input']!r})")
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def call(fn, inp, opt):
|
|
22
|
+
if fn == "words":
|
|
23
|
+
return b.words(inp, **opt)
|
|
24
|
+
if fn == "ordinal":
|
|
25
|
+
return b.ordinal(inp, **opt)
|
|
26
|
+
if fn == "year":
|
|
27
|
+
return b.year(inp, **opt)
|
|
28
|
+
if fn == "date":
|
|
29
|
+
return b.date(*inp, **opt)
|
|
30
|
+
if fn == "currency":
|
|
31
|
+
return b.currency(inp, **opt)
|
|
32
|
+
if fn == "to_bn_digits":
|
|
33
|
+
return b.to_bn_digits(inp)
|
|
34
|
+
if fn == "to_en_digits":
|
|
35
|
+
return b.to_en_digits(inp)
|
|
36
|
+
raise AssertionError(f"unknown function {fn}")
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
@pytest.mark.parametrize("fn,case", list(_cases()))
|
|
40
|
+
def test_vector(fn, case):
|
|
41
|
+
if "error" in case:
|
|
42
|
+
with pytest.raises(ERRORS[case["error"]]):
|
|
43
|
+
call(fn, case["input"], case["options"])
|
|
44
|
+
else:
|
|
45
|
+
assert call(fn, case["input"], case["options"]) == case["expected"]
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def test_vectors_found():
|
|
49
|
+
assert len(list(_cases())) > 500
|