Skip to content

Commit

Permalink
Merge 6d11f14 into 4f11622
Browse files Browse the repository at this point in the history
  • Loading branch information
arielallon committed Oct 28, 2019
2 parents 4f11622 + 6d11f14 commit 44f060e
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 16 deletions.
39 changes: 24 additions & 15 deletions num2words/lang_HE.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,33 @@

from __future__ import print_function, unicode_literals

from .base import Num2Word_Base
from .utils import get_digits, splitbyx

ZERO = (u'אפס',)

ONES = {
1: (u'אחד',),
2: (u'שנים',),
1: (u'אחת',),
2: (u'שתים',),
3: (u'שלש',),
4: (u'ארבע',),
5: (u'חמש',),
6: (u'שש',),
7: (u'שבע',),
8: (u'שמנה',),
8: (u'שמונה',),
9: (u'תשע',),
}

TENS = {
0: (u'עשר',),
1: (u'אחד עשרה',),
2: (u'שנים עשרה',),
1: (u'אחת עשרה',),
2: (u'שתים עשרה',),
3: (u'שלש עשרה',),
4: (u'ארבע עשרה',),
5: (u'חמש עשרה',),
6: (u'שש עשרה',),
7: (u'שבע עשרה',),
8: (u'שמנה עשרה',),
8: (u'שמונה עשרה',),
9: (u'תשע עשרה',),
}

Expand All @@ -54,7 +55,7 @@
5: (u'חמישים',),
6: (u'ששים',),
7: (u'שבעים',),
8: (u'שמנים',),
8: (u'שמונים',),
9: (u'תשעים',),
}

Expand All @@ -67,6 +68,13 @@
THOUSANDS = {
1: (u'אלף',),
2: (u'אלפיים',),
3: (u'שלשת אלפים',),
4: (u'ארבעת אלפים',),
5: (u'חמשת אלפים',),
6: (u'ששת אלפים',),
7: (u'שבעת אלפים',),
8: (u'שמונת אלפים',),
9: (u'תשעת אלפים',),
}

AND = u'ו'
Expand Down Expand Up @@ -100,12 +108,15 @@ def int2word(n):

n1, n2, n3 = get_digits(x)

if i > 0:
words.append(THOUSANDS[n1][0])
continue

if n3 > 0:
if n3 <= 2:
words.append(HUNDRED[n3][0])
else:
words.append(ONES[n3][0])
words.append(HUNDRED[3][0])
words.append(ONES[n3][0] + ' ' + HUNDRED[3][0])

if n2 > 1:
words.append(TWENTIES[n2][0])
Expand All @@ -116,14 +127,12 @@ def int2word(n):
words.append(ONES[n1][0])

if i > 0:
if i <= 2:
words.append(THOUSANDS[i][0])
else:
words.append(ONES[i][0])
words.append(THOUSANDS[1][0])
words.append(THOUSANDS[i][0])

# source: https://hebrew-academy.org.il/2017/01/30/%d7%95-%d7%94%d7%97%d7%99%d7%91%d7%95%d7%a8-%d7%91%d7%9e%d7%a1%d7%a4%d7%a8%d7%99%d7%9d/
if len(words) > 1:
words[-1] = AND + words[-1]

return ' '.join(words)


Expand All @@ -135,7 +144,7 @@ def to_currency(n, currency='EUR', cents=True, separator=','):
raise NotImplementedError()


class Num2Word_HE(object):
class Num2Word_HE(Num2Word_Base):
def to_cardinal(self, number):
return n2w(number)

Expand Down
2 changes: 1 addition & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def test_cli_with_lang(self):
)

def test_cli_with_lang_to(self):
"""You should be able to specify a language
"""You should be able to specify a language and currency
"""
output = self.cli.run_cmd(150.55, '--lang', 'es', '--to', 'currency')
self.assertEqual(output.return_code, 0)
Expand Down
73 changes: 73 additions & 0 deletions tests/test_he.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2003, Taro Ogawa. All Rights Reserved.
# Copyright (c) 2013, Savoir-faire Linux inc. All Rights Reserved.

# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301 USA

from __future__ import unicode_literals

from unittest import TestCase

from num2words import num2words


class Num2WordsHETest(TestCase):
maxDiff = None

def test_0(self):
self.assertEqual(num2words(0, lang="he"), u'אפס')

def test_1_to_10(self):
self.assertEqual(num2words(1, lang="he"), u'אחת')
self.assertEqual(num2words(2, lang="he"), u'שתים')
self.assertEqual(num2words(7, lang="he"), u'שבע')
self.assertEqual(num2words(10, lang="he"), u'עשר')

def test_11_to_19(self):
self.assertEqual(num2words(11, lang="he"), u'אחת עשרה')
self.assertEqual(num2words(13, lang="he"), u'שלש עשרה')
self.assertEqual(num2words(15, lang="he"), u'חמש עשרה')
self.assertEqual(num2words(16, lang="he"), u'שש עשרה')
self.assertEqual(num2words(19, lang="he"), u'תשע עשרה')

def test_20_to_99(self):
self.assertEqual(num2words(20, lang="he"), u'עשרים')
self.assertEqual(num2words(23, lang="he"), u'עשרים ושלש')
self.assertEqual(num2words(28, lang="he"), u'עשרים ושמונה')
self.assertEqual(num2words(31, lang="he"), u'שלשים ואחת')
self.assertEqual(num2words(40, lang="he"), u'ארבעים')
self.assertEqual(num2words(66, lang="he"), u'ששים ושש')
self.assertEqual(num2words(92, lang="he"), u'תשעים ושתים')

def test_100_to_999(self):
self.assertEqual(num2words(100, lang="he"), u'מאה')
self.assertEqual(num2words(111, lang="he"), u'מאה ואחת עשרה')
self.assertEqual(num2words(150, lang="he"), u'מאה וחמישים')
self.assertEqual(num2words(196, lang="he"), u'מאה תשעים ושש')
self.assertEqual(num2words(200, lang="he"), u'מאתיים')
self.assertEqual(num2words(210, lang="he"), u'מאתיים ועשר')
self.assertEqual(num2words(701, lang="he"), u'שבע מאות ואחת')

def test_1000_to_9999(self):
self.assertEqual(num2words(1000, lang="he"), u'אלף')
self.assertEqual(num2words(1001, lang="he"), u'אלף ואחת')
self.assertEqual(num2words(1500, lang="he"), u'אלף וחמש מאות')
self.assertEqual(
num2words(7378, lang="he"), u'שבעת אלפים שלש מאות שבעים ושמונה'
)
self.assertEqual(num2words(2000, lang="he"), u'אלפיים')
self.assertEqual(num2words(2100, lang="he"), u'אלפיים ומאה')
self.assertEqual(
num2words(6870, lang="he"), u'ששת אלפים שמונה מאות ושבעים'
)

0 comments on commit 44f060e

Please sign in to comment.