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
+small correction in RU
  • Loading branch information
SkiBY committed May 6, 2024
1 parent 5e6fa94 commit 033d371
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 111 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ Besides the numerical argument, there are two main optional arguments, ``to:`` a
* ``am`` (Amharic)
* ``ar`` (Arabic)
* ``az`` (Azerbaijani)
* ``by`` (Belarusian)
* ``be`` (Belarusian)
* ``ce`` (Chechen)
* ``cy`` (Welsh)
* ``cz`` (Czech)
Expand Down
4 changes: 2 additions & 2 deletions num2words/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

from __future__ import unicode_literals

from . import (lang_AM, lang_AR, lang_AZ, lang_BY, lang_CE, lang_CY, lang_CZ,
from . import (lang_AM, lang_AR, lang_AZ, lang_BE, lang_CE, lang_CY, lang_CZ,
lang_DE, lang_DK, lang_EN, lang_EN_IN, lang_EN_NG, lang_EO,
lang_ES, lang_ES_CO, lang_ES_CR, lang_ES_GT, lang_ES_NI,
lang_ES_VE, lang_FA, lang_FI, lang_FR, lang_FR_BE, lang_FR_CH,
Expand All @@ -31,7 +31,7 @@
'am': lang_AM.Num2Word_AM(),
'ar': lang_AR.Num2Word_AR(),
'az': lang_AZ.Num2Word_AZ(),
'by': lang_BY.Num2Word_BY(),
'be': lang_BE.Num2Word_BE(),
'ce': lang_CE.Num2Word_CE(),
'cy': lang_CY.Num2Word_CY(),
'cz': lang_CZ.Num2Word_CZ(),
Expand Down
47 changes: 30 additions & 17 deletions num2words/lang_BY.py → 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 All @@ -120,7 +131,7 @@
}


class Num2Word_BY(Num2Word_Base):
class Num2Word_BE(Num2Word_Base):
CURRENCY_FORMS = {
"RUB": (
("расійскі рубель", "расійскія рублі", "расійскіх рублёў"),
Expand Down Expand Up @@ -204,6 +215,8 @@ def pluralize(self, n, forms):

def to_ordinal(self, number, gender="m"):
self.verify_ordinal(number)
if isinstance(gender, bool) and gender:
gender = "f"
outwords = self.to_cardinal(number, gender).split(" ")
lastword = outwords[-1].lower()
try:
Expand All @@ -223,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 All @@ -242,6 +253,8 @@ def to_ordinal(self, number, gender="m"):

elif lastword[-1] == "н" or lastword[-2] == "н":
lastword = lastword[: lastword.rfind("н") + 1] + "ны"
elif lastword[-3:] == "наў":
lastword = lastword[: lastword.rfind("н") + 1] + "ны"
elif lastword[-1] == "д" or lastword[-2] == "д":
lastword = lastword[: lastword.rfind("д") + 1] + "ны"

Expand All @@ -254,9 +267,7 @@ def to_ordinal(self, number, gender="m"):
lastword = lastword[:-1] + "ая"

if gender == "n":
if lastword[-2:] in [
"ці", "ца"
]:
if lastword[-2:] in ["ці", "ца"]:
lastword = lastword[:-2] + "цяе"
else:
lastword = lastword[:-1] + "ае"
Expand All @@ -266,16 +277,20 @@ 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]
if len(outwords) > 1 and (
(any(x[0] in outwords[-1] for x in THOUSANDS.values()))
or "тысяч" in 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)]

elif len(outwords) > 1 and "тысяч" in outwords[-1]:
outwords[-2] = outwords[-2] + outwords[-1]
del outwords[-1]
return " ".join(outwords).strip()

def _money_verbose(self, number, currency):
Expand All @@ -294,8 +309,6 @@ def _cents_verbose(self, number, currency):
return self._int2word(number, gender)

def _int2word(self, n, gender="m"):
if isinstance(gender, bool) and gender:
gender = "f"
if n < 0:
return " ".join([self.negword, self._int2word(abs(n), gender)])

Expand Down
2 changes: 1 addition & 1 deletion num2words/lang_EU.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class Num2Word_EU(Num2Word_Base):

CURRENCY_ADJECTIVES = {
'AUD': 'Australian',
'BYN': 'Belarussian',
'BYN': 'Belarusian',
'CAD': 'Canadian',
'EEK': 'Estonian',
'USD': 'US',
Expand Down
8 changes: 3 additions & 5 deletions num2words/lang_RU.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,8 @@ def _int2word(self, n, feminine=False, cardinal=True, case=D_CASE,
'p' - prepositional
plural: True - plural
False - singular
gender: 'f' - masculine
'm' - feminine
gender: 'f' - feminine
'm' - masculine
'n' - neuter
animate: True - animate
False - inanimate
Expand Down Expand Up @@ -484,10 +484,8 @@ def __chunk_ordinal_join(self, hundreds, tens, ones, chunk_num, **kwargs):
if tens == 1:
words.append(get_num_element(TENS, ones, case='g'))
elif ones > 0:
if chunk_num == 0:
w_ones = get_num_element(ONES_ORD, ones, **kwargs)
# тысячный, миллионнный и т.д., двадцатиодномиллионный
elif chunk_num > 0 and ones == 1 and tens != 1:
if chunk_num > 0 and ones == 1 and tens != 1:
if tens == 0 and hundreds == 0:
w_ones = None
else:
Expand Down

0 comments on commit 033d371

Please sign in to comment.