Skip to content

Commit

Permalink
Simplify __chunk_ordinal_joined and make tests for backward compatibi…
Browse files Browse the repository at this point in the history
…lity (feminine=True)
  • Loading branch information
KhramtsovDR committed May 12, 2024
1 parent ba95e60 commit 87a77d9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
39 changes: 18 additions & 21 deletions num2words/lang_RU.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,11 @@ def setup(self):
self.pointword_ord = make_ord_cases("цел", 1)

def to_cardinal(self, number, case=D_CASE, plural=D_PLURAL,
gender=D_GENDER, animate=D_ANIMATE):
gender=D_GENDER, animate=D_ANIMATE, feminine=False):
# Backward compatibility
if feminine:
gender = 'f'

n = str(number).replace(',', '.')
if '.' in n:
left, right = n.split('.')
Expand Down Expand Up @@ -368,7 +372,11 @@ def pluralize(self, n, forms):
return forms[2]

def to_ordinal(self, number, case=D_CASE, plural=D_PLURAL, gender=D_GENDER,
animate=D_ANIMATE):
animate=D_ANIMATE, feminine=False):
# Backward compatibility
if feminine:
gender = 'f'

self.verify_ordinal(number)
n = str(number).replace(',', '.')
return self._int2word(int(n), cardinal=False, case=case, plural=plural,
Expand All @@ -384,12 +392,11 @@ def _cents_verbose(self, number, currency):
return self._int2word(number, gender='f')
return self._int2word(number, gender='m')

def _int2word(self, n, feminine=False, cardinal=True, case=D_CASE,
def _int2word(self, n, cardinal=True, case=D_CASE,
plural=D_PLURAL, gender=D_GENDER, animate=D_ANIMATE):
"""
Main function
:param n: number of items
:param feminine: not used (for backward compatibility)
:param cardinal: True(cardinal), False(ordinal)
:param case: 'n'(nominative), 'g'(genitive), 'd'(dative),
'a'(accusative),'i'(instrumental), 'p'(prepositional)
Expand All @@ -398,10 +405,6 @@ def _int2word(self, n, feminine=False, cardinal=True, case=D_CASE,
:param animate: True(animate), False(inanimate)
:return text
"""
# For backward compatibility
if feminine:
gender = 'f'

kwargs = {'case': case, 'plural': plural, 'gender': gender,
'animate': animate}

Expand Down Expand Up @@ -436,7 +439,7 @@ def _int2word(self, n, feminine=False, cardinal=True, case=D_CASE,
if i > 0:
chunk_words.append(
self.pluralize(x, get_thousands_elements(i, case)))
# ordinal, not joined like 'двухтысячный'
# ordinal, not joined (not like 'двухтысячный')
elif not (ord_join and rightest_nonzero_chunk_i == i):
chunk_words.extend(
self.__chunk_ordinal(n3, n2, n1, i, **kwargs)
Expand All @@ -447,14 +450,10 @@ def _int2word(self, n, feminine=False, cardinal=True, case=D_CASE,
self.pluralize(x, get_thousands_elements(i, t_case)))
# ordinal, joined
else:
chunk_words.extend(
self.__chunk_ordinal_join(n3, n2, n1, i, **kwargs)
chunk_words.append(
self.__chunk_ordinal_joined(n3, n2, n1, **kwargs) +
get_mldir_value(THOUSANDS_ORD, i, **kwargs)
)
if i > 0:
chunk_words.append(
get_mldir_value(THOUSANDS_ORD, i, **kwargs))

chunk_words = [''.join(chunk_words)]

words.extend(chunk_words)

Expand Down Expand Up @@ -517,7 +516,7 @@ def __chunk_ordinal(self, hundreds, tens, ones, chunk_num, **kwargs):

return words

def __chunk_ordinal_join(self, hundreds, tens, ones, chunk_num, **kwargs):
def __chunk_ordinal_joined(self, hundreds, tens, ones, **kwargs):
words = []

if hundreds > 1:
Expand All @@ -533,10 +532,8 @@ def __chunk_ordinal_join(self, hundreds, tens, ones, chunk_num, **kwargs):
if tens == 1:
words.append(get_mldir_value(TENS, ones, case='g'))
elif ones > 0:
if chunk_num == 0:
w_ones = get_mldir_value(ONES_ORD, ones, **kwargs)
# тысячный, миллионнный и т.д., двадцатиодномиллионный
elif chunk_num > 0 and ones == 1 and tens != 1:
if ones == 1 and tens != 1:
if tens == 0 and hundreds == 0:
w_ones = None
else:
Expand All @@ -547,4 +544,4 @@ def __chunk_ordinal_join(self, hundreds, tens, ones, chunk_num, **kwargs):
if w_ones:
words.append(w_ones)

return words
return ''.join(words)
10 changes: 10 additions & 0 deletions tests/test_ru.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,3 +519,13 @@ def test_to_currency(self):
'одна тысяча двести тридцать четыре польских злотых, '
'пятьдесят шесть грошей'
)

def test_backward_compatibility(self):
self.assertEqual(
num2words(1, lang='ru', feminine=True),
'одна'
)
self.assertEqual(
num2words(1, lang='ru', to='ordinal', feminine=True),
'первая'
)

0 comments on commit 87a77d9

Please sign in to comment.