Merge branch 'master' into master

This commit is contained in:
kkonieczny
2019-01-18 14:55:41 -05:00
committed by GitHub

View File

@@ -1,165 +1,165 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# Copyright (c) 2003, Taro Ogawa. All Rights Reserved. # Copyright (c) 2003, Taro Ogawa. All Rights Reserved.
# Copyright (c) 2013, Savoir-faire Linux inc. All Rights Reserved. # Copyright (c) 2013, Savoir-faire Linux inc. All Rights Reserved.
# This library is free software; you can redistribute it and/or # This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public # modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either # License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version. # 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, # This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of # but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details. # Lesser General Public License for more details.
# You should have received a copy of the GNU Lesser General Public # You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software # License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301 USA # MA 02110-1301 USA
from __future__ import unicode_literals from __future__ import unicode_literals
from .base import Num2Word_Base import itertools
from .utils import get_digits, splitbyx
from .base import Num2Word_Base
ZERO = ('zero',) from .utils import get_digits, splitbyx
ONES = { ZERO = ('zero',)
1: ('jeden',),
2: ('dwa',), ONES = {
3: ('trzy',), 1: ('jeden',),
4: ('cztery',), 2: ('dwa',),
5: ('pięć',), 3: ('trzy',),
6: ('sześć',), 4: ('cztery',),
7: ('siedem',), 5: ('pięć',),
8: ('osiem',), 6: ('sześć',),
9: ('dziewięć',), 7: ('siedem',),
} 8: ('osiem',),
9: ('dziewięć',),
TENS = { }
0: ('dziesięć',),
1: ('jedenaście',), TENS = {
2: ('dwanaście',), 0: ('dziesięć',),
3: ('trzynaście',), 1: ('jedenaście',),
4: ('czternaście',), 2: ('dwanaście',),
5: ('piętnaście',), 3: ('trzynaście',),
6: ('szesnaście',), 4: ('czternaście',),
7: ('siedemnaście',), 5: ('piętnaście',),
8: ('osiemnaście',), 6: ('szesnaście',),
9: ('dziewiętnaście',), 7: ('siedemnaście',),
} 8: ('osiemnaście',),
9: ('dziewiętnaście',),
TWENTIES = { }
2: ('dwadzieścia',),
3: ('trzydzieści',), TWENTIES = {
4: ('czterdzieści',), 2: ('dwadzieścia',),
5: ('pięćdziesiąt',), 3: ('trzydzieści',),
6: ('sześćdziesiąt',), 4: ('czterdzieści',),
7: ('siedemdziesiąt',), 5: ('pięćdziesiąt',),
8: ('osiemdziesiąt',), 6: ('sześćdziesiąt',),
9: ('dziewięćdzisiąt',), 7: ('siedemdziesiąt',),
} 8: ('osiemdziesiąt',),
9: ('dziewięćdzisiąt',),
HUNDREDS = { }
1: ('sto',),
2: ('dwieście',), HUNDREDS = {
3: ('trzysta',), 1: ('sto',),
4: ('czterysta',), 2: ('dwieście',),
5: ('pięćset',), 3: ('trzysta',),
6: ('sześćset',), 4: ('czterysta',),
7: ('siedemset',), 5: ('pięćset',),
8: ('osiemset',), 6: ('sześćset',),
9: ('dziewięćset',), 7: ('siedemset',),
} 8: ('osiemset',),
9: ('dziewięćset',),
THOUSANDS = { }
1: ('tysiąc', 'tysiące', 'tysięcy'), # 10^3
2: ('milion', 'miliony', 'milionów'), # 10^6 THOUSANDS = {
3: ('miliard', 'miliardy', 'miliardów'), # 10^9 1: ('tysiąc', 'tysiące', 'tysięcy'), # 10^3
4: ('bilion', 'biliony', 'bilionów'), # 10^12 }
5: ('biliard', 'biliardy', 'biliardów'), # 10^15
6: ('trylion', 'tryliony', 'trylionów'), # 10^18 prefixes = ( # 10^(6*x)
7: ('tryliard', 'tryliardy', 'tryliardów'), # 10^21 "mi", # 10^6
8: ('kwadrylion', 'kwadryliony', 'kwadrylionów'), # 10^24 "bi", # 10^12
9: ('kwadryliard', 'kwadryliardy', 'kwadryliardów'), # 10^27 "try", # 10^18
10: ('kwintylion', 'kwintyliony', 'kwintylionów'), # 10^30 "kwadry", # 10^24
11: ('kwintyliard', 'kwintyliardy', 'kwintyliardów'), # 10^33 "kwinty", # 10^30
12: ('sekstylion', 'sekstyliony', 'sekstylionów'), # 10^36 "seksty", # 10^36
13: ('sekstyliard', 'sekstyliardy', 'sekstyliardów'), # 10^39 "septy", # 10^42
14: ('septylion', 'septyliony', 'septylionów'), # 10^42 "okty", # 10^48
15: ('septyliard', 'septyliardy', 'septyliardów'), # 10^45 "nony", # 10^54
16: ('oktylion', 'oktyliony', 'oktylionów'), # 10^48 "decy" # 10^60
17: ('oktyliard', 'oktyliardy', 'oktyliardów'), # 10^51 )
18: ('nonylion', 'nonyliony', 'nonylionów'), # 10^54 suffixes = ("lion", "liard") # 10^x or 10^(x+3)
19: ('nonyliard', 'nonyliardy', 'nonyliardów'), # 10^57
20: ('decylion', 'decyliony', 'decylionów'), # 10^60 for idx, (p, s) in enumerate(itertools.product(prefixes, suffixes)):
21: ('decyliard', 'decyliardy', 'decyliardów'), # 10^63 name = p + s
} THOUSANDS[idx+2] = (name, name + 'y', name + 'ów')
class Num2Word_PL(Num2Word_Base): class Num2Word_PL(Num2Word_Base):
CURRENCY_FORMS = { CURRENCY_FORMS = {
'PLN': ( 'PLN': (
('złoty', 'złote', 'złotych'), ('grosz', 'grosze', 'groszy') ('złoty', 'złote', 'złotych'), ('grosz', 'grosze', 'groszy')
), ),
'EUR': ( 'EUR': (
('euro', 'euro', 'euro'), ('cent', 'centy', 'centów') ('euro', 'euro', 'euro'), ('cent', 'centy', 'centów')
), ),
} }
def setup(self): def setup(self):
self.negword = "minus" self.negword = "minus"
self.pointword = "przecinek" self.pointword = "przecinek"
def to_cardinal(self, number): def to_cardinal(self, number):
n = str(number).replace(',', '.') n = str(number).replace(',', '.')
if '.' in n: if '.' in n:
left, right = n.split('.') left, right = n.split('.')
return u'%s %s %s' % ( return u'%s %s %s' % (
self._int2word(int(left)), self._int2word(int(left)),
self.pointword, self.pointword,
self._int2word(int(right)) self._int2word(int(right))
) )
else: else:
return self._int2word(int(n)) return self._int2word(int(n))
def pluralize(self, n, forms): def pluralize(self, n, forms):
if n == 1: if n == 1:
form = 0 form = 0
elif 5 > n % 10 > 1 and (n % 100 < 10 or n % 100 > 20): elif 5 > n % 10 > 1 and (n % 100 < 10 or n % 100 > 20):
form = 1 form = 1
else: else:
form = 2 form = 2
return forms[form] return forms[form]
def to_ordinal(self, number): def to_ordinal(self, number):
raise NotImplementedError() raise NotImplementedError()
def _int2word(self, n): def _int2word(self, n):
if n == 0: if n == 0:
return ZERO[0] return ZERO[0]
words = [] words = []
chunks = list(splitbyx(str(n), 3)) chunks = list(splitbyx(str(n), 3))
i = len(chunks) i = len(chunks)
for x in chunks: for x in chunks:
i -= 1 i -= 1
if x == 0: if x == 0:
continue continue
n1, n2, n3 = get_digits(x) n1, n2, n3 = get_digits(x)
if n3 > 0: if n3 > 0:
words.append(HUNDREDS[n3][0]) words.append(HUNDREDS[n3][0])
if n2 > 1: if n2 > 1:
words.append(TWENTIES[n2][0]) words.append(TWENTIES[n2][0])
if n2 == 1: if n2 == 1:
words.append(TENS[n1][0]) words.append(TENS[n1][0])
elif n1 > 0 and not (i > 0 and x == 1): elif n1 > 0 and not (i > 0 and x == 1):
words.append(ONES[n1][0]) words.append(ONES[n1][0])
if i > 0: if i > 0:
words.append(self.pluralize(x, THOUSANDS[i])) words.append(self.pluralize(x, THOUSANDS[i]))
return ' '.join(words) return ' '.join(words)