Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
BayMinimum committed Oct 19, 2018
2 parents 587f1de + ca61c77 commit 6dc21f4
Show file tree
Hide file tree
Showing 16 changed files with 652 additions and 153 deletions.
5 changes: 3 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,12 @@ Besides the numerical argument, there are two main optional arguments.
* ``no`` (Norwegian)
* ``pl`` (Polish)
* ``pt_BR`` (Portuguese - Brazilian)
* ``sl`` (Slovene)
* ``ro`` (Romanian)
* ``ru`` (Russian)
* ``sl`` (Slovene)
* ``tr`` (Turkish)
* ``th`` (Thai)
* ``vn`` (Vietnamese)
* ``vi`` (Vietnamese)
* ``nl`` (Dutch)
* ``uk`` (Ukrainian)

Expand Down
6 changes: 4 additions & 2 deletions num2words/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from . import lang_LT
from . import lang_LV
from . import lang_PL
from . import lang_RO
from . import lang_RU
from . import lang_ID
from . import lang_JA
Expand All @@ -40,7 +41,7 @@
from . import lang_IT
from . import lang_ES_VE
from . import lang_ES_CO
from . import lang_VN
from . import lang_VI
from . import lang_TR
from . import lang_NL
from . import lang_UK
Expand Down Expand Up @@ -68,14 +69,15 @@
'lt': lang_LT.Num2Word_LT(),
'lv': lang_LV.Num2Word_LV(),
'pl': lang_PL.Num2Word_PL(),
'ro': lang_RO.Num2Word_RO(),
'ru': lang_RU.Num2Word_RU(),
'sl': lang_SL.Num2Word_SL(),
'no': lang_NO.Num2Word_NO(),
'dk': lang_DK.Num2Word_DK(),
'pt_BR': lang_PT_BR.Num2Word_PT_BR(),
'he': lang_HE.Num2Word_HE(),
'it': lang_IT.Num2Word_IT(),
'vi_VN': lang_VN.Num2Word_VN(),
'vi': lang_VI.Num2Word_VI(),
'th': lang_TH.Num2Word_TH(),
'tr': lang_TR.Num2Word_TR(),
'nl': lang_NL.Num2Word_NL(),
Expand Down
6 changes: 3 additions & 3 deletions num2words/lang_DE.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ def setup(self):
"ert": "erts",
"end": "ends",
"ion": "ions",
"nen": "nens",
"rde": "rdes",
"rden": "rdens"}
"nen": "ns",
"rde": "rds",
"rden": "rds"}

def merge(self, curr, next):
ctext, cnum, ntext, nnum = curr + next
Expand Down
1 change: 1 addition & 0 deletions num2words/lang_ES.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Num2Word_ES(Num2Word_EU):
'EUR': (('euro', 'euros'), ('centimo', 'centimos')),
'ESP': (('peseta', 'pesetas'), ('centimo', 'centimos')),
'USD': (('dolar', 'dolares'), ('centavo', 'centavos')),
'PEN': (('sol', 'soles'), ('centimo', 'centimos')),
}

# //CHECK: Is this sufficient??
Expand Down
4 changes: 4 additions & 0 deletions num2words/lang_EU.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class Num2Word_EU(Num2Word_Base):
'SEK': (('krona', 'kronor'), ('öre', 'öre')),
'NOK': (('krone', 'kroner'), ('øre', 'øre')),
'PLN': (('zloty', 'zlotys', 'zlotu'), ('grosz', 'groszy')),
'MXN': (('peso', 'pesos'), GENERIC_CENTS),
'RON': (('leu', 'lei'), ('ban', 'bani')),
}

CURRENCY_ADJECTIVES = {
Expand All @@ -49,6 +51,8 @@ class Num2Word_EU(Num2Word_Base):
'USD': 'US',
'RUB': 'Russian',
'NOK': 'Norwegian',
'MXN': 'Mexican',
'RON': 'Romanian',
}

GIGA_SUFFIX = "illiard"
Expand Down
136 changes: 136 additions & 0 deletions num2words/lang_RO.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
# -*- 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 division, print_function, unicode_literals

from . import lang_EU


class Num2Word_RO(lang_EU.Num2Word_EU):
GIGA_SUFFIX = "iliard/e"
MEGA_SUFFIX = "ilion"
# inflection for million follows different rule
MEGA_SUFFIX_I = "ilioane"

def setup(self):
super(Num2Word_RO, self).setup()

