From 182b82cf1ff47d588f50b1feb650e721626cbbd1 Mon Sep 17 00:00:00 2001 From: Noah Santacruz Date: Tue, 22 Nov 2016 10:23:15 -0500 Subject: [PATCH] Added Hebrew ordinal numbers #54 Squashed commit of the following: commit 88b946ef72928e859d078f3febaf9c76ce0849b9 Merge: bca0277 79ab811 Author: Noah Santacruz Date: Tue Nov 22 09:41:05 2016 +0200 merge commit bca0277424c074af217df5e86abfd2def3a30bc7 Author: Noah Santacruz Date: Tue Nov 22 09:40:03 2016 +0200 removed out.txt commit 79ab811e97fd14bc5899174b198e86f5c6ba2c5f Author: Noah Santacruz Date: Mon Nov 21 18:09:42 2016 +0200 Update README.rst commit 507e4d4cec5b5458b2546ebebe5e49d376b88646 Author: Noah Santacruz Date: Mon Nov 21 18:08:39 2016 +0200 updated init commit 7d3aa5ab33d92b0b374ed1bfbf17807836e465bf Author: Noah Santacruz Date: Mon Nov 21 16:05:06 2016 +0200 changed init commit 29b4c54047ff9ab84b4c95e9ff05ebcb12c15f49 Author: Noah Santacruz Date: Mon Nov 21 16:01:17 2016 +0200 added Hebrew --- README.rst | 1 + num2words/__init__.py | 2 + num2words/lang_HE.py | 162 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 165 insertions(+) create mode 100644 num2words/lang_HE.py diff --git a/README.rst b/README.rst index 1aed5bc..8e8894d 100644 --- a/README.rst +++ b/README.rst @@ -54,6 +54,7 @@ Besides the numerical argument, there's two optional arguments. * ``ru`` (Russian) * ``dk`` (Danish) * ``pt_BR`` (Brazilian Portuguese) +* ``he`` (Hebrew) You can supply values like ``fr_FR``, the code will be correctly interpreted. If you supply an unsupported language, ``NotImplementedError`` is raised. diff --git a/num2words/__init__.py b/num2words/__init__.py index 85d3328..6de57a2 100644 --- a/num2words/__init__.py +++ b/num2words/__init__.py @@ -31,6 +31,7 @@ from . import lang_ID from . import lang_NO from . import lang_DK from . import lang_PT_BR +from . import lang_HE CONVERTER_CLASSES = { 'en': lang_EN.Num2Word_EN(), @@ -48,6 +49,7 @@ CONVERTER_CLASSES = { 'no': lang_NO.Num2Word_NO(), 'dk': lang_DK.Num2Word_DK(), 'pt_BR': lang_PT_BR.Num2Word_PT_BR(), + 'he': lang_HE.Num2Word_HE() } def num2words(number, ordinal=False, lang='en'): diff --git a/num2words/lang_HE.py b/num2words/lang_HE.py new file mode 100644 index 0000000..73857dc --- /dev/null +++ b/num2words/lang_HE.py @@ -0,0 +1,162 @@ +# -*- encoding: 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 + +ZERO = (u'אפס',) + +ONES = { + 1: (u'אחד',), + 2: (u'שנים',), + 3: (u'שלש',), + 4: (u'ארבע',), + 5: (u'חמש',), + 6: (u'שש',), + 7: (u'שבע',), + 8: (u'שמנה',), + 9: (u'תשע',), +} + +TENS = { + 0: (u'עשר',), + 1: (u'אחד עשרה',), + 2: (u'שנים עשרה',), + 3: (u'שלש עשרה',), + 4: (u'ארבע עשרה',), + 5: (u'חמש עשרה',), + 6: (u'שש עשרה',), + 7: (u'שבע עשרה',), + 8: (u'שמנה עשרה',), + 9: (u'תשע עשרה',), +} + +TWENTIES = { + 2: (u'עשרים',), + 3: (u'שלשים',), + 4: (u'ארבעים',), + 5: (u'חמישים',), + 6: (u'ששים',), + 7: (u'שבעים',), + 8: (u'שמנים',), + 9: (u'תשעים',), +} + +HUNDRED = { + 1: (u'מאה',), + 2: (u'מאתיים',), + 3: (u'מאות',) +} + +THOUSANDS = { + 1: (u'אלף',), + 2: (u'אלפיים',), +} + +AND = u'ו' + +def splitby3(n): + length = len(n) + if length > 3: + start = length % 3 + if start > 0: + yield int(n[:start]) + for i in range(start, length, 3): + yield int(n[i:i+3]) + else: + yield int(n) + + +def get_digits(n): + return [int(x) for x in reversed(list(('%03d' % n)[-3:]))] + + +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 + + return forms[form] + + +def int2word(n): + if n > 9999: #doesn't yet work for numbers this big + raise NotImplementedError() + + if n == 0: + return ZERO[0] + + words = [] + + chunks = list(splitby3(str(n))) + i = len(chunks) + for x in chunks: + i -= 1 + n1, n2, n3 = get_digits(x) + + # print str(n3) + str(n2) + str(n1) + + if n3 > 0: + if n3 <= 2: + words.append(HUNDRED[n3][0]) + else: + words.append(ONES[n3][0]) + words.append(HUNDRED[3][0]) + + if n2 > 1: + words.append(TWENTIES[n2][0]) + + if n2 == 1: + words.append(TENS[n1][0]) + elif n1 > 0 and not (i > 0 and x == 1): + 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]) + + if len(words) > 1: + words[-1] = AND + words[-1] + return ' '.join(words) + + +def n2w(n): + return int2word(int(n)) + + +def to_currency(n, currency='EUR', cents=True, seperator=','): + raise NotImplementedError() + + +class Num2Word_HE(object): + def to_cardinal(self, number): + return n2w(number) + + def to_ordinal(self, number): + raise NotImplementedError() + + +if __name__ == '__main__': + yo = Num2Word_HE() + nums = [1, 11, 21, 24, 99, 100, 101, 200, 211, 345, 1000, 1011] + for num in nums: + print num, yo.to_cardinal(num) +