Skip to content

Commit

Permalink
[FIX] ISO code for Belarusian language is be, not by. + coverage
Browse files Browse the repository at this point in the history
+ improvements in big numbers
+reformat flake8
  • Loading branch information
SkiBY committed May 6, 2024
1 parent 5ba54c5 commit c0dad0b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 19 deletions.
41 changes: 22 additions & 19 deletions num2words/lang_BE.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,17 @@
9: "дзевяноста",
}

TWENTIES_ORD = (
("дваццаць", "дваццаці"),
("трыццаць", "трыццаці"),
("сорак", "сарака"),
("пяцьдзясят", "пяцідзясяці"),
("шэсцьдзясят", "шaсцідзясяці"),
("семдзесят", "сямідзесяці"),
("восемдзесят", "васьмідзесяці"),
("дзевяноста", "дзевяноста"),
)

HUNDREDS = {
1: "сто",
2: "дзвесце",
Expand Down Expand Up @@ -225,8 +236,6 @@ def to_ordinal(self, number, gender="m"):
lastword = (
self.ords_adjective.get(lastword[:-3], lastword) + "соты"
)
elif lastword[-5:] == "шэсць":
lastword = "шосты"
elif lastword[-7:] == "дзесяць":
lastword = "дзясяты"
elif lastword[-9:] == "семдзесят":
Expand Down Expand Up @@ -268,25 +277,19 @@ def to_ordinal(self, number, gender="m"):
outwords[-2] = outwords[-1]
del outwords[-1]

if len(outwords) > 2 and "тысяч" in outwords[-1]:
if "сорак" in outwords[-3]:
outwords[-3] = outwords[-3].replace("сорак", "сарака")
outwords[-3] = outwords[-3] + outwords[-2] + outwords[-1]
del outwords[-1]
del outwords[-1]

elif len(outwords) > 1 and (
any(x[0] in outwords[-1] for x in THOUSANDS.values())
if len(outwords) > 1 and (
(any(x[0] in outwords[-1] for x in THOUSANDS.values()))
or "тысяч" in outwords[-1]
):
if "трыццаць" in outwords[-2]:
outwords[-2] = outwords[-2].replace("трыццаць", "трыццаці")
outwords[-2] = outwords[-2] + outwords[-1]
del outwords[-1]

elif len(outwords) > 1 and "мільярд" in outwords[-1]:
outwords[-2] = outwords[-2] + outwords[-1]
del outwords[-1]
new_outwords = []
for _w in outwords:
replacement = next(
(x for x in TWENTIES_ORD if x[0] in _w), None
)
if replacement:
_w = _w.replace(replacement[0], replacement[1])
new_outwords.append(_w)
outwords = ["".join(new_outwords)]

return " ".join(outwords).strip()

Expand Down
13 changes: 13 additions & 0 deletions tests/test_be.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ def test_to_ordinal(self):
self.assertEqual(
num2words(42000, lang="be", to="ordinal"), "саракадвухтысячны"
)

self.assertEqual(
num2words(75000, lang="be", to="ordinal"), "сямідзесяціпяцітысячны"
)

self.assertEqual(
num2words(1000000, lang="be", to="ordinal"), "мільённы"
)
Expand All @@ -175,6 +180,14 @@ def test_to_ordinal(self):
self.assertEqual(
num2words(3000000000, lang="be", to="ordinal"), "трохмільярдны"
)
self.assertEqual(
num2words(43000000000, lang="be", to="ordinal"),
"саракатрохмільярдны",
)
self.assertEqual(
num2words(333000000000, lang="be", to="ordinal"),
"трыстатрыццацітрохмільярдны",
)

def test_to_currency(self):
self.assertEqual(
Expand Down

0 comments on commit c0dad0b

Please sign in to comment.