self.negword = "minus "
self.pointword = "virgulă"
self.exclude_title = ["și", "virgulă", "minus"]
self.errmsg_toobig = (
"Numărul e prea mare pentru a fi convertit în cuvinte."
)
self.mid_numwords = [(1000, "mie/i"), (100, "sută/e"),
(90, "nouăzeci"), (80, "optzeci"),
(70, "șaptezeci"), (60, "șaizeci"),
(50, "cincizeci"), (40, "patruzeci"),
(30, "treizeci")]
self.low_numwords = ["douăzeci", "nouăsprezece", "optsprezece",
"șaptesprezece", "șaisprezece", "cincisprezece",
"paisprezece", "treisprezece", "doisprezece",
"unsprezece", "zece", "nouă", "opt", "șapte",
"șase", "cinci", "patru", "trei", "doi",
"unu", "zero"]
self.gen_numwords = ["", "o", "două", "trei", "patru", "cinci",
"șase", "șapte", "opt", "nouă"]
self.gen_numwords_m = ["", "un", "două", "trei", "patru", "cinci",
"șase", "șapte", "opt", "nouă"]
self.numwords_inflections = {
100: self.gen_numwords,
1000: self.gen_numwords,
1000000: self.gen_numwords_m,
1000000000: self.gen_numwords_m
}
self.ords = {"unu": "primul",
"doi": "al doilea",
"three": "al treilea",
"cinci": "al cincilea",
"opt": "al optulea",
"nouă": "al nouălea",
"doisprezece": "al doisprezecelea"}

def merge(self, lpair, rpair):
ltext, lnum = lpair
rtext, rnum = rpair
rtext_i = self.inflect(rnum, rtext)
if lnum > 1 and rtext_i.endswith(self.MEGA_SUFFIX):
rtext_i = rtext_i.replace(self.MEGA_SUFFIX, self.MEGA_SUFFIX_I)
if 1 <= lnum < 10:
if rnum not in self.numwords_inflections:
return (rtext, rnum)
else:
rtext_i = self.inflect(lnum * rnum, rtext)
lresult = (self.numwords_inflections[rnum][lnum], rtext_i)
return ("%s %s" % lresult, rnum)
elif 10 < lnum < 100:
if lnum % 10 == 0:
return ("%s și %s" % (ltext, rtext), lnum + rnum)
else:
return ("%s %s" % (ltext, rtext_i), lnum * rnum)
else:
if rnum in self.numwords_inflections:
rtext_i = self.inflect(lnum * rnum, rtext)
return ("%s %s" % (ltext, rtext_i), lnum * rnum)

def to_ordinal(self, value):
self.verify_ordinal(value)
if value == 1:
return "primul"
else:
value = self.to_cardinal(value)
return "al %slea" % (value)

def to_ordinal_num(self, value):
self.verify_ordinal(value)
if value == 1:
return "1-ul"
return "al %s-lea" % (value)

def inflect(self, value, text):
text = text.split("/")
if value in (1, 100, 1000, 100000, 1000000000):
return text[0]
if len(text) > 1 and text[0][-1] in "aăeiou":
text[0] = text[0][:-1]
return "".join(text)

def to_currency(self, val, longval=True, old=False):
cents = int(round(val*100))
result = self.to_splitnum(cents, hightxt="leu/i", lowtxt="ban/i",
divisor=100, jointxt="și", longval=longval)
return result.replace(
"unu leu", "un leu"
).replace("unu ban", "un ban")

def to_year(self, val, suffix=None, longval=True):
result = super(Num2Word_RO, self).to_year(
val,
longval=longval
)
# for years we want the era negation e.g. B.C., in our case
# it's î.Hr. or î.e.n
if result.startswith(self.negword):
result = result.replace(self.negword, "")
suffix = "î.Hr." if not suffix else suffix
if suffix:
result = "".join([
result,
" ",
suffix
])
return result
60 changes: 59 additions & 1 deletion num2words/lang_RU.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,35 @@ class Num2Word_RU(Num2Word_Base):
'EUR': (
('евро', 'евро', 'евро'), ('цент', 'цента', 'центов')
),
'USD': (
('доллар', 'доллара', 'долларов'), ('цент', 'цента', 'центов')
),
}

def setup(self):
self.negword = "минус"
self.pointword = "запятая"
self.ords = {"ноль": "нулевой",
"один": "первый",
"два": "второй",
"три": "третий",
"четыре": "четвертый",
"пять": "пятый",
"шесть": "шестой",
"семь": "седьмой",
"восемь": "восьмой",
"девять": "девятый",
"сто": "сотый"}
self.ords_feminine = {"один": "",
"одна": "",
"две": "двух",
"три": "трёх",
"четыре": "четырёх",
"пять": "пяти",
"шесть": "шести",
"семь": "семи",
"восемь": "восьми",
"девять": "девяти"}

def to_cardinal(self, number):
n = str(number).replace(',', '.')
Expand All @@ -134,7 +158,41 @@ def pluralize(self, n, forms):
return forms[form]

