Merge pull request #330 from gs202/master

Fix Hebrew pluralize and implement to_currency
This commit is contained in:
Marlon Rodriguez Garcia
2022-08-11 10:08:31 -04:00
committed by GitHub
2 changed files with 61 additions and 9 deletions

View File

@@ -81,11 +81,7 @@ AND = u'ו'
def pluralize(n, forms): def pluralize(n, forms):
# gettext implementation: form = 1 if n == 0 else 0 if n == 1 else 1
# (n%10==1 && n%100!=11 ? 0 : n != 0 ? 1 : 2)
form = 0 if (n % 10 == 1 and n % 100 != 11) else 1 if n != 0 else 2
return forms[form] return forms[form]
@@ -140,17 +136,30 @@ def n2w(n):
return int2word(int(n)) return int2word(int(n))
def to_currency(n, currency='EUR', cents=True, separator=','):
raise NotImplementedError()
class Num2Word_HE(Num2Word_Base): class Num2Word_HE(Num2Word_Base):
CURRENCY_FORMS = {
'NIS': (('שקל', 'שקלים'), ('אגורה', 'אגורות')),
'EUR': (('אירו', 'אירו'), ('סנט', 'סנט')),
'USD': (('דולר', 'דולרים'), ('סנט', 'סנט')),
}
def to_cardinal(self, number): def to_cardinal(self, number):
return n2w(number) return n2w(number)
def to_ordinal(self, number): def to_ordinal(self, number):
raise NotImplementedError() raise NotImplementedError()
def pluralize(self, n, forms):
return pluralize(n, forms)
def to_currency(self, val, currency='NIS', cents=True, separator=' ו',
adjective=False):
result = super(Num2Word_HE, self).to_currency(
val, currency=currency, cents=cents, separator=separator,
adjective=adjective)
# In Hebrew the separator is along with the following word
return result.replace(" ו ", " ו")
if __name__ == '__main__': if __name__ == '__main__':
yo = Num2Word_HE() yo = Num2Word_HE()

View File

@@ -20,6 +20,7 @@ from __future__ import unicode_literals
from unittest import TestCase from unittest import TestCase
from num2words import num2words from num2words import num2words
from num2words.lang_HE import Num2Word_HE
class Num2WordsHETest(TestCase): class Num2WordsHETest(TestCase):
@@ -71,3 +72,45 @@ class Num2WordsHETest(TestCase):
self.assertEqual( self.assertEqual(
num2words(6870, lang="he"), u'ששת אלפים שמונה מאות ושבעים' num2words(6870, lang="he"), u'ששת אלפים שמונה מאות ושבעים'
) )
def test_pluralize(self):
n = Num2Word_HE()
cr1, cr2 = n.CURRENCY_FORMS['NIS']
self.assertEqual(n.pluralize(1, cr1), 'שקל')
self.assertEqual(n.pluralize(2, cr1), 'שקלים')
self.assertEqual(n.pluralize(1, cr2), 'אגורה')
self.assertEqual(n.pluralize(2, cr2), 'אגורות')
cr1, cr2 = n.CURRENCY_FORMS['USD']
self.assertEqual(n.pluralize(1, cr1), 'דולר')
self.assertEqual(n.pluralize(2, cr1), 'דולרים')
self.assertEqual(n.pluralize(1, cr2), 'סנט')
self.assertEqual(n.pluralize(2, cr2), 'סנט')
def test_to_currency(self):
n = Num2Word_HE()
self.assertEqual(
n.to_currency(20.0, currency='NIS'), 'עשרים שקלים ואפס אגורות'
)
self.assertEqual(
n.to_currency(100.0, currency='NIS'), 'מאה שקלים ואפס אגורות'
)
self.assertEqual(
n.to_currency(100.50, currency='NIS'), 'מאה שקלים וחמישים אגורות'
)
def test_to_cardinal(self):
n = Num2Word_HE()
self.assertEqual(n.to_cardinal(1500), u'אלף וחמש מאות')
class Num2WordsHETestNotImplementedMethofs(TestCase):
n = Num2Word_HE()
def test_to_ordinal(self):
with self.assertRaises(NotImplementedError):
self.n.to_ordinal('1')
def test_large_number(self):
with self.assertRaises(NotImplementedError):
num2words(2000000, lang="he")