savoirfairelinux / num2words

Modules to convert numbers to words. 42 --> forty-two
GNU Lesser General Public License v2.1
820 stars 497 forks source link

Arabic: num2words(..., lang="ar") can fail with IndexError or InvalidOperation (instead of OverflowError) #511

Closed Jeronymous closed 1 year ago

Jeronymous commented 1 year ago

Expected Behaviour

num2words can sometimes fail with OverflowError on big numbers, that's fine.

But sometimes for Arabic language, it fails with other errors that seem to be bugs.

Actual Behaviour / Steps to reproduce

Here are two input which fails, with 2 different errors (IndexError / InvalidOperation).

from num2words import num2words

num2words(55772955305950220940549447929, lang="ar")

num2words(5577295530595022094054944792999999, lang="ar")

The first error is IndexError:

File ~/.local/lib/python3.10/site-packages/num2words/__init__.py:98, in num2words(number, ordinal, lang, to, **kwargs)
#      95 if to not in CONVERTES_TYPES:
#      96     raise NotImplementedError()
# ---> 98 return getattr(converter, 'to_{}'.format(to))(number, **kwargs)

# File ~/.local/lib/python3.10/site-packages/num2words/lang_AR.py:352, in Num2Word_AR.to_cardinal(self, number)
#     350 self.arabicSuffixText = ""
#     351 self.arabicOnes = ARABIC_ONES
# --> 352 return minus + self.convert(value=abs(number)).strip()

# File ~/.local/lib/python3.10/site-packages/num2words/lang_AR.py:205, in Num2Word_AR.convert(self, value)
#     203 self.number = "{:.9f}".format(value)
#     204 self.number_to_arabic(self.arabicPrefixText, self.arabicSuffixText)
# --> 205 return self.convert_to_arabic()

# File ~/.local/lib/python3.10/site-packages/num2words/lang_AR.py:241, in Num2Word_AR.convert_to_arabic(self)
#     238 else:
#     239     if ret_val != "":
#     240         ret_val = "{} {}".format(
# --> 241             self.arabicAppendedGroup[group],
#     242             ret_val)
#     243     else:
#     244         ret_val = "{} {}".format(
#     245             self.arabicGroup[group], ret_val)

# IndexError: list index out of range

The second one is InvalidOperation:

# ile ~/.local/lib/python3.10/site-packages/num2words/__init__.py:98, in num2words(number, ordinal, lang, to, **kwargs)
#      95 if to not in CONVERTES_TYPES:
#      96     raise NotImplementedError()
# ---> 98 return getattr(converter, 'to_{}'.format(to))(number, **kwargs)

# File ~/.local/lib/python3.10/site-packages/num2words/lang_AR.py:352, in Num2Word_AR.to_cardinal(self, number)
#     350 self.arabicSuffixText = ""
#     351 self.arabicOnes = ARABIC_ONES
# --> 352 return minus + self.convert(value=abs(number)).strip()

# File ~/.local/lib/python3.10/site-packages/num2words/lang_AR.py:205, in Num2Word_AR.convert(self, value)
#     203 self.number = "{:.9f}".format(value)
#     204 self.number_to_arabic(self.arabicPrefixText, self.arabicSuffixText)
# --> 205 return self.convert_to_arabic()

# File ~/.local/lib/python3.10/site-packages/num2words/lang_AR.py:222, in Num2Word_AR.convert_to_arabic(self)
#     217 group = 0
#     219 while temp_number > Decimal(0):
#     221     number_to_process = int(
# --> 222         Decimal(str(temp_number)) % Decimal(str(1000)))
#     223     temp_number = int(Decimal(temp_number) / Decimal(1000))
#     225     group_description = \
#     226         self.process_arabic_group(number_to_process,
#     227                                   group,
#     228                                   Decimal(floor(temp_number)))

# InvalidOperation: [<class 'decimal.DivisionImpossible'>]