def to_ordinal(self, number):
raise NotImplementedError()
self.verify_ordinal(number)
outwords = self.to_cardinal(number).split(" ")
lastword = outwords[-1].lower()
try:
if len(outwords) > 1:
if outwords[-2] in self.ords_feminine:
outwords[-2] = self.ords_feminine.get(
outwords[-2], outwords[-2])
elif outwords[-2] == 'десять':
outwords[-2] = outwords[-2][:-1] + 'и'
if len(outwords) == 3:
if outwords[-3] in ['один', 'одна']:
outwords[-3] = ''
lastword = self.ords[lastword]
except KeyError:
if lastword[:-3] in self.ords_feminine:
lastword = self.ords_feminine.get(
lastword[:-3], lastword) + "сотый"
elif lastword[-1] == "ь" or lastword[-2] == "т":
lastword = lastword[:-1] + "ый"
elif lastword[-1] == "к":
lastword = lastword + "овой"
elif lastword[-5:] == "десят":
lastword = lastword.replace('ь', 'и') + 'ый'
elif lastword[-2] == "ч" or lastword[-1] == "ч":
if lastword[-2] == "ч":
lastword = lastword[:-1] + "ный"
if lastword[-1] == "ч":
lastword = lastword + "ный"
elif lastword[-1] == "н" or lastword[-2] == "н":
lastword = lastword[:lastword.rfind('н') + 1] + "ный"
elif lastword[-1] == "д" or lastword[-2] == "д":
lastword = lastword[:lastword.rfind('д') + 1] + "ный"
outwords[-1] = self.title(lastword)
return " ".join(outwords).strip()

def _cents_verbose(self, number, currency):
return self._int2word(number, currency == 'RUB')
Expand Down
2 changes: 1 addition & 1 deletion num2words/lang_VN.py → num2words/lang_VI.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
'Octodecillion', 'Novemdecillion', 'Vigintillion')


class Num2Word_VN(object):
class Num2Word_VI(object):

def _convert_nn(self, val):
if val < 20:
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,6 @@ def find_version(fname):
test_suite='tests',
classifiers=CLASSIFIERS,
scripts=['bin/num2words'],
install_requires=["docopt>=0.6.2"]
install_requires=["docopt>=0.6.2"],
tests_require=['delegator.py'],
)
4 changes: 2 additions & 2 deletions tests/test_de.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ def test_ordinal_at_crucial_number(self):
num2words(4000, ordinal=True, lang='de'), "viertausendste"
)
self.assertEqual(
num2words(2000000, ordinal=True, lang='de'), "zwei millionenste"
num2words(2000000, ordinal=True, lang='de'), "zwei millionste"
)
self.assertEqual(
num2words(5000000000, ordinal=True, lang='de'),
"fünf milliardenste"
"fünf milliardste"
)

def test_cardinal_at_some_numbers(self):
Expand Down
24 changes: 24 additions & 0 deletions tests/test_en.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,30 @@ def test_to_currency(self):
'four thousand, seven hundred and seventy-eight dollars and'
' zero cents')

self.assertEqual(
num2words('1.1', lang='en', to='currency', seperator=' and',
cents=True, currency='MXN'),
"one peso and ten cents"
)

self.assertEqual(
num2words('158.3', lang='en', to='currency', seperator=' and',
cents=True, currency='MXN'),
"one hundred and fifty-eight pesos and thirty cents"
)

self.assertEqual(
num2words('2000.00', lang='en', to='currency', seperator=' and',
cents=True, currency='MXN'),
"two thousand pesos and zero cents"
)

self.assertEqual(
num2words('4.01', lang='en', to='currency', seperator=' and',
cents=True, currency='MXN'),
"four pesos and one cent"
)

def test_to_year(self):
# issue 141
# "e2 e2"
Expand Down
18 changes: 18 additions & 0 deletions tests/test_es.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,17 @@
(100.00, 'cien dolares con cero centavos'),
)

TEST_CASES_TO_CURRENCY_PEN = (
(1.00, 'un sol con cero centimos'),
(2.00, 'dos soles con cero centimos'),
(8.00, 'ocho soles con cero centimos'),
(12.00, 'doce soles con cero centimos'),
(21.00, 'veintiun soles con cero centimos'),
(81.25, 'ochenta y un soles con veinticinco centimos'),
(350.90, 'trescientos cincuenta soles con noventa centimos'),
(100.00, 'cien soles con cero centimos'),
)


class Num2WordsESTest(TestCase):

Expand Down Expand Up @@ -183,3 +194,10 @@ def test_currency_usd(self):
num2words(test[0], lang='es', to='currency', currency='USD'),
test[1]
)

def test_currency_pen(self):
for test in TEST_CASES_TO_CURRENCY_PEN:
self.assertEqual(
num2words(test[0], lang='es', to='currency', currency='PEN'),
test[1]
)

0 comments on commit 6dc21f4

Please sign in to comment.