From a6cae07703045c251fc8f608aef55ba706896499 Mon Sep 17 00:00:00 2001 From: gshekler Date: Sat, 3 Oct 2020 15:02:49 +0300 Subject: [PATCH 1/6] fix pluralize --- num2words/lang_HE.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/num2words/lang_HE.py b/num2words/lang_HE.py index 34b2ec8..2b96dc1 100644 --- a/num2words/lang_HE.py +++ b/num2words/lang_HE.py @@ -81,11 +81,7 @@ AND = u'ו' def pluralize(n, forms): - # gettext implementation: - # (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 - + form = 1 if n == 0 else 0 if n == 1 else 1 return forms[form] @@ -151,6 +147,9 @@ class Num2Word_HE(Num2Word_Base): def to_ordinal(self, number): raise NotImplementedError() + def pluralize(self, n, forms): + return pluralize(n, forms) + if __name__ == '__main__': yo = Num2Word_HE() From 92a0915508514983a8f1388d6acf062f593ac8e4 Mon Sep 17 00:00:00 2001 From: gshekler Date: Sat, 3 Oct 2020 15:03:43 +0300 Subject: [PATCH 2/6] implement currency for HE --- num2words/lang_HE.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/num2words/lang_HE.py b/num2words/lang_HE.py index 2b96dc1..f987192 100644 --- a/num2words/lang_HE.py +++ b/num2words/lang_HE.py @@ -136,11 +136,13 @@ def n2w(n): return int2word(int(n)) -def to_currency(n, currency='EUR', cents=True, separator=','): - raise NotImplementedError() - - class Num2Word_HE(Num2Word_Base): + CURRENCY_FORMS = { + 'NIS': (('שקל', 'שקלים'), ('אגורה', 'אגורות')), + 'EUR': (('אירו', 'אירו'), ('סנט', 'סנט')), + 'USD': (('דולר', 'דולרים'), ('סנט', 'סנט')), + } + def to_cardinal(self, number): return n2w(number) @@ -150,6 +152,14 @@ class Num2Word_HE(Num2Word_Base): 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__': yo = Num2Word_HE() From 6ea1a3da713906117e0660bd5dd68a5cad392053 Mon Sep 17 00:00:00 2001 From: gshekler Date: Sat, 3 Oct 2020 15:03:57 +0300 Subject: [PATCH 3/6] add unit tests --- tests/test_he.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/tests/test_he.py b/tests/test_he.py index 206d7cc..5a17694 100644 --- a/tests/test_he.py +++ b/tests/test_he.py @@ -20,6 +20,7 @@ from __future__ import unicode_literals from unittest import TestCase from num2words import num2words +from num2words.lang_HE import Num2Word_HE class Num2WordsHETest(TestCase): @@ -71,3 +72,39 @@ class Num2WordsHETest(TestCase): self.assertEqual( 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_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") From c95fe6260b1a5fd3f40cb4ac5f10557315d1507f Mon Sep 17 00:00:00 2001 From: gshekler Date: Sat, 3 Oct 2020 15:14:21 +0300 Subject: [PATCH 4/6] typo --- tests/test_he.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_he.py b/tests/test_he.py index 5a17694..688bbf0 100644 --- a/tests/test_he.py +++ b/tests/test_he.py @@ -87,7 +87,7 @@ class Num2WordsHETest(TestCase): self.assertEqual(n.pluralize(1, cr2), 'סנט') self.assertEqual(n.pluralize(2, cr2), 'סנט') - def test_currency_(self): + 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'), 'מאה שקלים ואפס אגורות') From 4b13dfddb7dc60821d840dbdf85c0372ba96c1fd Mon Sep 17 00:00:00 2001 From: Gabriel Shekler <46564521+gs202@users.noreply.github.com> Date: Sat, 6 Aug 2022 13:51:57 +0300 Subject: [PATCH 5/6] fix flake issues --- tests/test_he.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/tests/test_he.py b/tests/test_he.py index 688bbf0..2ce6797 100644 --- a/tests/test_he.py +++ b/tests/test_he.py @@ -89,9 +89,15 @@ class Num2WordsHETest(TestCase): 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'), 'מאה שקלים וחמישים אגורות') + 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() From 55deaa9cbee39498baa9614abc33a7c89fcadc57 Mon Sep 17 00:00:00 2001 From: Gabriel Shekler <46564521+gs202@users.noreply.github.com> Date: Sat, 6 Aug 2022 13:53:14 +0300 Subject: [PATCH 6/6] typo --- tests/test_he.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_he.py b/tests/test_he.py index 2ce6797..e50a6a2 100644 --- a/tests/test_he.py +++ b/tests/test_he.py @@ -93,10 +93,10 @@ class Num2WordsHETest(TestCase): n.to_currency(20.0, currency='NIS'), 'עשרים שקלים ואפס אגורות' ) self.assertEqual( - (n.to_currency(100.0, currency='NIS'), 'מאה שקלים ואפס אגורות' + n.to_currency(100.0, currency='NIS'), 'מאה שקלים ואפס אגורות' ) self.assertEqual( - (n.to_currency(100.50, currency='NIS'), 'מאה שקלים וחמישים אגורות' + n.to_currency(100.50, currency='NIS'), 'מאה שקלים וחמישים אגורות' ) def test_to_cardinal(self):