mirror of
https://github.com/bblaz/num2words.git
synced 2025-12-06 06:42:25 +00:00
1
.gitignore
vendored
1
.gitignore
vendored
@@ -2,3 +2,4 @@
|
|||||||
build
|
build
|
||||||
dist
|
dist
|
||||||
*.egg-info
|
*.egg-info
|
||||||
|
/.tox
|
||||||
|
|||||||
9
.travis.yml
Normal file
9
.travis.yml
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
sudo: false
|
||||||
|
language: python
|
||||||
|
python:
|
||||||
|
- "2.7"
|
||||||
|
- "3.4"
|
||||||
|
- "3.5"
|
||||||
|
- "3.6"
|
||||||
|
install: pip install tox-travis
|
||||||
|
script: tox
|
||||||
25
CHANGES.rst
25
CHANGES.rst
@@ -1,6 +1,31 @@
|
|||||||
Changelog
|
Changelog
|
||||||
=========
|
=========
|
||||||
|
|
||||||
|
Version 0.5.5 -- 2017/07/02
|
||||||
|
---------------------------
|
||||||
|
|
||||||
|
* Add Arabic localization (#72)
|
||||||
|
* Add Spanish-Colombian and Spanish-Venezuelan localization (#67)
|
||||||
|
* Add VietNam localization (#61)
|
||||||
|
* Add Italian localization (#56, #59)
|
||||||
|
* Improve Russian localization (#62)
|
||||||
|
* Improve Polish localization (#58)
|
||||||
|
|
||||||
|
Version 0.5.4 -- 2016/10/18
|
||||||
|
---------------------------
|
||||||
|
|
||||||
|
* Tons of new languages!
|
||||||
|
* Add Polish localization. (#23)
|
||||||
|
* Add Swiss-French localization. (#38)
|
||||||
|
* Add Russian localization. (#28, #46, #48)
|
||||||
|
* Add Indonesian localization. (#29)
|
||||||
|
* Add Norwegian localization. (#33)
|
||||||
|
* Add Danish localization. (#40)
|
||||||
|
* Add Brazilian localization. (#37, #47)
|
||||||
|
* Improve German localization. (#25, #27, #49)
|
||||||
|
* Improve Lithuanian localization. (#52)
|
||||||
|
* Improve floating point spelling. (#24)
|
||||||
|
|
||||||
Version 0.5.3 -- 2015/06/09
|
Version 0.5.3 -- 2015/06/09
|
||||||
---------------------------
|
---------------------------
|
||||||
|
|
||||||
|
|||||||
52
README.rst
52
README.rst
@@ -1,9 +1,13 @@
|
|||||||
num2words - Convert numbers to words in multiple languages
|
num2words - Convert numbers to words in multiple languages
|
||||||
==========================================================
|
==========================================================
|
||||||
|
|
||||||
``num2words`` is a library that converts numbers like ``42`` to words like ``forty-two``. It
|
.. image:: https://travis-ci.org/savoirfairelinux/num2words.svg?branch=master
|
||||||
supports multiple languages (English, French, Spanish, German and Lithuanian) and can even generate
|
:target: https://travis-ci.org/savoirfairelinux/num2words
|
||||||
ordinal numbers like ``forty-second`` (altough this last feature is a bit buggy at the moment).
|
|
||||||
|
``num2words`` is a library that converts numbers like ``42`` to words like ``forty-two``.
|
||||||
|
It supports multiple languages (see the list below for full list
|
||||||
|
of languages) and can even generate ordinal numbers like ``forty-second``
|
||||||
|
(although this last feature is a bit buggy for some languages at the moment).
|
||||||
|
|
||||||
The project is hosted on https://github.com/savoirfairelinux/num2words
|
The project is hosted on https://github.com/savoirfairelinux/num2words
|
||||||
|
|
||||||
@@ -37,22 +41,40 @@ There's only one function to use::
|
|||||||
|
|
||||||
Besides the numerical argument, there's two optional arguments.
|
Besides the numerical argument, there's two optional arguments.
|
||||||
|
|
||||||
**ordinal:** A boolean flag indicating to return an ordinal number instead of a cardinal one.
|
**ordinal:** A boolean flag indicating to return an ordinal number instead of a
|
||||||
|
cardinal one.
|
||||||
|
|
||||||
**lang:** The language in which to convert the number. Supported values are:
|
**lang:** The language in which to convert the number. Supported values are:
|
||||||
|
|
||||||
* ``en`` (English, default)
|
* ``en`` (English, default)
|
||||||
* ``fr`` (French)
|
* ``ar`` (Arabic)
|
||||||
* ``de`` (German)
|
* ``de`` (German)
|
||||||
|
* ``dk`` (Danish)
|
||||||
|
* ``en_GB`` (English - Great Britain)
|
||||||
|
* ``en_IN`` (English - India)
|
||||||
* ``es`` (Spanish)
|
* ``es`` (Spanish)
|
||||||
|
* ``es_CO`` (Spanish - Colombia)
|
||||||
|
* ``es_VE`` (Spanish - Venezuela)
|
||||||
|
* ``eu`` (EURO)
|
||||||
|
* ``fr`` (French)
|
||||||
|
* ``fr_CH`` (French - Switzerland)
|
||||||
|
* ``fr_DZ`` (French - Algeria)
|
||||||
|
* ``he`` (Hebrew)
|
||||||
|
* ``id`` (Indonesian)
|
||||||
|
* ``it`` (Italian)
|
||||||
* ``lt`` (Lithuanian)
|
* ``lt`` (Lithuanian)
|
||||||
* ``lv`` (Latvian)
|
* ``lv`` (Latvian)
|
||||||
* ``en_GB`` (British English)
|
* ``no`` (Norwegian)
|
||||||
* ``en_IN`` (Indian English)
|
|
||||||
* ``pl`` (Polish)
|
* ``pl`` (Polish)
|
||||||
|
* ``pt_BR`` (Brazilian Portuguese)
|
||||||
|
* ``ru`` (Russian)
|
||||||
|
* ``tr`` (Turkish)
|
||||||
|
* ``vn`` (Vietnamese)
|
||||||
|
* ``nl`` (Dutch)
|
||||||
|
* ``uk`` (Ukrainian)
|
||||||
|
|
||||||
You can supply values like ``fr_FR``, the code will be
|
You can supply values like ``fr_FR``, the code will be correctly interpreted. If
|
||||||
correctly interpreted. If you supply an unsupported language, ``NotImplementedError`` is raised.
|
you supply an unsupported language, ``NotImplementedError`` is raised.
|
||||||
Therefore, if you want to call ``num2words`` with a fallback, you can do::
|
Therefore, if you want to call ``num2words`` with a fallback, you can do::
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@@ -63,12 +85,12 @@ Therefore, if you want to call ``num2words`` with a fallback, you can do::
|
|||||||
History
|
History
|
||||||
-------
|
-------
|
||||||
|
|
||||||
``num2words`` is based on an old library , ``pynum2word`` created by Taro Ogawa in 2003.
|
``num2words`` is based on an old library, ``pynum2word`` created by Taro Ogawa
|
||||||
Unfortunately, the library stopped being maintained and the author can't be reached. There was
|
in 2003. Unfortunately, the library stopped being maintained and the author
|
||||||
another developer, Marius Grigaitis, who in 2011 added Lithuanian support, but didn't take over
|
can't be reached. There was another developer, Marius Grigaitis, who in 2011
|
||||||
maintenance of the project.
|
added Lithuanian support, but didn't take over maintenance of the project.
|
||||||
|
|
||||||
I am thus basing myself on Marius Grigaitis' improvements and re-publishing ``pynum2word`` as
|
I am thus basing myself on Marius Grigaitis' improvements and re-publishing
|
||||||
``num2words``.
|
``pynum2word`` as ``num2words``.
|
||||||
|
|
||||||
Virgil Dupras, Savoir-faire Linux
|
Virgil Dupras, Savoir-faire Linux
|
||||||
|
|||||||
@@ -16,26 +16,58 @@
|
|||||||
|
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
from . import lang_AR
|
||||||
from . import lang_EN
|
from . import lang_EN
|
||||||
from . import lang_EN_GB
|
from . import lang_EN_GB
|
||||||
from . import lang_EN_IN
|
from . import lang_EN_IN
|
||||||
from . import lang_FR
|
from . import lang_FR
|
||||||
|
from . import lang_FR_CH
|
||||||
|
from . import lang_FR_DZ
|
||||||
from . import lang_DE
|
from . import lang_DE
|
||||||
from . import lang_ES
|
from . import lang_ES
|
||||||
from . import lang_LT
|
from . import lang_LT
|
||||||
from . import lang_LV
|
from . import lang_LV
|
||||||
from . import lang_PL
|
from . import lang_PL
|
||||||
|
from . import lang_RU
|
||||||
|
from . import lang_ID
|
||||||
|
from . import lang_NO
|
||||||
|
from . import lang_DK
|
||||||
|
from . import lang_PT_BR
|
||||||
|
from . import lang_HE
|
||||||
|
from . import lang_IT
|
||||||
|
from . import lang_ES_VE
|
||||||
|
from . import lang_ES_CO
|
||||||
|
from . import lang_VN
|
||||||
|
from . import lang_TR
|
||||||
|
from . import lang_NL
|
||||||
|
from . import lang_UK
|
||||||
|
|
||||||
CONVERTER_CLASSES = {
|
CONVERTER_CLASSES = {
|
||||||
|
'ar': lang_AR.Num2Word_AR(),
|
||||||
'en': lang_EN.Num2Word_EN(),
|
'en': lang_EN.Num2Word_EN(),
|
||||||
'en_GB': lang_EN_GB.Num2Word_EN_GB(),
|
'en_GB': lang_EN_GB.Num2Word_EN_GB(),
|
||||||
'en_IN': lang_EN_IN.Num2Word_EN_IN(),
|
'en_IN': lang_EN_IN.Num2Word_EN_IN(),
|
||||||
'fr': lang_FR.Num2Word_FR(),
|
'fr': lang_FR.Num2Word_FR(),
|
||||||
|
'fr_CH': lang_FR_CH.Num2Word_FR_CH(),
|
||||||
|
'fr_DZ': lang_FR_DZ.Num2Word_FR_DZ(),
|
||||||
'de': lang_DE.Num2Word_DE(),
|
'de': lang_DE.Num2Word_DE(),
|
||||||
'es': lang_ES.Num2Word_ES(),
|
'es': lang_ES.Num2Word_ES(),
|
||||||
|
'es_CO': lang_ES_CO.Num2Word_ES_CO(),
|
||||||
|
'es_VE': lang_ES_VE.Num2Word_ES_VE(),
|
||||||
|
'id': lang_ID.Num2Word_ID(),
|
||||||
'lt': lang_LT.Num2Word_LT(),
|
'lt': lang_LT.Num2Word_LT(),
|
||||||
'lv': lang_LV.Num2Word_LV(),
|
'lv': lang_LV.Num2Word_LV(),
|
||||||
'pl': lang_PL.Num2Word_PL(),
|
'pl': lang_PL.Num2Word_PL(),
|
||||||
|
'ru': lang_RU.Num2Word_RU(),
|
||||||
|
'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(),
|
||||||
|
'tr': lang_TR.Num2Word_TR(),
|
||||||
|
'nl': lang_NL.Num2Word_NL(),
|
||||||
|
'uk': lang_UK.Num2Word_UK()
|
||||||
}
|
}
|
||||||
|
|
||||||
def num2words(number, ordinal=False, lang='en'):
|
def num2words(number, ordinal=False, lang='en'):
|
||||||
|
|||||||
@@ -15,7 +15,11 @@
|
|||||||
# MA 02110-1301 USA
|
# MA 02110-1301 USA
|
||||||
|
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
import math
|
||||||
|
|
||||||
from .orderedmapping import OrderedMapping
|
from .orderedmapping import OrderedMapping
|
||||||
|
from .compat import to_s
|
||||||
|
|
||||||
|
|
||||||
class Num2Word_Base(object):
|
class Num2Word_Base(object):
|
||||||
@@ -88,7 +92,7 @@ class Num2Word_Base(object):
|
|||||||
|
|
||||||
def to_cardinal(self, value):
|
def to_cardinal(self, value):
|
||||||
try:
|
try:
|
||||||
assert long(value) == value
|
assert int(value) == value
|
||||||
except (ValueError, TypeError, AssertionError):
|
except (ValueError, TypeError, AssertionError):
|
||||||
return self.to_cardinal_float(value)
|
return self.to_cardinal_float(value)
|
||||||
|
|
||||||
@@ -102,30 +106,43 @@ class Num2Word_Base(object):
|
|||||||
if value >= self.MAXVAL:
|
if value >= self.MAXVAL:
|
||||||
raise OverflowError(self.errmsg_toobig % (value, self.MAXVAL))
|
raise OverflowError(self.errmsg_toobig % (value, self.MAXVAL))
|
||||||
|
|
||||||
|
|
||||||
val = self.splitnum(value)
|
val = self.splitnum(value)
|
||||||
words, num = self.clean(val)
|
words, num = self.clean(val)
|
||||||
return self.title(out + words)
|
return self.title(out + words)
|
||||||
|
|
||||||
|
|
||||||
|
def float2tuple(self, value):
|
||||||
|
pre = int(value)
|
||||||
|
post = abs(value - pre) * 10**self.precision
|
||||||
|
if abs(round(post) - post) < 0.01:
|
||||||
|
# We generally floor all values beyond our precision (rather than rounding), but in
|
||||||
|
# cases where we have something like 1.239999999, which is probably due to python's
|
||||||
|
# handling of floats, we actually want to consider it as 1.24 instead of 1.23
|
||||||
|
post = int(round(post))
|
||||||
|
else:
|
||||||
|
post = int(math.floor(post))
|
||||||
|
|
||||||
|
return pre, post
|
||||||
|
|
||||||
|
|
||||||
def to_cardinal_float(self, value):
|
def to_cardinal_float(self, value):
|
||||||
try:
|
try:
|
||||||
float(value) == value
|
float(value) == value
|
||||||
except (ValueError, TypeError, AssertionError):
|
except (ValueError, TypeError, AssertionError):
|
||||||
raise TypeError(self.errmsg_nonnum % value)
|
raise TypeError(self.errmsg_nonnum % value)
|
||||||
|
|
||||||
pre = int(value)
|
pre, post = self.float2tuple(float(value))
|
||||||
post = abs(value - pre)
|
|
||||||
|
post = str(post)
|
||||||
|
post = '0' * (self.precision - len(post)) + post
|
||||||
|
|
||||||
out = [self.to_cardinal(pre)]
|
out = [self.to_cardinal(pre)]
|
||||||
if self.precision:
|
if self.precision:
|
||||||
out.append(self.title(self.pointword))
|
out.append(self.title(self.pointword))
|
||||||
|
|
||||||
for i in range(self.precision):
|
for i in range(self.precision):
|
||||||
post *= 10
|
curr = int(post[i])
|
||||||
curr = int(post)
|
out.append(to_s(self.to_cardinal(curr)))
|
||||||
out.append(str(self.to_cardinal(curr)))
|
|
||||||
post -= curr
|
|
||||||
|
|
||||||
return " ".join(out)
|
return " ".join(out)
|
||||||
|
|
||||||
@@ -170,10 +187,10 @@ class Num2Word_Base(object):
|
|||||||
|
|
||||||
|
|
||||||
def verify_ordinal(self, value):
|
def verify_ordinal(self, value):
|
||||||
if not value == long(value):
|
if not value == int(value):
|
||||||
raise TypeError, self.errmsg_floatord %(value)
|
raise TypeError(self.errmsg_floatord % value)
|
||||||
if not abs(value) == value:
|
if not abs(value) == value:
|
||||||
raise TypeError, self.errmsg_negord %(value)
|
raise TypeError(self.errmsg_negord % value)
|
||||||
|
|
||||||
|
|
||||||
def verify_num(self, value):
|
def verify_num(self, value):
|
||||||
@@ -184,7 +201,7 @@ class Num2Word_Base(object):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def to_ordinal(value):
|
def to_ordinal(self, value):
|
||||||
return self.to_cardinal(value)
|
return self.to_cardinal(value)
|
||||||
|
|
||||||
|
|
||||||
@@ -202,12 +219,17 @@ class Num2Word_Base(object):
|
|||||||
|
|
||||||
#//CHECK: generalise? Any others like pounds/shillings/pence?
|
#//CHECK: generalise? Any others like pounds/shillings/pence?
|
||||||
def to_splitnum(self, val, hightxt="", lowtxt="", jointxt="",
|
def to_splitnum(self, val, hightxt="", lowtxt="", jointxt="",
|
||||||
divisor=100, longval=True, cents = True):
|
divisor=100, longval=True, cents=True):
|
||||||
out = []
|
out = []
|
||||||
|
|
||||||
|
if isinstance(val, float):
|
||||||
|
high, low = self.float2tuple(val)
|
||||||
|
else:
|
||||||
try:
|
try:
|
||||||
high, low = val
|
high, low = val
|
||||||
except TypeError:
|
except TypeError:
|
||||||
high, low = divmod(val, divisor)
|
high, low = divmod(val, divisor)
|
||||||
|
|
||||||
if high:
|
if high:
|
||||||
hightxt = self.title(self.inflect(high, hightxt))
|
hightxt = self.title(self.inflect(high, hightxt))
|
||||||
out.append(self.to_cardinal(high))
|
out.append(self.to_cardinal(high))
|
||||||
@@ -219,6 +241,7 @@ class Num2Word_Base(object):
|
|||||||
out.append(self.title(jointxt))
|
out.append(self.title(jointxt))
|
||||||
elif hightxt:
|
elif hightxt:
|
||||||
out.append(hightxt)
|
out.append(hightxt)
|
||||||
|
|
||||||
if low:
|
if low:
|
||||||
if cents:
|
if cents:
|
||||||
out.append(self.to_cardinal(low))
|
out.append(self.to_cardinal(low))
|
||||||
@@ -226,6 +249,7 @@ class Num2Word_Base(object):
|
|||||||
out.append("%02d" % low)
|
out.append("%02d" % low)
|
||||||
if lowtxt and longval:
|
if lowtxt and longval:
|
||||||
out.append(self.title(self.inflect(low, lowtxt)))
|
out.append(self.title(self.inflect(low, lowtxt)))
|
||||||
|
|
||||||
return " ".join(out)
|
return " ".join(out)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
26
num2words/compat.py
Normal file
26
num2words/compat.py
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
# Copyright (c) 2003, Taro Ogawa. All Rights Reserved.
|
||||||
|
# Copyright (c) 2016, 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
|
||||||
|
|
||||||
|
import sys
|
||||||
|
|
||||||
|
PY3 = sys.version_info[0] == 3
|
||||||
|
|
||||||
|
def to_s(val):
|
||||||
|
if PY3:
|
||||||
|
return str(val)
|
||||||
|
else:
|
||||||
|
return unicode(val)
|
||||||
|
|
||||||
121
num2words/lang_AR.py
Normal file
121
num2words/lang_AR.py
Normal file
@@ -0,0 +1,121 @@
|
|||||||
|
# -*- 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 division, unicode_literals, print_function
|
||||||
|
from . import lang_EU
|
||||||
|
|
||||||
|
class Num2Word_AR(lang_EU.Num2Word_EU):
|
||||||
|
def set_high_numwords(self, high):
|
||||||
|
max = 3 + 3*len(high)
|
||||||
|
for word, n in zip(high, range(max, 3, -3)):
|
||||||
|
self.cards[10**n] = word + "illion"
|
||||||
|
|
||||||
|
def setup(self):
|
||||||
|
self.negword = "سالب "
|
||||||
|
self.pointword = "فاصلة"
|
||||||
|
self.errmsg_nornum = "Only numbers may be converted to words."
|
||||||
|
self.exclude_title = ["و", "فاصلة", "سالب"]
|
||||||
|
|
||||||
|
self.mid_numwords = [(1000000, "مليون"),(1000, "ألف"), (100, "مئة"),
|
||||||
|
(90, "تسعين"), (80, "ثمانين"), (70, "سبعين"),
|
||||||
|
(60, "ستين"), (50, "خمسين"), (40, "أربعين"),
|
||||||
|
(30, "ثلاثين")]
|
||||||
|
self.low_numwords = ["عشرين", "تسعة عشر", "ثمانية عشر", "سبعة عشر",
|
||||||
|
"ستة عشر", "خمسة عشر", "أربعة عشر", "ثلاثة عشر",
|
||||||
|
"اثناعشر", "أحد عشر", "عشرة", "تسعة", "ثمانية",
|
||||||
|
"سبعة", "ستة", "خمسة", "أربعة", "ثلاثة", "اثنين",
|
||||||
|
"واحد", "صفر"]
|
||||||
|
self.ords = { "واحد" : "أول",
|
||||||
|
"اثنين" : "ثاني",
|
||||||
|
"ثلاثة" : "ثالث",
|
||||||
|
"أربعة": "رابع",
|
||||||
|
"خمسة" : "خامس",
|
||||||
|
"ثمانية" : "ثامن",
|
||||||
|
"تسعة" : "تاسع",
|
||||||
|
"اثناعشر" : "ثاني عشر" }
|
||||||
|
|
||||||
|
|
||||||
|
def merge(self, lpair, rpair):
|
||||||
|
ltext, lnum = lpair
|
||||||
|
rtext, rnum = rpair
|
||||||
|
if lnum == 1 and rnum < 100:
|
||||||
|
return (rtext, rnum)
|
||||||
|
elif 100 > lnum > rnum :
|
||||||
|
return ("%s و%s"%(rtext, ltext), rnum + lnum)
|
||||||
|
elif lnum >= 100 > rnum:
|
||||||
|
return ("%s و %s"%(ltext, rtext), lnum + rnum)
|
||||||
|
elif rnum > lnum:
|
||||||
|
if lnum == 1 and rnum in [100, 1000, 1000000]:
|
||||||
|
return ("%s"%(rtext), rnum * lnum)
|
||||||
|
if lnum == 2 and rnum == 100:
|
||||||
|
return ("مئتين", rnum * lnum)
|
||||||
|
if lnum == 2 and rnum in [100, 1000]:
|
||||||
|
return ("%sين"%(rtext), rnum * lnum)
|
||||||
|
return ("%s %s"%(ltext, rtext), lnum * rnum)
|
||||||
|
return ("%s، %s"%(ltext, rtext), lnum + rnum)
|
||||||
|
|
||||||
|
|
||||||
|
def to_ordinal(self, value):
|
||||||
|
self.verify_ordinal(value)
|
||||||
|
outwords = self.to_cardinal(value).split(" ")
|
||||||
|
lastwords = outwords[-1].split("-")
|
||||||
|
lastword = lastwords[-1].lower()
|
||||||
|
try:
|
||||||
|
lastword = self.ords[lastword]
|
||||||
|
except KeyError:
|
||||||
|
lastword += ""
|
||||||
|
lastwords[-1] = self.title(lastword)
|
||||||
|
outwords[-1] = "،".join(lastwords)
|
||||||
|
return " ".join(outwords)
|
||||||
|
|
||||||
|
|
||||||
|
def to_ordinal_num(self, value):
|
||||||
|
self.verify_ordinal(value)
|
||||||
|
return "%s%s"%(value, self.to_ordinal(value)[-2:])
|
||||||
|
|
||||||
|
|
||||||
|
def to_year(self, val, longval=True):
|
||||||
|
if not (val//100)%10:
|
||||||
|
return self.to_cardinal(val)
|
||||||
|
return self.to_splitnum(val, hightxt="مئة", jointxt="و",
|
||||||
|
longval=longval)
|
||||||
|
|
||||||
|
def to_currency(self, val, longval=True):
|
||||||
|
return self.to_splitnum(val, hightxt="ريال", lowtxt="هللة",
|
||||||
|
jointxt="و", longval=longval, cents = True)
|
||||||
|
|
||||||
|
|
||||||
|
n2w = Num2Word_AR()
|
||||||
|
to_card = n2w.to_cardinal
|
||||||
|
to_ord = n2w.to_ordinal
|
||||||
|
to_ordnum = n2w.to_ordinal_num
|
||||||
|
to_year = n2w.to_year
|
||||||
|
|
||||||
|
def main():
|
||||||
|
for val in [ 1, 11, 12, 21, 31, 33, 71, 80, 81, 91, 99, 100, 101, 102, 155,
|
||||||
|
180, 300, 308, 832, 1000, 1001, 1061, 1100, 1500, 1701, 3000,
|
||||||
|
8280, 8291, 150000, 500000, 1000000, 2000000, 2000001,
|
||||||
|
-21212121211221211111, -2.121212, -1.0000100]:
|
||||||
|
n2w.test(val)
|
||||||
|
n2w.test(1325325436067876801768700107601001012212132143210473207540327057320957032975032975093275093275093270957329057320975093272950730)
|
||||||
|
for val in [1,120,1000,1120,1800, 1976,2000,2010,2099,2171]:
|
||||||
|
print(val, "is", n2w.to_currency(val))
|
||||||
|
print(val, "is", n2w.to_year(val))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
# -*- 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.
|
||||||
|
|
||||||
@@ -14,10 +15,9 @@
|
|||||||
# 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, print_function
|
||||||
from .lang_EU import Num2Word_EU
|
from .lang_EU import Num2Word_EU
|
||||||
|
|
||||||
#//TODO: Use German error messages
|
|
||||||
class Num2Word_DE(Num2Word_EU):
|
class Num2Word_DE(Num2Word_EU):
|
||||||
def set_high_numwords(self, high):
|
def set_high_numwords(self, high):
|
||||||
max = 3 + 6*len(high)
|
max = 3 + 6*len(high)
|
||||||
@@ -26,12 +26,13 @@ class Num2Word_DE(Num2Word_EU):
|
|||||||
self.cards[10**n] = word + "illiarde"
|
self.cards[10**n] = word + "illiarde"
|
||||||
self.cards[10**(n-3)] = word + "illion"
|
self.cards[10**(n-3)] = word + "illion"
|
||||||
|
|
||||||
|
|
||||||
def setup(self):
|
def setup(self):
|
||||||
self.negword = "minus "
|
self.negword = "minus "
|
||||||
self.pointword = "Komma"
|
self.pointword = "Komma"
|
||||||
self.errmsg_nonnum = "Only numbers may be converted to words."
|
self.errmsg_floatord = "Die Gleitkommazahl %s kann nicht in eine Ordnungszahl konvertiert werden." # "Cannot treat float %s as ordinal."
|
||||||
self.errmsg_toobig = "Number is too large to convert to words."
|
self.errmsg_nonnum = "Nur Zahlen (type(%s)) können in Wörter konvertiert werden." # "type(((type(%s)) ) not in [long, int, float]"
|
||||||
|
self.errmsg_negord = "Die negative Zahl %s kann nicht in eine Ordnungszahl konvertiert werden." # "Cannot treat negative num %s as ordinal."
|
||||||
|
self.errmsg_toobig = "Die Zahl %s muss kleiner als %s sein." # "abs(%s) must be less than %s."
|
||||||
self.exclude_title = []
|
self.exclude_title = []
|
||||||
|
|
||||||
lows = ["non", "okt", "sept", "sext", "quint", "quadr", "tr", "b", "m"]
|
lows = ["non", "okt", "sept", "sext", "quint", "quadr", "tr", "b", "m"]
|
||||||
@@ -49,29 +50,33 @@ class Num2Word_DE(Num2Word_EU):
|
|||||||
"zw\xF6lf", "elf", "zehn", "neun", "acht", "sieben",
|
"zw\xF6lf", "elf", "zehn", "neun", "acht", "sieben",
|
||||||
"sechs", "f\xFCnf", "vier", "drei", "zwei", "eins",
|
"sechs", "f\xFCnf", "vier", "drei", "zwei", "eins",
|
||||||
"null"]
|
"null"]
|
||||||
self.ords = { "eins" : "ers",
|
self.ords = {"eins": "ers",
|
||||||
"drei" : "drit",
|
"drei": "drit",
|
||||||
"acht" : "ach",
|
"acht": "ach",
|
||||||
"sieben" : "sieb",
|
"sieben": "sieb",
|
||||||
"ig" : "igs" }
|
"ig": "igs",
|
||||||
self.ordflag = False
|
"ert": "erts",
|
||||||
|
"end": "ends",
|
||||||
|
"ion": "ions",
|
||||||
|
"nen": "nens",
|
||||||
|
"rde": "rdes",
|
||||||
|
"rden": "rdens"}
|
||||||
|
|
||||||
def merge(self, curr, next):
|
def merge(self, curr, next):
|
||||||
ctext, cnum, ntext, nnum = curr + next
|
ctext, cnum, ntext, nnum = curr + next
|
||||||
|
|
||||||
if cnum == 1:
|
if cnum == 1:
|
||||||
if nnum < 10**6 or self.ordflag:
|
if nnum < 10**6:
|
||||||
return next
|
return next
|
||||||
ctext = "eine"
|
ctext = "eine"
|
||||||
|
|
||||||
if nnum > cnum:
|
if nnum > cnum:
|
||||||
if nnum >= 10**6:
|
if nnum >= 10**6:
|
||||||
if cnum > 1:
|
if cnum > 1:
|
||||||
if ntext.endswith("e") or self.ordflag:
|
if ntext.endswith("e"):
|
||||||
ntext += "s"
|
ntext += "n"
|
||||||
else:
|
else:
|
||||||
ntext += "es"
|
ntext += "en"
|
||||||
ctext += " "
|
ctext += " "
|
||||||
val = cnum * nnum
|
val = cnum * nnum
|
||||||
else:
|
else:
|
||||||
@@ -86,24 +91,18 @@ class Num2Word_DE(Num2Word_EU):
|
|||||||
word = ctext + ntext
|
word = ctext + ntext
|
||||||
return (word, val)
|
return (word, val)
|
||||||
|
|
||||||
|
|
||||||
def to_ordinal(self, value):
|
def to_ordinal(self, value):
|
||||||
self.verify_ordinal(value)
|
self.verify_ordinal(value)
|
||||||
self.ordflag = True
|
|
||||||
outword = self.to_cardinal(value)
|
outword = self.to_cardinal(value)
|
||||||
self.ordflag = False
|
|
||||||
for key in self.ords:
|
for key in self.ords:
|
||||||
if outword.endswith(key):
|
if outword.endswith(key):
|
||||||
outword = outword[:len(outword) - len(key)] + self.ords[key]
|
outword = outword[:len(outword) - len(key)] + self.ords[key]
|
||||||
break
|
break
|
||||||
return outword + "te"
|
return outword + "te"
|
||||||
|
|
||||||
|
|
||||||
# Is this correct??
|
|
||||||
def to_ordinal_num(self, value):
|
def to_ordinal_num(self, value):
|
||||||
self.verify_ordinal(value)
|
self.verify_ordinal(value)
|
||||||
return str(value) + "te"
|
return str(value) + "."
|
||||||
|
|
||||||
|
|
||||||
def to_currency(self, val, longval=True, old=False):
|
def to_currency(self, val, longval=True, old=False):
|
||||||
if old:
|
if old:
|
||||||
@@ -117,8 +116,6 @@ class Num2Word_DE(Num2Word_EU):
|
|||||||
return self.to_cardinal(val)
|
return self.to_cardinal(val)
|
||||||
return self.to_splitnum(val, hightxt="hundert", longval=longval)
|
return self.to_splitnum(val, hightxt="hundert", longval=longval)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
n2w = Num2Word_DE()
|
n2w = Num2Word_DE()
|
||||||
to_card = n2w.to_cardinal
|
to_card = n2w.to_cardinal
|
||||||
to_ord = n2w.to_ordinal
|
to_ord = n2w.to_ordinal
|
||||||
@@ -126,15 +123,20 @@ to_ordnum = n2w.to_ordinal_num
|
|||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
for val in [ 1, 11, 12, 21, 31, 33, 71, 80, 81, 91, 99, 100, 101, 102, 155,
|
for val in [1, 7, 8, 12, 17, 81, 91, 99, 100, 101, 102, 155,
|
||||||
180, 300, 308, 832, 1000, 1001, 1061, 1100, 1500, 1701, 3000,
|
180, 300, 308, 832, 1000, 1001, 1061, 1100, 1500, 1701, 3000,
|
||||||
8280, 8291, 150000, 500000, 1000000, 2000000, 2000001,
|
8280, 8291, 150000, 500000, 3000000, 1000000, 2000001, 1000000000, 2000000000,
|
||||||
-21212121211221211111, -2.121212, -1.0000100]:
|
-21212121211221211111, -2.121212, -1.0000100]:
|
||||||
n2w.test(val)
|
n2w.test(val)
|
||||||
|
|
||||||
n2w.test(1325325436067876801768700107601001012212132143210473207540327057320957032975032975093275093275093270957329057320975093272950730)
|
# n2w.test(1325325436067876801768700107601001012212132143210473207540327057320957032975032975093275093275093270957329057320975093272950730)
|
||||||
print n2w.to_currency(112121)
|
n2w.test(3000000)
|
||||||
print n2w.to_year(2000)
|
n2w.test(3000000000001)
|
||||||
|
n2w.test(3000000324566)
|
||||||
|
print(n2w.to_currency(112121))
|
||||||
|
print(n2w.to_year(2000))
|
||||||
|
print(n2w.to_year(1820))
|
||||||
|
print(n2w.to_year(2001))
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|||||||
154
num2words/lang_DK.py
Normal file
154
num2words/lang_DK.py
Normal file
@@ -0,0 +1,154 @@
|
|||||||
|
# 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, unicode_literals, print_function
|
||||||
|
from num2words import lang_EU
|
||||||
|
|
||||||
|
class Num2Word_DK(lang_EU.Num2Word_EU):
|
||||||
|
def set_high_numwords(self, high):
|
||||||
|
max = 3 + 6*len(high)
|
||||||
|
for word, n in zip(high, range(max, 3, -6)):
|
||||||
|
self.cards[10**n] = word + "illarder"
|
||||||
|
self.cards[10**(n-3)] = word + "illioner"
|
||||||
|
|
||||||
|
def setup(self):
|
||||||
|
self.negword = "minus "
|
||||||
|
self.pointword = "komma"
|
||||||
|
self.errmsg_nornum = "Kun tal kan blive konverteret til ord."
|
||||||
|
self.exclude_title = ["og", "komma", "minus"]
|
||||||
|
|
||||||
|
self.mid_numwords = [(1000, "tusind"), (100, "hundrede"),
|
||||||
|
(90, "halvfems"), (80, "firs"), (70, "halvfjerds"),
|
||||||
|
(60, "treds"), (50, "halvtreds"), (40, "fyrre"),
|
||||||
|
(30, "tredive")]
|
||||||
|
self.low_numwords = ["tyve", "nitten", "atten", "sytten",
|
||||||
|
"seksten", "femten", "fjorten", "tretten",
|
||||||
|
"tolv", "elleve", "ti", "ni", "otte",
|
||||||
|
"syv", "seks", "fem", "fire", "tre", "to",
|
||||||
|
"et", "nul"]
|
||||||
|
self.ords = { "nul" : "nul",
|
||||||
|
"et" : "f\xf8rste",
|
||||||
|
"to" : "anden",
|
||||||
|
"tre" : "tredje",
|
||||||
|
"fire" : "fjerde",
|
||||||
|
"fem" : "femte",
|
||||||
|
"seks" : "sjette",
|
||||||
|
"syv" : "syvende",
|
||||||
|
"otte" : "ottende",
|
||||||
|
"ni" : "niende",
|
||||||
|
"ti" : "tiende",
|
||||||
|
"elleve" : "ellevte",
|
||||||
|
"tolv" : "tolvte",
|
||||||
|
"tretten" : "trett",
|
||||||
|
"fjorten" : "fjort",
|
||||||
|
"femten" : "femt",
|
||||||
|
"seksten" : "sekst",
|
||||||
|
"sytten" : "sytt",
|
||||||
|
"atten" : "att",
|
||||||
|
"nitten" : "nitt",
|
||||||
|
"tyve" : "tyv"}
|
||||||
|
|
||||||
|
def merge(self, curr, next):
|
||||||
|
ctext, cnum, ntext, nnum = curr + next
|
||||||
|
if next[1] == 100 or next[1] == 1000:
|
||||||
|
lst = list(next)
|
||||||
|
lst[0] = 'et' + lst[0]
|
||||||
|
next = tuple(lst)
|
||||||
|
|
||||||
|
if cnum == 1:
|
||||||
|
if nnum < 10**6 or self.ordflag:
|
||||||
|
return next
|
||||||
|
ctext = "en"
|
||||||
|
if nnum > cnum:
|
||||||
|
if nnum >= 10**6:
|
||||||
|
ctext += " "
|
||||||
|
val = cnum * nnum
|
||||||
|
else:
|
||||||
|
if cnum >= 100 and cnum < 1000:
|
||||||
|
ctext += " og "
|
||||||
|
elif cnum >= 1000 and cnum <= 100000:
|
||||||
|
ctext += "e og "
|
||||||
|
if nnum < 10 < cnum < 100:
|
||||||
|
if nnum == 1:
|
||||||
|
ntext = "en"
|
||||||
|
ntext, ctext = ctext, ntext + "og"
|
||||||
|
elif cnum >= 10**6:
|
||||||
|
ctext += " "
|
||||||
|
val = cnum + nnum
|
||||||
|
word = ctext + ntext
|
||||||
|
return (word, val)
|
||||||
|
|
||||||
|
|
||||||
|
def to_ordinal(self, value):
|
||||||
|
self.verify_ordinal(value)
|
||||||
|
self.ordflag = True
|
||||||
|
outword = self.to_cardinal(value)
|
||||||
|
self.ordflag = False
|
||||||
|
for key in self.ords:
|
||||||
|
if outword.endswith(key):
|
||||||
|
outword = outword[:len(outword) - len(key)] + self.ords[key]
|
||||||
|
break
|
||||||
|
if value %100 >= 30 and value %100 <= 39 or value %100 == 0:
|
||||||
|
outword += "te"
|
||||||
|
elif value % 100 > 12 or value %100 == 0:
|
||||||
|
outword += "ende"
|
||||||
|
return outword
|
||||||
|
|
||||||
|
def to_ordinal_num(self, value):
|
||||||
|
self.verify_ordinal(value)
|
||||||
|
vaerdte = (0,1,5,6,11,12)
|
||||||
|
if value %100 >= 30 and value %100 <= 39 or value % 100 in vaerdte:
|
||||||
|
return str(value) + "te"
|
||||||
|
elif value % 100 == 2:
|
||||||
|
return str(value) + "en"
|
||||||
|
return str(value) + "ende"
|
||||||
|
|
||||||
|
|
||||||
|
def to_currency(self, val, longval=True):
|
||||||
|
if val//100 == 1 or val == 1:
|
||||||
|
ret = self.to_splitnum(val, hightxt="kr", lowtxt="\xf8re",
|
||||||
|
jointxt="og",longval=longval)
|
||||||
|
return "en " + ret[3:]
|
||||||
|
return self.to_splitnum(val, hightxt="kr", lowtxt="\xf8re",
|
||||||
|
jointxt="og",longval=longval)
|
||||||
|
|
||||||
|
def to_year(self, val, longval=True):
|
||||||
|
if val == 1:
|
||||||
|
return 'en'
|
||||||
|
if not (val//100)%10:
|
||||||
|
return self.to_cardinal(val)
|
||||||
|
return self.to_splitnum(val, hightxt="hundrede", longval=longval)
|
||||||
|
|
||||||
|
n2w = Num2Word_DK()
|
||||||
|
to_card = n2w.to_cardinal
|
||||||
|
to_ord = n2w.to_ordinal
|
||||||
|
to_ordnum = n2w.to_ordinal_num
|
||||||
|
to_year = n2w.to_year
|
||||||
|
|
||||||
|
def main():
|
||||||
|
for val in [ 1, 11, 12, 21, 31, 33, 71, 80, 81, 91, 99, 100, 101, 102, 155,
|
||||||
|
180, 300, 308, 832, 1000, 1001, 1061, 1100, 1500, 1701, 3000,
|
||||||
|
8280, 8291, 150000, 500000, 1000000, 2000000, 2000001,
|
||||||
|
-21212121211221211111, -2.121212, -1.0000100]:
|
||||||
|
n2w.test(val)
|
||||||
|
n2w.test(1325325436067876801768700107601001012212132143210473207540327057320957032975032975093275093275093270957329057320975093272950730)
|
||||||
|
for val in [1,120, 160, 1000,1120,1800, 1976,2000,2010,2099,2171]:
|
||||||
|
print(val, "er", n2w.to_currency(val))
|
||||||
|
print(val, "er", n2w.to_year(val))
|
||||||
|
n2w.test(65132)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
@@ -14,7 +14,7 @@
|
|||||||
# 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 division, unicode_literals
|
from __future__ import division, unicode_literals, print_function
|
||||||
from . import lang_EU
|
from . import lang_EU
|
||||||
|
|
||||||
class Num2Word_EN(lang_EU.Num2Word_EU):
|
class Num2Word_EN(lang_EU.Num2Word_EU):
|
||||||
@@ -47,7 +47,9 @@ class Num2Word_EN(lang_EU.Num2Word_EU):
|
|||||||
"twelve" : "twelfth" }
|
"twelve" : "twelfth" }
|
||||||
|
|
||||||
|
|
||||||
def merge(self, (ltext, lnum), (rtext, rnum)):
|
def merge(self, lpair, rpair):
|
||||||
|
ltext, lnum = lpair
|
||||||
|
rtext, rnum = rpair
|
||||||
if lnum == 1 and rnum < 100:
|
if lnum == 1 and rnum < 100:
|
||||||
return (rtext, rnum)
|
return (rtext, rnum)
|
||||||
elif 100 > lnum > rnum :
|
elif 100 > lnum > rnum :
|
||||||
@@ -105,8 +107,8 @@ def main():
|
|||||||
n2w.test(val)
|
n2w.test(val)
|
||||||
n2w.test(1325325436067876801768700107601001012212132143210473207540327057320957032975032975093275093275093270957329057320975093272950730)
|
n2w.test(1325325436067876801768700107601001012212132143210473207540327057320957032975032975093275093275093270957329057320975093272950730)
|
||||||
for val in [1,120,1000,1120,1800, 1976,2000,2010,2099,2171]:
|
for val in [1,120,1000,1120,1800, 1976,2000,2010,2099,2171]:
|
||||||
print val, "is", n2w.to_currency(val)
|
print(val, "is", n2w.to_currency(val))
|
||||||
print val, "is", n2w.to_year(val)
|
print(val, "is", n2w.to_year(val))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
@@ -14,7 +14,7 @@
|
|||||||
# 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, print_function
|
||||||
from .lang_EN import Num2Word_EN
|
from .lang_EN import Num2Word_EN
|
||||||
|
|
||||||
|
|
||||||
@@ -38,8 +38,8 @@ def main():
|
|||||||
n2w.test(val)
|
n2w.test(val)
|
||||||
n2w.test(1325325436067876801768700107601001012212132143210473207540327057320957032975032975093275093275093270957329057320975093272950730)
|
n2w.test(1325325436067876801768700107601001012212132143210473207540327057320957032975032975093275093275093270957329057320975093272950730)
|
||||||
for val in [1,120,1000,1120,1800, 1976,2000,2010,2099,2171]:
|
for val in [1,120,1000,1120,1800, 1976,2000,2010,2099,2171]:
|
||||||
print val, "is", n2w.to_currency(val)
|
print(val, "is", n2w.to_currency(val))
|
||||||
print val, "is", n2w.to_year(val)
|
print(val, "is", n2w.to_year(val))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
#encoding: UTF-8
|
# encoding: 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.
|
||||||
@@ -16,19 +16,19 @@
|
|||||||
# 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, print_function
|
||||||
from .lang_EU import Num2Word_EU
|
from .lang_EU import Num2Word_EU
|
||||||
|
|
||||||
|
|
||||||
class Num2Word_ES(Num2Word_EU):
|
class Num2Word_ES(Num2Word_EU):
|
||||||
|
|
||||||
#//CHECK: Is this sufficient??
|
# //CHECK: Is this sufficient??
|
||||||
def set_high_numwords(self, high):
|
def set_high_numwords(self, high):
|
||||||
max = 3 + 6*len(high)
|
max = 3 + 6*len(high)
|
||||||
|
|
||||||
for word, n in zip(high, range(max, 3, -6)):
|
for word, n in zip(high, range(max, 3, -6)):
|
||||||
self.cards[10**(n-3)] = word + "illón"
|
self.cards[10**(n-3)] = word + "illón"
|
||||||
|
|
||||||
|
|
||||||
def setup(self):
|
def setup(self):
|
||||||
lows = ["cuatr", "tr", "b", "m"]
|
lows = ["cuatr", "tr", "b", "m"]
|
||||||
self.high_numwords = self.gen_high_numwords([], [], lows)
|
self.high_numwords = self.gen_high_numwords([], [], lows)
|
||||||
@@ -81,7 +81,6 @@ class Num2Word_ES(Num2Word_EU):
|
|||||||
1e12 : "trillonésim",
|
1e12 : "trillonésim",
|
||||||
1e15 : "cuadrillonésim" }
|
1e15 : "cuadrillonésim" }
|
||||||
|
|
||||||
|
|
||||||
def merge(self, curr, next):
|
def merge(self, curr, next):
|
||||||
ctext, cnum, ntext, nnum = curr + next
|
ctext, cnum, ntext, nnum = curr + next
|
||||||
|
|
||||||
@@ -94,8 +93,8 @@ class Num2Word_ES(Num2Word_EU):
|
|||||||
|
|
||||||
if nnum < cnum:
|
if nnum < cnum:
|
||||||
if cnum < 100:
|
if cnum < 100:
|
||||||
return ("%s y %s"%(ctext, ntext), cnum + nnum)
|
return "%s y %s"%(ctext, ntext), cnum + nnum
|
||||||
return ("%s %s"%(ctext, ntext), cnum + nnum)
|
return "%s %s"%(ctext, ntext), cnum + nnum
|
||||||
elif (not nnum % 1000000) and cnum > 1:
|
elif (not nnum % 1000000) and cnum > 1:
|
||||||
ntext = ntext[:-3] + "lones"
|
ntext = ntext[:-3] + "lones"
|
||||||
|
|
||||||
@@ -113,7 +112,6 @@ class Num2Word_ES(Num2Word_EU):
|
|||||||
|
|
||||||
return (ctext + ntext, cnum * nnum)
|
return (ctext + ntext, cnum * nnum)
|
||||||
|
|
||||||
|
|
||||||
def to_ordinal(self, value):
|
def to_ordinal(self, value):
|
||||||
self.verify_ordinal(value)
|
self.verify_ordinal(value)
|
||||||
text = ""
|
text = ""
|
||||||
@@ -125,10 +123,10 @@ class Num2Word_ES(Num2Word_EU):
|
|||||||
elif value <= 12:
|
elif value <= 12:
|
||||||
text = "%s%s%s" % (self.ords[10], self.gender_stem, self.to_ordinal(value - 10))
|
text = "%s%s%s" % (self.ords[10], self.gender_stem, self.to_ordinal(value - 10))
|
||||||
elif value <= 100:
|
elif value <= 100:
|
||||||
dec = (value / 10) * 10
|
dec = (value // 10) * 10
|
||||||
text = "%s%s %s" % (self.ords[dec], self.gender_stem, self.to_ordinal(value - dec))
|
text = "%s%s %s" % (self.ords[dec], self.gender_stem, self.to_ordinal(value - dec))
|
||||||
elif value <= 1e3:
|
elif value <= 1e3:
|
||||||
cen = (value / 100) * 100
|
cen = (value // 100) * 100
|
||||||
text = "%s%s %s" % (self.ords[cen], self.gender_stem, self.to_ordinal(value - cen))
|
text = "%s%s %s" % (self.ords[cen], self.gender_stem, self.to_ordinal(value - cen))
|
||||||
elif value < 1e18:
|
elif value < 1e18:
|
||||||
# dec contains the following:
|
# dec contains the following:
|
||||||
@@ -151,7 +149,6 @@ class Num2Word_ES(Num2Word_EU):
|
|||||||
self.verify_ordinal(value)
|
self.verify_ordinal(value)
|
||||||
return "%s%s" % (value, "º" if self.gender_stem == 'o' else "ª")
|
return "%s%s" % (value, "º" if self.gender_stem == 'o' else "ª")
|
||||||
|
|
||||||
|
|
||||||
def to_currency(self, val, longval=True, old=False):
|
def to_currency(self, val, longval=True, old=False):
|
||||||
if old:
|
if old:
|
||||||
return self.to_splitnum(val, hightxt="peso/s", lowtxt="peseta/s",
|
return self.to_splitnum(val, hightxt="peso/s", lowtxt="peseta/s",
|
||||||
@@ -165,17 +162,18 @@ to_card = n2w.to_cardinal
|
|||||||
to_ord = n2w.to_ordinal
|
to_ord = n2w.to_ordinal
|
||||||
to_ordnum = n2w.to_ordinal_num
|
to_ordnum = n2w.to_ordinal_num
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
for val in [ 1, 11, 12, 21, 31, 33, 71, 80, 81, 91, 99, 100, 101, 102, 155,
|
for val in [1, 11, 12, 21, 31, 33, 71, 80, 81, 91, 99, 100, 101, 102, 155,
|
||||||
180, 300, 308, 832, 1000, 1001, 1061, 1100, 1500, 1701, 3000,
|
180, 300, 308, 832, 1000, 1001, 1061, 1100, 1500, 1701, 3000,
|
||||||
8280, 8291, 150000, 500000, 1000000, 2000000, 2000001,
|
8280, 8291, 150000, 500000, 1000000, 2000000, 2000001,
|
||||||
-21212121211221211111, -2.121212, -1.0000100]:
|
-21212121211221211111, -2.121212, -1.0000100]:
|
||||||
n2w.test(val)
|
n2w.test(val)
|
||||||
|
|
||||||
n2w.test(1325325436067876801768700107601001012212132143210473207540327057320957032975032975093275093275093270957329057320975093272950730)
|
n2w.test(1325325436067876801768700107601001012212132143210473207540327057320957032975032975093275093275093270957329057320975093272950730)
|
||||||
print n2w.to_currency(1222)
|
print(n2w.to_currency(1222))
|
||||||
print n2w.to_currency(1222, old=True)
|
print(n2w.to_currency(1222, old=True))
|
||||||
print n2w.to_year(1222)
|
print(n2w.to_year(1222))
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|||||||
50
num2words/lang_ES_CO.py
Normal file
50
num2words/lang_ES_CO.py
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
# 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, print_function
|
||||||
|
from .lang_ES import Num2Word_ES
|
||||||
|
|
||||||
|
|
||||||
|
class Num2Word_ES_CO(Num2Word_ES):
|
||||||
|
|
||||||
|
def to_currency(self, val, longval=True, old=False):
|
||||||
|
return self.to_splitnum(val, hightxt="peso/s", lowtxt="peso/s",
|
||||||
|
divisor=1000, jointxt="y", longval=longval)
|
||||||
|
|
||||||
|
|
||||||
|
n2w = Num2Word_ES_CO()
|
||||||
|
to_card = n2w.to_cardinal
|
||||||
|
to_ord = n2w.to_ordinal
|
||||||
|
to_ordnum = n2w.to_ordinal_num
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
for val in [1, 11, 12, 21, 31, 33, 71, 80, 81, 91, 99, 100, 101, 102, 155,
|
||||||
|
180, 300, 308, 832, 1000, 1001, 1061, 1100, 1500, 1701, 3000,
|
||||||
|
8280, 8291, 150000, 500000, 1000000, 2000000, 2000001,
|
||||||
|
-21212121211221211111, -2.121212, -1.0000100]:
|
||||||
|
n2w.test(val)
|
||||||
|
|
||||||
|
n2w.test(1325325436067876801768700107601001012212132143210473207540327057320957032975032975093275093275093270957329057320975093272950730)
|
||||||
|
print(n2w.to_currency(1222))
|
||||||
|
print(n2w.to_currency(1222, old=True))
|
||||||
|
print(n2w.to_year(1222))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
49
num2words/lang_ES_VE.py
Normal file
49
num2words/lang_ES_VE.py
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
# 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, print_function
|
||||||
|
from .lang_ES import Num2Word_ES
|
||||||
|
|
||||||
|
|
||||||
|
class Num2Word_ES_VE(Num2Word_ES):
|
||||||
|
|
||||||
|
def to_currency(self, val, longval=True, old=False):
|
||||||
|
return self.to_splitnum(val, hightxt="bolívar/es Fuerte/s", lowtxt="bolívar/es fuerte/s",
|
||||||
|
divisor=1000, jointxt="y", longval=longval)
|
||||||
|
|
||||||
|
n2w = Num2Word_ES_VE()
|
||||||
|
to_card = n2w.to_cardinal
|
||||||
|
to_ord = n2w.to_ordinal
|
||||||
|
to_ordnum = n2w.to_ordinal_num
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
for val in [1, 11, 12, 21, 31, 33, 71, 80, 81, 91, 99, 100, 101, 102, 155,
|
||||||
|
180, 300, 308, 832, 1000, 1001, 1061, 1100, 1500, 1701, 3000,
|
||||||
|
8280, 8291, 150000, 500000, 1000000, 2000000, 2000001,
|
||||||
|
-21212121211221211111, -2.121212, -1.0000100]:
|
||||||
|
n2w.test(val)
|
||||||
|
|
||||||
|
n2w.test(1325325436067876801768700107601001012212132143210473207540327057320957032975032975093275093275093270957329057320975093272950730)
|
||||||
|
print(n2w.to_currency(1222))
|
||||||
|
print(n2w.to_currency(1222, old=True))
|
||||||
|
print(n2w.to_year(1222))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
@@ -15,16 +15,18 @@
|
|||||||
# 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, print_function
|
||||||
from .lang_EU import Num2Word_EU
|
from .lang_EU import Num2Word_EU
|
||||||
|
|
||||||
#//TODO: error messages in French
|
|
||||||
class Num2Word_FR(Num2Word_EU):
|
class Num2Word_FR(Num2Word_EU):
|
||||||
def setup(self):
|
def setup(self):
|
||||||
|
Num2Word_EU.setup(self)
|
||||||
|
|
||||||
self.negword = "moins "
|
self.negword = "moins "
|
||||||
self.pointword = "virgule"
|
self.pointword = "virgule"
|
||||||
self.errmsg_nonnum = "Only numbers may be converted to words."
|
self.errmsg_nonnum = u"Seulement des nombres peuvent être convertis en mots."
|
||||||
self.errmsg_toobig = "Number is too large to convert to words."
|
self.errmsg_toobig = u"Nombre trop grand pour être converti en mots."
|
||||||
self.exclude_title = ["et", "virgule", "moins"]
|
self.exclude_title = ["et", "virgule", "moins"]
|
||||||
self.mid_numwords = [(1000, "mille"), (100, "cent"),
|
self.mid_numwords = [(1000, "mille"), (100, "cent"),
|
||||||
(80, "quatre-vingts"), (60, "soixante"),
|
(80, "quatre-vingts"), (60, "soixante"),
|
||||||
@@ -49,15 +51,14 @@ class Num2Word_FR(Num2Word_EU):
|
|||||||
else:
|
else:
|
||||||
if (not (cnum - 80)%100 or not cnum%100) and ctext[-1] == "s":
|
if (not (cnum - 80)%100 or not cnum%100) and ctext[-1] == "s":
|
||||||
ctext = ctext[:-1]
|
ctext = ctext[:-1]
|
||||||
if (cnum<1000 and nnum != 1000 and ntext[-1] != "s"
|
if cnum < 1000 and nnum != 1000 and ntext[-1] != "s" and not nnum % 100:
|
||||||
and not nnum%100):
|
|
||||||
ntext += "s"
|
ntext += "s"
|
||||||
|
|
||||||
if nnum < cnum < 100:
|
if nnum < cnum < 100:
|
||||||
if nnum % 10 == 1 and cnum != 80:
|
if nnum % 10 == 1 and cnum != 80:
|
||||||
return ("%s et %s"%(ctext, ntext), cnum + nnum)
|
return ("%s et %s"%(ctext, ntext), cnum + nnum)
|
||||||
return ("%s-%s"%(ctext, ntext), cnum + nnum)
|
return ("%s-%s"%(ctext, ntext), cnum + nnum)
|
||||||
elif nnum > cnum:
|
if nnum > cnum:
|
||||||
return ("%s %s"%(ctext, ntext), cnum * nnum)
|
return ("%s %s"%(ctext, ntext), cnum * nnum)
|
||||||
return ("%s %s"%(ctext, ntext), cnum + nnum)
|
return ("%s %s"%(ctext, ntext), cnum + nnum)
|
||||||
|
|
||||||
@@ -83,15 +84,15 @@ class Num2Word_FR(Num2Word_EU):
|
|||||||
def to_ordinal_num(self, value):
|
def to_ordinal_num(self, value):
|
||||||
self.verify_ordinal(value)
|
self.verify_ordinal(value)
|
||||||
out = str(value)
|
out = str(value)
|
||||||
out += {"1" : "er" }.get(out[-1], "me")
|
out += {"1" : "er"}.get(out[-1], "me")
|
||||||
return out
|
return out
|
||||||
|
|
||||||
def to_currency(self, val, longval=True, old=False):
|
def to_currency(self, val, longval=True, old=False):
|
||||||
hightxt = "Euro/s"
|
hightxt = "Euro/s"
|
||||||
if old:
|
if old:
|
||||||
hightxt="franc/s"
|
hightxt = "franc/s"
|
||||||
return self.to_splitnum(val, hightxt=hightxt, lowtxt="centime/s",
|
return self.to_splitnum(val, hightxt=hightxt, lowtxt="centime/s",
|
||||||
jointxt="et",longval=longval)
|
jointxt="et", longval=longval)
|
||||||
|
|
||||||
n2w = Num2Word_FR()
|
n2w = Num2Word_FR()
|
||||||
to_card = n2w.to_cardinal
|
to_card = n2w.to_cardinal
|
||||||
@@ -99,15 +100,15 @@ to_ord = n2w.to_ordinal
|
|||||||
to_ordnum = n2w.to_ordinal_num
|
to_ordnum = n2w.to_ordinal_num
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
for val in [ 1, 11, 12, 21, 31, 33, 71, 80, 81, 91, 99, 100, 101, 102, 155,
|
for val in [1, 11, 12, 21, 31, 33, 71, 80, 81, 91, 99, 100, 101, 102, 155,
|
||||||
180, 300, 308, 832, 1000, 1001, 1061, 1100, 1500, 1701, 3000,
|
180, 300, 308, 832, 1000, 1001, 1061, 1100, 1500, 1701, 3000,
|
||||||
8280, 8291, 150000, 500000, 1000000, 2000000, 2000001,
|
8280, 8291, 150000, 500000, 1000000, 2000000, 2000001,
|
||||||
-21212121211221211111, -2.121212, -1.0000100]:
|
-21212121211221211111, -2.121212, -1.0000100]:
|
||||||
n2w.test(val)
|
n2w.test(val)
|
||||||
|
|
||||||
n2w.test(1325325436067876801768700107601001012212132143210473207540327057320957032975032975093275093275093270957329057320975093272950730)
|
n2w.test(1325325436067876801768700107601001012212132143210473207540327057320957032975032975093275093275093270957329057320975093272950730)
|
||||||
print n2w.to_currency(112121)
|
print(n2w.to_currency(112121))
|
||||||
print n2w.to_year(1996)
|
print(n2w.to_year(1996))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
69
num2words/lang_FR_CH.py
Normal file
69
num2words/lang_FR_CH.py
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
# -*- 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, print_function
|
||||||
|
from .lang_FR import Num2Word_FR
|
||||||
|
|
||||||
|
|
||||||
|
class Num2Word_FR_CH(Num2Word_FR):
|
||||||
|
def setup(self):
|
||||||
|
Num2Word_FR.setup(self)
|
||||||
|
|
||||||
|
self.mid_numwords = [(1000, "mille"), (100, "cent"), (90, "nonante"),
|
||||||
|
(80, "huitante"), (70, "septante"), (60, "soixante"),
|
||||||
|
(50, "cinquante"), (40, "quarante"),
|
||||||
|
(30, "trente")]
|
||||||
|
|
||||||
|
|
||||||
|
def merge(self, curr, next):
|
||||||
|
ctext, cnum, ntext, nnum = curr + next
|
||||||
|
|
||||||
|
if cnum == 1:
|
||||||
|
if nnum < 1000000:
|
||||||
|
return next
|
||||||
|
|
||||||
|
if cnum < 1000 and nnum != 1000 and ntext[-1] != "s" and not nnum % 100:
|
||||||
|
ntext += "s"
|
||||||
|
|
||||||
|
if nnum < cnum < 100:
|
||||||
|
if nnum % 10 == 1:
|
||||||
|
return ("%s et %s"%(ctext, ntext), cnum + nnum)
|
||||||
|
return ("%s-%s"%(ctext, ntext), cnum + nnum)
|
||||||
|
if nnum > cnum:
|
||||||
|
return ("%s %s"%(ctext, ntext), cnum * nnum)
|
||||||
|
return ("%s %s"%(ctext, ntext), cnum + nnum)
|
||||||
|
|
||||||
|
|
||||||
|
n2w = Num2Word_FR_CH()
|
||||||
|
to_card = n2w.to_cardinal
|
||||||
|
to_ord = n2w.to_ordinal
|
||||||
|
to_ordnum = n2w.to_ordinal_num
|
||||||
|
|
||||||
|
def main():
|
||||||
|
for val in [1, 11, 12, 21, 31, 33, 71, 80, 81, 91, 99, 100, 101, 102, 155,
|
||||||
|
180, 300, 308, 832, 1000, 1001, 1061, 1100, 1500, 1701, 3000,
|
||||||
|
8280, 8291, 150000, 500000, 1000000, 2000000, 2000001,
|
||||||
|
-21212121211221211111, -2.121212, -1.0000100]:
|
||||||
|
n2w.test(val)
|
||||||
|
|
||||||
|
n2w.test(1325325436067876801768700107601001012212132143210473207540327057320957032975032975093275093275093270957329057320975093272950730)
|
||||||
|
print(n2w.to_currency(112121))
|
||||||
|
print(n2w.to_year(1996))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
48
num2words/lang_FR_DZ.py
Normal file
48
num2words/lang_FR_DZ.py
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
# 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 .lang_FR import Num2Word_FR
|
||||||
|
|
||||||
|
|
||||||
|
class Num2Word_FR_DZ(Num2Word_FR):
|
||||||
|
def to_currency(self, val, longval=True, cents=True, jointxt="virgule"):
|
||||||
|
return self.to_splitnum(val, hightxt="dinard/s", lowtxt="centime/s",
|
||||||
|
jointxt=jointxt, longval=longval, cents=cents)
|
||||||
|
|
||||||
|
|
||||||
|
n2w = Num2Word_FR_DZ()
|
||||||
|
to_card = n2w.to_cardinal
|
||||||
|
to_ord = n2w.to_ordinal
|
||||||
|
to_ordnum = n2w.to_ordinal_num
|
||||||
|
to_year = n2w.to_year
|
||||||
|
to_currency = n2w.to_currency
|
||||||
|
|
||||||
|
def main():
|
||||||
|
for val in [1, 11, 12, 21, 31, 33, 71, 80, 81, 91, 99, 100, 101, 102, 155,
|
||||||
|
180, 300, 308, 832, 1000, 1001, 1061, 1100, 1500, 1701, 3000,
|
||||||
|
8280, 8291, 150000, 500000, 1000000, 2000000, 2000001,
|
||||||
|
-21212121211221211111, -2.121212, -1.0000100]:
|
||||||
|
n2w.test(val)
|
||||||
|
|
||||||
|
n2w.test(1325325436067876801768700107601001012212132143210473207540327057320957032975032975093275093275093270957329057320975093272950730)
|
||||||
|
for val in [1, 120, 1000, 1120, 1800, 1976, 2000, 2010, 2099, 2171]:
|
||||||
|
print(val, "is", n2w.to_currency(val))
|
||||||
|
print(val, "is", n2w.to_year(val))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
162
num2words/lang_HE.py
Normal file
162
num2words/lang_HE.py
Normal file
@@ -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, print_function
|
||||||
|
|
||||||
|
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))
|
||||||
|
|
||||||
196
num2words/lang_ID.py
Normal file
196
num2words/lang_ID.py
Normal file
@@ -0,0 +1,196 @@
|
|||||||
|
# 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, print_function
|
||||||
|
|
||||||
|
class Num2Word_ID():
|
||||||
|
|
||||||
|
BASE = {0: [],
|
||||||
|
1: ["satu"],
|
||||||
|
2: ["dua"],
|
||||||
|
3: ["tiga"],
|
||||||
|
4: ["empat"],
|
||||||
|
5: ["lima"],
|
||||||
|
6: ["enam"],
|
||||||
|
7: ["tujuh"],
|
||||||
|
8: ["delapan"],
|
||||||
|
9: ["sembilan"]}
|
||||||
|
|
||||||
|
TENS_TO = {3: "ribu",
|
||||||
|
6: "juta",
|
||||||
|
9: "miliar",
|
||||||
|
12: "triliun",
|
||||||
|
15: "kuadriliun",
|
||||||
|
18: "kuantiliun",
|
||||||
|
21: "sekstiliun",
|
||||||
|
24: "septiliun",
|
||||||
|
27: "oktiliun",
|
||||||
|
30: "noniliun",
|
||||||
|
33: "desiliun"}
|
||||||
|
|
||||||
|
errmsg_floatord = "Cannot treat float number as ordinal"
|
||||||
|
errmsg_negord = "Cannot treat negative number as ordinal"
|
||||||
|
errmsg_toobig = "Too large"
|
||||||
|
max_num = 10**36
|
||||||
|
|
||||||
|
def split_by_koma(self, number):
|
||||||
|
return str(number).split('.')
|
||||||
|
|
||||||
|
def split_by_3(self, number):
|
||||||
|
"""
|
||||||
|
starting here, it groups the number by three from the tail
|
||||||
|
'1234567' -> (('1',),('234',),('567',))
|
||||||
|
:param number:str
|
||||||
|
:rtype:tuple
|
||||||
|
"""
|
||||||
|
blocks = ()
|
||||||
|
length = len(number)
|
||||||
|
|
||||||
|
if length < 3:
|
||||||
|
blocks += ((number,),)
|
||||||
|
else:
|
||||||
|
len_of_first_block = length % 3
|
||||||
|
|
||||||
|
if len_of_first_block > 0:
|
||||||
|
first_block = number[0:len_of_first_block],
|
||||||
|
blocks += first_block,
|
||||||
|
|
||||||
|
for i in range(len_of_first_block, length, 3):
|
||||||
|
next_block = (number[i:i+3],),
|
||||||
|
blocks += next_block
|
||||||
|
|
||||||
|
return blocks
|
||||||
|
|
||||||
|
def spell(self, blocks):
|
||||||
|
"""
|
||||||
|
it adds the list of spelling to the blocks
|
||||||
|
(('1',),('034',)) -> (('1',['satu']),('234',['tiga', 'puluh', 'empat']))
|
||||||
|
:param blocks: tuple
|
||||||
|
:rtype: tuple
|
||||||
|
"""
|
||||||
|
word_blocks = ()
|
||||||
|
first_block = blocks[0]
|
||||||
|
if len(first_block[0]) == 1:
|
||||||
|
if first_block[0] == '0':
|
||||||
|
spelling = ['nol']
|
||||||
|
else:
|
||||||
|
spelling = self.BASE[int(first_block[0])]
|
||||||
|
elif len(first_block[0]) == 2:
|
||||||
|
spelling = self.puluh(first_block[0])
|
||||||
|
else:
|
||||||
|
spelling = self.ratus(first_block[0][0]) + self.puluh(first_block[0][1:3])
|
||||||
|
|
||||||
|
word_blocks += (first_block[0], spelling),
|
||||||
|
|
||||||
|
for block in blocks[1:]:
|
||||||
|
spelling = self.ratus(block[0][0]) + self.puluh(block[0][1:3])
|
||||||
|
block += spelling,
|
||||||
|
word_blocks += block,
|
||||||
|
|
||||||
|
return word_blocks
|
||||||
|
|
||||||
|
def ratus(self, number):
|
||||||
|
# it is used to spell
|
||||||
|
if number == '1':
|
||||||
|
return ['seratus']
|
||||||
|
elif number == '0':
|
||||||
|
return []
|
||||||
|
else:
|
||||||
|
return self.BASE[int(number)]+['ratus']
|
||||||
|
|
||||||
|
def puluh(self, number):
|
||||||
|
# it is used to spell
|
||||||
|
if number[0] == '1':
|
||||||
|
if number[1]== '0':
|
||||||
|
return ['sepuluh']
|
||||||
|
elif number[1] == '1':
|
||||||
|
return ['sebelas']
|
||||||
|
else:
|
||||||
|
return self.BASE[int(number[1])]+['belas']
|
||||||
|
elif number[0] == '0':
|
||||||
|
return self.BASE[int(number[1])]
|
||||||
|
else:
|
||||||
|
return self.BASE[int(number[0])]+['puluh']+ self.BASE[int(number[1])]
|
||||||
|
|
||||||
|
def spell_float(self, float_part):
|
||||||
|
# spell the float number
|
||||||
|
word_list = []
|
||||||
|
for n in float_part:
|
||||||
|
if n == '0':
|
||||||
|
word_list += ['nol']
|
||||||
|
continue
|
||||||
|
word_list += self.BASE[int(n)]
|
||||||
|
return ' '.join(['','koma']+word_list)
|
||||||
|
|
||||||
|
def join(self, word_blocks, float_part):
|
||||||
|
"""
|
||||||
|
join the words by first join lists in the tuple
|
||||||
|
:param word_blocks: tuple
|
||||||
|
:rtype: str
|
||||||
|
"""
|
||||||
|
word_list = []
|
||||||
|
length = len(word_blocks)-1
|
||||||
|
first_block = word_blocks[0],
|
||||||
|
start = 0
|
||||||
|
|
||||||
|
if length == 1 and first_block[0][0] == '1':
|
||||||
|
word_list += ['seribu']
|
||||||
|
start = 1
|
||||||
|
|
||||||
|
for i in range(start, length+1, 1):
|
||||||
|
word_list += word_blocks[i][1]
|
||||||
|
if not word_blocks[i][1]:
|
||||||
|
continue
|
||||||
|
if i == length:
|
||||||
|
break
|
||||||
|
word_list += [self.TENS_TO[(length-i)*3]]
|
||||||
|
|
||||||
|
return ' '.join(word_list)+float_part
|
||||||
|
|
||||||
|
def to_cardinal(self, number):
|
||||||
|
if number >= self.max_num:
|
||||||
|
raise OverflowError(self.errmsg_toobig % (number, self.maxnum))
|
||||||
|
minus = ''
|
||||||
|
if number < 0:
|
||||||
|
minus = 'min '
|
||||||
|
float_word = ''
|
||||||
|
n = self.split_by_koma(abs(number))
|
||||||
|
if len(n)==2:
|
||||||
|
float_word = self.spell_float(n[1])
|
||||||
|
return minus + self.join(self.spell(self.split_by_3(n[0])), float_word)
|
||||||
|
|
||||||
|
def to_ordinal(self, number):
|
||||||
|
self.verify_ordinal(number)
|
||||||
|
out_word = self.to_cardinal(number)
|
||||||
|
if out_word == "satu":
|
||||||
|
return "pertama"
|
||||||
|
return "ke" + out_word
|
||||||
|
|
||||||
|
def to_ordinal_num(self, number):
|
||||||
|
self.verify_ordinal(number)
|
||||||
|
return "ke-" + str(number)
|
||||||
|
|
||||||
|
def to_currency(self, value):
|
||||||
|
return self.to_cardinal(value)+" rupiah"
|
||||||
|
|
||||||
|
def to_year(self, value):
|
||||||
|
return self.to_cardinal(value)
|
||||||
|
|
||||||
|
def verify_ordinal(self, value):
|
||||||
|
if not value == int(value):
|
||||||
|
raise TypeError(self.errmsg_floatord % value)
|
||||||
|
if not abs(value) == value:
|
||||||
|
raise TypeError(self.errmsg_negord % value)
|
||||||
202
num2words/lang_IT.py
Normal file
202
num2words/lang_IT.py
Normal file
@@ -0,0 +1,202 @@
|
|||||||
|
# -*- encoding: utf-8 -*-
|
||||||
|
#
|
||||||
|
# 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 .lang_EU import Num2Word_EU
|
||||||
|
|
||||||
|
# Globals
|
||||||
|
# -------
|
||||||
|
|
||||||
|
ZERO = "zero"
|
||||||
|
|
||||||
|
CARDINAL_WORDS = [
|
||||||
|
ZERO, "uno", "due", "tre", "quattro", "cinque", "sei", "sette", "otto",
|
||||||
|
"nove", "dieci", "undici", "dodici", "tredici", "quattordici", "quindici",
|
||||||
|
"sedici", "diciassette", "diciotto", "diciannove"
|
||||||
|
]
|
||||||
|
|
||||||
|
ORDINAL_WORDS = [
|
||||||
|
ZERO, "primo", "secondo", "terzo", "quarto", "quinto", "sesto", "settimo",
|
||||||
|
"ottavo", "nono", "decimo", "undicesimo", "dodicesimo", "tredicesimo",
|
||||||
|
"quattordicesimo", "quindicesimo", "sedicesimo", "diciassettesimo",
|
||||||
|
"diciottesimo", "diciannovesimo"
|
||||||
|
]
|
||||||
|
|
||||||
|
# The script can extrapolate the missing numbers from the base forms.
|
||||||
|
STR_TENS = {2: "venti", 3: "trenta", 4: "quaranta", 6: "sessanta"}
|
||||||
|
|
||||||
|
# These prefixes are used for extremely big numbers.
|
||||||
|
EXPONENT_PREFIXES = [
|
||||||
|
ZERO, "m", "b", "tr", "quadr", "quint", "sest", "sett", "ott", "nov", "dec"
|
||||||
|
]
|
||||||
|
|
||||||
|
# Utils
|
||||||
|
# =====
|
||||||
|
|
||||||
|
def phonetic_contraction(string):
|
||||||
|
return (string
|
||||||
|
.replace("oo", "o") # ex. "centootto"
|
||||||
|
.replace("ao", "o") # ex. "settantaotto"
|
||||||
|
.replace("io", "o") # ex. "ventiotto"
|
||||||
|
.replace("au", "u") # ex. "trentauno"
|
||||||
|
)
|
||||||
|
|
||||||
|
def exponent_length_to_string(exponent_length):
|
||||||
|
# We always assume `exponent` to be a multiple of 3. If it's not true, then
|
||||||
|
# Num2Word_IT.big_number_to_cardinal did something wrong.
|
||||||
|
prefix = EXPONENT_PREFIXES[exponent_length // 6]
|
||||||
|
if exponent_length % 6 == 0:
|
||||||
|
return prefix + "ilione"
|
||||||
|
else:
|
||||||
|
return prefix + "iliardo"
|
||||||
|
|
||||||
|
def accentuate(string):
|
||||||
|
# This is inefficient: it may do several rewritings when deleting
|
||||||
|
# half-sentence accents. However, it is the easiest method and speed is
|
||||||
|
# not crucial (duh), so...
|
||||||
|
return " ".join(
|
||||||
|
# Deletes half-sentence accents and accentuates the last "tre"
|
||||||
|
[w.replace("tré", "tre")[:-3] + "tré"
|
||||||
|
# We shouldn't accentuate a single "tre": is has to be a composite
|
||||||
|
# word. ~~~~~~~~~~
|
||||||
|
if w[-3:] == "tre" and len(w) > 3
|
||||||
|
# Deletes half-sentence accents anyway
|
||||||
|
# ~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
else w.replace("tré", "tre")
|
||||||
|
for w in string.split()
|
||||||
|
])
|
||||||
|
|
||||||
|
def omitt_if_zero(number_to_string):
|
||||||
|
return "" if number_to_string == ZERO else number_to_string
|
||||||
|
|
||||||
|
# Main class
|
||||||
|
# ==========
|
||||||
|
|
||||||
|
class Num2Word_IT:
|
||||||
|
|
||||||
|
MINUS_PREFIX_WORD = "meno "
|
||||||
|
FLOAT_INFIX_WORD = " virgola "
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def float_to_words(self, float_number, ordinal=False):
|
||||||
|
if ordinal:
|
||||||
|
prefix = self.to_ordinal(int(float_number))
|
||||||
|
else:
|
||||||
|
prefix = self.to_cardinal(int(float_number))
|
||||||
|
postfix = " ".join(
|
||||||
|
# Drops the trailing zero and comma ~~~~
|
||||||
|
[self.to_cardinal(int(c)) for c in str(float_number % 1)[2:]]
|
||||||
|
)
|
||||||
|
return prefix + Num2Word_IT.FLOAT_INFIX_WORD + postfix
|
||||||
|
|
||||||
|
def tens_to_cardinal(self, number):
|
||||||
|
tens = number // 10
|
||||||
|
units = number % 10
|
||||||
|
if tens in STR_TENS:
|
||||||
|
prefix = STR_TENS[tens]
|
||||||
|
else:
|
||||||
|
prefix = CARDINAL_WORDS[tens][:-1] + "anta"
|
||||||
|
postfix = omitt_if_zero(CARDINAL_WORDS[units])
|
||||||
|
return phonetic_contraction(prefix + postfix)
|
||||||
|
|
||||||
|
def hundreds_to_cardinal(self, number):
|
||||||
|
hundreds = number // 100
|
||||||
|
prefix = "cento"
|
||||||
|
if hundreds != 1:
|
||||||
|
prefix = CARDINAL_WORDS[hundreds] + prefix
|
||||||
|
postfix = omitt_if_zero(self.to_cardinal(number % 100))
|
||||||
|
return phonetic_contraction(prefix + postfix)
|
||||||
|
|
||||||
|
def thousands_to_cardinal(self, number):
|
||||||
|
thousands = number // 1000
|
||||||
|
if thousands == 1:
|
||||||
|
prefix = "mille"
|
||||||
|
else:
|
||||||
|
prefix = self.to_cardinal(thousands) + "mila"
|
||||||
|
postfix = omitt_if_zero(self.to_cardinal(number % 1000))
|
||||||
|
# "mille" and "mila" don't need any phonetic contractions
|
||||||
|
return prefix + postfix
|
||||||
|
|
||||||
|
def big_number_to_cardinal(self, number):
|
||||||
|
digits = [c for c in str(number)]
|
||||||
|
length = len(digits)
|
||||||
|
if length >= 66:
|
||||||
|
raise NotImplementedError("The given number is too large.")
|
||||||
|
# This is how many digits come before the "illion" term.
|
||||||
|
# cento miliardi => 3
|
||||||
|
# dieci milioni => 2
|
||||||
|
# un miliardo => 1
|
||||||
|
predigits = length % 3 or 3
|
||||||
|
multiplier = digits[:predigits]
|
||||||
|
exponent = digits[predigits:]
|
||||||
|
# Default infix string: "milione", "biliardo", "sestilione", ecc.
|
||||||
|
infix = exponent_length_to_string(len(exponent))
|
||||||
|
if multiplier == ["1"]:
|
||||||
|
prefix = "un "
|
||||||
|
else:
|
||||||
|
prefix = self.to_cardinal(int("".join(multiplier)))
|
||||||
|
# Plural form ~~~~~~~~~~~
|
||||||
|
infix = " " + infix[:-1] + "i"
|
||||||
|
# Read as: Does the value of exponent equal 0?
|
||||||
|
if set(exponent) != set("0"):
|
||||||
|
postfix = self.to_cardinal(int("".join(exponent)))
|
||||||
|
if " e " in postfix:
|
||||||
|
infix += ", "
|
||||||
|
else:
|
||||||
|
infix += " e "
|
||||||
|
else:
|
||||||
|
postfix = ""
|
||||||
|
return prefix + infix + postfix
|
||||||
|
|
||||||
|
def to_cardinal(self, number):
|
||||||
|
if number < 0:
|
||||||
|
string = Num2Word_IT.MINUS_PREFIX_WORD + self.to_cardinal(-number)
|
||||||
|
elif number % 1 != 0:
|
||||||
|
string = self.float_to_words(number)
|
||||||
|
elif number < 20:
|
||||||
|
string = CARDINAL_WORDS[number]
|
||||||
|
elif number < 100:
|
||||||
|
string = self.tens_to_cardinal(number)
|
||||||
|
elif number < 1000:
|
||||||
|
string = self.hundreds_to_cardinal(number)
|
||||||
|
elif number < 1000000:
|
||||||
|
string = self.thousands_to_cardinal(number)
|
||||||
|
else:
|
||||||
|
string = self.big_number_to_cardinal(number)
|
||||||
|
return accentuate(string)
|
||||||
|
|
||||||
|
def to_ordinal(self, number):
|
||||||
|
tens = number % 100
|
||||||
|
# Italian grammar is poorly defined here ¯\_(ツ)_/¯:
|
||||||
|
# centodecimo VS centodieciesimo VS centesimo decimo?
|
||||||
|
is_outside_teens = not 10 < tens < 20
|
||||||
|
if number < 0:
|
||||||
|
return Num2Word_IT.MINUS_PREFIX_WORD + self.to_ordinal(-number)
|
||||||
|
elif number % 1 != 0:
|
||||||
|
return self.float_to_words(number, ordinal=True)
|
||||||
|
elif number < 20:
|
||||||
|
return ORDINAL_WORDS[number]
|
||||||
|
elif is_outside_teens and tens % 10 == 3:
|
||||||
|
# Gets ride of the accent ~~~~~~~~~~
|
||||||
|
return self.to_cardinal(number)[:-1] + "eesimo"
|
||||||
|
elif is_outside_teens and tens % 10 == 6:
|
||||||
|
return self.to_cardinal(number) + "esimo"
|
||||||
|
else:
|
||||||
|
string = self.to_cardinal(number)[:-1]
|
||||||
|
if string[-3:] == "mil":
|
||||||
|
string += "l"
|
||||||
|
return string + "esimo"
|
||||||
@@ -85,7 +85,13 @@ vienas litas, nulis centų
|
|||||||
vienas tūkstantis du šimtai trisdešimt keturi litai, penkiasdešimt šeši centai
|
vienas tūkstantis du šimtai trisdešimt keturi litai, penkiasdešimt šeši centai
|
||||||
|
|
||||||
>>> print(to_currency(-1251985, cents = False))
|
>>> print(to_currency(-1251985, cents = False))
|
||||||
minus dvylika tūkstančių penki šimtai devyniolika litų, 85 centai
|
minus dvylika tūkstančių penki šimtai devyniolika eurų, 85 centai
|
||||||
|
|
||||||
|
>>> print(to_currency(1.0, 'EUR'))
|
||||||
|
vienas euras, nulis centų
|
||||||
|
|
||||||
|
>>> print(to_currency(1234.56, 'EUR'))
|
||||||
|
vienas tūkstantis du šimtai trisdešimt keturi eurai, penkiasdešimt šeši centai
|
||||||
"""
|
"""
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
@@ -144,6 +150,7 @@ THOUSANDS = {
|
|||||||
|
|
||||||
CURRENCIES = {
|
CURRENCIES = {
|
||||||
'LTL': ((u'litas', u'litai', u'litų'), (u'centas', u'centai', u'centų')),
|
'LTL': ((u'litas', u'litai', u'litų'), (u'centas', u'centai', u'centų')),
|
||||||
|
'EUR': ((u'euras', u'eurai', u'eurų'), (u'centas', u'centai', u'centų')),
|
||||||
}
|
}
|
||||||
|
|
||||||
def splitby3(n):
|
def splitby3(n):
|
||||||
@@ -210,7 +217,7 @@ def n2w(n):
|
|||||||
else:
|
else:
|
||||||
return int2word(int(n))
|
return int2word(int(n))
|
||||||
|
|
||||||
def to_currency(n, currency='LTL', cents = True):
|
def to_currency(n, currency='EUR', cents = True):
|
||||||
if type(n) == int:
|
if type(n) == int:
|
||||||
if n < 0:
|
if n < 0:
|
||||||
minus = True
|
minus = True
|
||||||
|
|||||||
154
num2words/lang_NL.py
Normal file
154
num2words/lang_NL.py
Normal file
@@ -0,0 +1,154 @@
|
|||||||
|
# -*- 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, print_function
|
||||||
|
from .lang_EU import Num2Word_EU
|
||||||
|
|
||||||
|
class Num2Word_NL(Num2Word_EU):
|
||||||
|
def set_high_numwords(self, high):
|
||||||
|
max = 3 + 6*len(high)
|
||||||
|
|
||||||
|
for word, n in zip(high, range(max, 3, -6)):
|
||||||
|
self.cards[10**n] = word + "iljard"
|
||||||
|
self.cards[10**(n-3)] = word + "iljoen"
|
||||||
|
|
||||||
|
def setup(self):
|
||||||
|
self.negword = "min "
|
||||||
|
self.pointword = "komma"
|
||||||
|
self.errmsg_floatord = "Het zwevende puntnummer %s kan niet omgezet worden naar een ordernummer." # "Cannot treat float %s as ordinal."
|
||||||
|
self.errmsg_nonnum = "Alleen nummers (type (%s)) kunnen naar woorden omgezet worden." # "type(((type(%s)) ) not in [long, int, float]"
|
||||||
|
self.errmsg_negord = "Het negatieve getal %s kan niet omgezet worden naar een ordernummer." # "Cannot treat negative num %s as ordinal."
|
||||||
|
self.errmsg_toobig = "Het getal %s moet minder zijn dan %s." # "abs(%s) must be less than %s."
|
||||||
|
self.exclude_title = []
|
||||||
|
|
||||||
|
lows = ["non", "okt", "sept", "sext", "quint", "quadr", "tr", "b", "m"]
|
||||||
|
units = ["", "un", "duo", "tre", "quattuor", "quin", "sex", "sept",
|
||||||
|
"okto", "novem"]
|
||||||
|
tens = ["dez", "vigint", "trigint", "quadragint", "quinquagint",
|
||||||
|
"sexagint", "septuagint", "oktogint", "nonagint"]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
self.high_numwords = ["zend"]+self.gen_high_numwords(units, tens, lows)
|
||||||
|
self.mid_numwords = [(1000, "duizend"), (100, "honderd"),
|
||||||
|
(90, "negentig"), (80, "tachtig"), (70, "zeventig"),
|
||||||
|
(60, "zestig"), (50, "vijftig"), (40, "veertig"),
|
||||||
|
(30, "dertig")]
|
||||||
|
self.low_numwords = ["twintig", "negentien", "achttien", "zeventien",
|
||||||
|
"zestien", "vijftien", "veertien", "dertien",
|
||||||
|
"twaalf", "elf", "tien", "negen", "acht", "zeven",
|
||||||
|
"zes", "vijf", "vier", "drie", "twee", "één",
|
||||||
|
"nul"]
|
||||||
|
|
||||||
|
self.ords = {"één": "eerst",
|
||||||
|
"twee": "tweed",
|
||||||
|
"drie": "derd",
|
||||||
|
"vier": "vierd",
|
||||||
|
"vijf": "vijfd",
|
||||||
|
"zes": "zesd",
|
||||||
|
"zeven": "zevend",
|
||||||
|
"acht": "achtst",
|
||||||
|
"negen": "negend",
|
||||||
|
"tien":"tiend",
|
||||||
|
"elf":"elfd",
|
||||||
|
"twaalf":"twaalfd",
|
||||||
|
|
||||||
|
"ig": "igst",
|
||||||
|
"erd": "erdst",
|
||||||
|
"end": "endst",
|
||||||
|
"joen": "joenst",
|
||||||
|
"rd": "rdst"}
|
||||||
|
|
||||||
|
def merge(self, curr, next):
|
||||||
|
ctext, cnum, ntext, nnum = curr + next
|
||||||
|
|
||||||
|
if cnum == 1:
|
||||||
|
if nnum < 10**6:
|
||||||
|
return next
|
||||||
|
ctext = "een"
|
||||||
|
|
||||||
|
if nnum > cnum:
|
||||||
|
if nnum >= 10**6:
|
||||||
|
ctext += " "
|
||||||
|
val = cnum * nnum
|
||||||
|
else:
|
||||||
|
if nnum < 10 < cnum < 100:
|
||||||
|
if nnum == 1:
|
||||||
|
ntext = "een"
|
||||||
|
|
||||||
|
if ntext.endswith("e"):
|
||||||
|
ntext += "ën"#"n"
|
||||||
|
else:
|
||||||
|
ntext += "en"
|
||||||
|
ntext, ctext = ctext, ntext #+ "en"
|
||||||
|
elif cnum >= 10**6:
|
||||||
|
ctext += " "
|
||||||
|
val = cnum + nnum
|
||||||
|
|
||||||
|
word = ctext + ntext
|
||||||
|
return (word, val)
|
||||||
|
|
||||||
|
def to_ordinal(self, value):
|
||||||
|
self.verify_ordinal(value)
|
||||||
|
outword = self.to_cardinal(value)
|
||||||
|
for key in self.ords:
|
||||||
|
if outword.endswith(key):
|
||||||
|
outword = outword[:len(outword) - len(key)] + self.ords[key]
|
||||||
|
break
|
||||||
|
return outword + "e"
|
||||||
|
|
||||||
|
def to_ordinal_num(self, value):
|
||||||
|
self.verify_ordinal(value)
|
||||||
|
return str(value) + "."
|
||||||
|
|
||||||
|
def to_currency(self, val, longval=True, old=False):
|
||||||
|
if old:
|
||||||
|
return self.to_splitnum(val, hightxt="euro/s", lowtxt="cent/s",
|
||||||
|
jointxt="en",longval=longval)
|
||||||
|
return super(Num2Word_NL, self).to_currency(val, jointxt="en",
|
||||||
|
longval=longval)
|
||||||
|
|
||||||
|
def to_year(self, val, longval=True):
|
||||||
|
if not (val//100)%10:
|
||||||
|
return self.to_cardinal(val)
|
||||||
|
return self.to_splitnum(val, hightxt="honderd", longval=longval)
|
||||||
|
|
||||||
|
n2w = Num2Word_NL()
|
||||||
|
to_card = n2w.to_cardinal
|
||||||
|
to_ord = n2w.to_ordinal
|
||||||
|
to_ordnum = n2w.to_ordinal_num
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
for val in [1, 7, 8, 12, 17, 62,81, 91, 99, 100, 101, 102, 155,
|
||||||
|
180, 300, 308, 832, 1000, 1001, 1061,1062, 1100, 1500, 1701, 3000,
|
||||||
|
8280, 8291, 150000, 500000, 3000000, 1000000, 2000001, 1000000000, 2000000000,
|
||||||
|
-21212121211221211111, -2.121212, -1.0000100]:
|
||||||
|
n2w.test(val)
|
||||||
|
|
||||||
|
n2w.test(3000000)
|
||||||
|
n2w.test(3000000000001)
|
||||||
|
n2w.test(3000000324566)
|
||||||
|
print(n2w.to_currency(112121))
|
||||||
|
print(n2w.to_year(2000))
|
||||||
|
print(n2w.to_year(1820))
|
||||||
|
print(n2w.to_year(2001))
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
|
|
||||||
123
num2words/lang_NO.py
Normal file
123
num2words/lang_NO.py
Normal file
@@ -0,0 +1,123 @@
|
|||||||
|
# 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, unicode_literals, print_function
|
||||||
|
from . import lang_EU
|
||||||
|
|
||||||
|
class Num2Word_NO(lang_EU.Num2Word_EU):
|
||||||
|
def set_high_numwords(self, high):
|
||||||
|
max = 3 + 6*len(high)
|
||||||
|
for word, n in zip(high, range(max, 3, -6)):
|
||||||
|
self.cards[10**n] = word + "illard"
|
||||||
|
self.cards[10**(n-3)] = word + "illion"
|
||||||
|
|
||||||
|
def setup(self):
|
||||||
|
self.negword = "minus "
|
||||||
|
self.pointword = "komma"
|
||||||
|
self.errmsg_nornum = "Bare tall kan bli konvertert til ord."
|
||||||
|
self.exclude_title = ["og", "komma", "minus"]
|
||||||
|
|
||||||
|
self.mid_numwords = [(1000, "tusen"), (100, "hundre"),
|
||||||
|
(90, "nitti"), (80, "\xe5tti"), (70, "sytti"),
|
||||||
|
(60, "seksti"), (50, "femti"), (40, "f\xf8rti"),
|
||||||
|
(30, "tretti")]
|
||||||
|
self.low_numwords = ["tjue", "nitten", "atten", "sytten",
|
||||||
|
"seksten", "femten", "fjorten", "tretten",
|
||||||
|
"tolv", "elleve", "ti", "ni", "\xe5tte",
|
||||||
|
"syv", "seks", "fem", "fire", "tre", "to",
|
||||||
|
"en", "null"]
|
||||||
|
self.ords = { "en" : "f\xf8rste",
|
||||||
|
"to" : "andre",
|
||||||
|
"tre" : "tredje",
|
||||||
|
"fire" : "fjerde",
|
||||||
|
"fem" : "femte",
|
||||||
|
"seks" : "sjette",
|
||||||
|
"syv" : "syvende",
|
||||||
|
"\xe5tte" : "\xe5ttende",
|
||||||
|
"ni" : "niende",
|
||||||
|
"ti" : "tiende",
|
||||||
|
"elleve" : "ellevte",
|
||||||
|
"tolv" : "tolvte",
|
||||||
|
"tjue" : "tjuende" }
|
||||||
|
|
||||||
|
|
||||||
|
def merge(self, lpair, rpair):
|
||||||
|
ltext, lnum = lpair
|
||||||
|
rtext, rnum = rpair
|
||||||
|
if lnum == 1 and rnum < 100:
|
||||||
|
return (rtext, rnum)
|
||||||
|
elif 100 > lnum > rnum :
|
||||||
|
return ("%s-%s"%(ltext, rtext), lnum + rnum)
|
||||||
|
elif lnum >= 100 > rnum:
|
||||||
|
return ("%s og %s"%(ltext, rtext), lnum + rnum)
|
||||||
|
elif rnum > lnum:
|
||||||
|
return ("%s %s"%(ltext, rtext), lnum * rnum)
|
||||||
|
return ("%s, %s"%(ltext, rtext), lnum + rnum)
|
||||||
|
|
||||||
|
|
||||||
|
def to_ordinal(self, value):
|
||||||
|
self.verify_ordinal(value)
|
||||||
|
outwords = self.to_cardinal(value).split(" ")
|
||||||
|
lastwords = outwords[-1].split("-")
|
||||||
|
lastword = lastwords[-1].lower()
|
||||||
|
try:
|
||||||
|
lastword = self.ords[lastword]
|
||||||
|
except KeyError:
|
||||||
|
if lastword[-2:] == "ti":
|
||||||
|
lastword = lastword + "ende"
|
||||||
|
else:
|
||||||
|
lastword += "de"
|
||||||
|
lastwords[-1] = self.title(lastword)
|
||||||
|
outwords[-1] = "".join(lastwords)
|
||||||
|
return " ".join(outwords)
|
||||||
|
|
||||||
|
|
||||||
|
def to_ordinal_num(self, value):
|
||||||
|
self.verify_ordinal(value)
|
||||||
|
return "%s%s"%(value, self.to_ordinal(value)[-2:])
|
||||||
|
|
||||||
|
|
||||||
|
def to_year(self, val, longval=True):
|
||||||
|
if not (val//100)%10:
|
||||||
|
return self.to_cardinal(val)
|
||||||
|
return self.to_splitnum(val, hightxt="hundre", jointxt="og",
|
||||||
|
longval=longval)
|
||||||
|
|
||||||
|
def to_currency(self, val, longval=True):
|
||||||
|
return self.to_splitnum(val, hightxt="krone/r", lowtxt="\xf8re/r",
|
||||||
|
jointxt="og", longval=longval, cents = True)
|
||||||
|
|
||||||
|
|
||||||
|
n2w = Num2Word_NO()
|
||||||
|
to_card = n2w.to_cardinal
|
||||||
|
to_ord = n2w.to_ordinal
|
||||||
|
to_ordnum = n2w.to_ordinal_num
|
||||||
|
to_year = n2w.to_year
|
||||||
|
|
||||||
|
def main():
|
||||||
|
for val in [ 1, 11, 12, 21, 31, 33, 71, 80, 81, 91, 99, 100, 101, 102, 155,
|
||||||
|
180, 300, 308, 832, 1000, 1001, 1061, 1100, 1500, 1701, 3000,
|
||||||
|
8280, 8291, 150000, 500000, 1000000, 2000000, 2000001,
|
||||||
|
-21212121211221211111, -2.121212, -1.0000100]:
|
||||||
|
n2w.test(val)
|
||||||
|
n2w.test(1325325436067876801768700107601001012212132143210473207540327057320957032975032975093275093275093270957329057320975093272950730)
|
||||||
|
for val in [1,120,1000,1120,1800, 1976,2000,2010,2099,2171]:
|
||||||
|
print(val, "er", n2w.to_currency(val))
|
||||||
|
print(val, "er", n2w.to_year(val))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
@@ -55,6 +55,9 @@ dwa tysiące dwanaście
|
|||||||
>>> print(n2w(12519.85))
|
>>> print(n2w(12519.85))
|
||||||
dwanaście tysięcy pięćset dziewiętnaście przecinek osiemdziesiąt pięć
|
dwanaście tysięcy pięćset dziewiętnaście przecinek osiemdziesiąt pięć
|
||||||
|
|
||||||
|
>>> print(n2w(123.50))
|
||||||
|
sto dwadzieścia trzy przecinek pięć
|
||||||
|
|
||||||
>>> print(fill(n2w(1234567890)))
|
>>> print(fill(n2w(1234567890)))
|
||||||
miliard dwieście trzydzieści cztery miliony pięćset sześćdziesiąt
|
miliard dwieście trzydzieści cztery miliony pięćset sześćdziesiąt
|
||||||
siedem tysięcy osiemset dziewięćdzisiąt
|
siedem tysięcy osiemset dziewięćdzisiąt
|
||||||
@@ -95,6 +98,9 @@ sto jeden złotych i dwadzieścia jeden groszy
|
|||||||
|
|
||||||
>>> print(to_currency(-1251985, cents = False))
|
>>> print(to_currency(-1251985, cents = False))
|
||||||
minus dwanaście tysięcy pięćset dziewiętnaście euro, 85 centów
|
minus dwanaście tysięcy pięćset dziewiętnaście euro, 85 centów
|
||||||
|
|
||||||
|
>>> print(to_currency(123.50, 'PLN', seperator=' i'))
|
||||||
|
sto dwadzieścia trzy złote i pięćdziesiąt groszy
|
||||||
"""
|
"""
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
@@ -178,7 +184,7 @@ def splitby3(n):
|
|||||||
if start > 0:
|
if start > 0:
|
||||||
yield int(n[:start])
|
yield int(n[:start])
|
||||||
for i in range(start, length, 3):
|
for i in range(start, length, 3):
|
||||||
yield int(n[i:i+3])
|
yield int(n[i:i + 3])
|
||||||
else:
|
else:
|
||||||
yield int(n)
|
yield int(n)
|
||||||
|
|
||||||
@@ -188,7 +194,7 @@ def get_digits(n):
|
|||||||
|
|
||||||
|
|
||||||
def pluralize(n, forms):
|
def pluralize(n, forms):
|
||||||
form = 0 if n==1 else 1 if (n % 10 > 1 and n % 10 < 5 and (n % 100 < 10 or n % 100 > 20)) else 2
|
form = 0 if n == 1 else 1 if (n % 10 > 1 and n % 10 < 5 and (n % 100 < 10 or n % 100 > 20)) else 2
|
||||||
return forms[form]
|
return forms[form]
|
||||||
|
|
||||||
|
|
||||||
@@ -203,8 +209,6 @@ def int2word(n):
|
|||||||
i -= 1
|
i -= 1
|
||||||
n1, n2, n3 = get_digits(x)
|
n1, n2, n3 = get_digits(x)
|
||||||
|
|
||||||
# print str(n3) + str(n2) + str(n1)
|
|
||||||
|
|
||||||
if n3 > 0:
|
if n3 > 0:
|
||||||
words.append(HUNDREDS[n3][0])
|
words.append(HUNDREDS[n3][0])
|
||||||
|
|
||||||
@@ -231,7 +235,7 @@ def n2w(n):
|
|||||||
return int2word(int(n))
|
return int2word(int(n))
|
||||||
|
|
||||||
|
|
||||||
def to_currency(n, currency='EUR', cents=True, seperator=','):
|
def to_currency(n, currency = 'EUR', cents = True, seperator = ','):
|
||||||
if type(n) == int:
|
if type(n) == int:
|
||||||
if n < 0:
|
if n < 0:
|
||||||
minus = True
|
minus = True
|
||||||
@@ -239,12 +243,14 @@ def to_currency(n, currency='EUR', cents=True, seperator=','):
|
|||||||
minus = False
|
minus = False
|
||||||
|
|
||||||
n = abs(n)
|
n = abs(n)
|
||||||
left = n / 100
|
left = n // 100
|
||||||
right = n % 100
|
right = n % 100
|
||||||
else:
|
else:
|
||||||
n = str(n).replace(',', '.')
|
n = str(n).replace(',', '.')
|
||||||
if '.' in n:
|
if '.' in n:
|
||||||
left, right = n.split('.')
|
left, right = n.split('.')
|
||||||
|
if len(right) == 1:
|
||||||
|
right = right + '0'
|
||||||
else:
|
else:
|
||||||
left, right = n, 0
|
left, right = n, 0
|
||||||
left, right = int(left), int(right)
|
left, right = int(left), int(right)
|
||||||
|
|||||||
209
num2words/lang_PT_BR.py
Normal file
209
num2words/lang_PT_BR.py
Normal file
@@ -0,0 +1,209 @@
|
|||||||
|
# -*- 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 division, unicode_literals
|
||||||
|
import re
|
||||||
|
|
||||||
|
from . import lang_EU
|
||||||
|
|
||||||
|
|
||||||
|
class Num2Word_PT_BR(lang_EU.Num2Word_EU):
|
||||||
|
def set_high_numwords(self, high):
|
||||||
|
max = 3 + 3*len(high)
|
||||||
|
for word, n in zip(high, range(max, 3, -3)):
|
||||||
|
self.cards[10**n] = word + "ilhão"
|
||||||
|
|
||||||
|
def setup(self):
|
||||||
|
self.negword = "menos "
|
||||||
|
self.pointword = "vírgula"
|
||||||
|
self.errmsg_nornum = "Somente números podem ser convertidos para palavras"
|
||||||
|
self.exclude_title = ["e", "vírgula", "menos"]
|
||||||
|
|
||||||
|
self.mid_numwords = [
|
||||||
|
(1000, "mil"), (100, "cem"), (90, "noventa"),
|
||||||
|
(80, "oitenta"), (70, "setenta"), (60, "sessenta"), (50, "cinquenta"),
|
||||||
|
(40, "quarenta"), (30, "trinta")
|
||||||
|
]
|
||||||
|
self.low_numwords = [
|
||||||
|
"vinte", "dezenove", "dezoito", "dezessete", "dezesseis",
|
||||||
|
"quinze", "catorze", "treze", "doze", "onze", "dez",
|
||||||
|
"nove", "oito", "sete", "seis", "cinco", "quatro", "três", "dois",
|
||||||
|
"um", "zero"
|
||||||
|
]
|
||||||
|
self.ords = [
|
||||||
|
{
|
||||||
|
0: "",
|
||||||
|
1: "primeiro",
|
||||||
|
2: "segundo",
|
||||||
|
3: "terceiro",
|
||||||
|
4: "quarto",
|
||||||
|
5: "quinto",
|
||||||
|
6: "sexto",
|
||||||
|
7: "sétimo",
|
||||||
|
8: "oitavo",
|
||||||
|
9: "nono",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
0: "",
|
||||||
|
1: "décimo",
|
||||||
|
2: "vigésimo",
|
||||||
|
3: "trigésimo",
|
||||||
|
4: "quadragésimo",
|
||||||
|
5: "quinquagésimo",
|
||||||
|
6: "sexagésimo",
|
||||||
|
7: "septuagésimo",
|
||||||
|
8: "octogésimo",
|
||||||
|
9: "nonagésimo",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
0: "",
|
||||||
|
1: "centésimo",
|
||||||
|
2: "ducentésimo",
|
||||||
|
3: "tricentésimo",
|
||||||
|
4: "quadrigentésimo",
|
||||||
|
5: "quingentésimo",
|
||||||
|
6: "seiscentésimo",
|
||||||
|
7: "septigentésimo",
|
||||||
|
8: "octigentésimo",
|
||||||
|
9: "nongentésimo",
|
||||||
|
},
|
||||||
|
]
|
||||||
|
self.thousand_separators = {
|
||||||
|
3: "milésimo",
|
||||||
|
6: "milionésimo",
|
||||||
|
9: "bilionésimo",
|
||||||
|
12: "trilionésimo",
|
||||||
|
15: "quadrilionésimo"
|
||||||
|
}
|
||||||
|
self.hundreds = {
|
||||||
|
1: "cento",
|
||||||
|
2: "duzentos",
|
||||||
|
3: "trezentos",
|
||||||
|
4: "quatrocentos",
|
||||||
|
5: "quinhentos",
|
||||||
|
6: "seiscentos",
|
||||||
|
7: "setecentos",
|
||||||
|
8: "oitocentos",
|
||||||
|
9: "novecentos",
|
||||||
|
}
|
||||||
|
|
||||||
|
def merge(self, curr, next):
|
||||||
|
ctext, cnum, ntext, nnum = curr + next
|
||||||
|
|
||||||
|
if cnum == 1:
|
||||||
|
if nnum < 1000000:
|
||||||
|
return next
|
||||||
|
ctext = "um"
|
||||||
|
elif cnum == 100 and not nnum == 1000:
|
||||||
|
ctext = "cento"
|
||||||
|
|
||||||
|
if nnum < cnum:
|
||||||
|
if cnum < 100:
|
||||||
|
return ("%s e %s" % (ctext, ntext), cnum + nnum)
|
||||||
|
return ("%s e %s" % (ctext, ntext), cnum + nnum)
|
||||||
|
|
||||||
|
elif (not nnum % 1000000) and cnum > 1:
|
||||||
|
ntext = ntext[:-4] + "lhões"
|
||||||
|
|
||||||
|
if nnum == 100:
|
||||||
|
ctext = self.hundreds[cnum]
|
||||||
|
ntext = ""
|
||||||
|
|
||||||
|
else:
|
||||||
|
ntext = " " + ntext
|
||||||
|
|
||||||
|
return (ctext + ntext, cnum * nnum)
|
||||||
|
|
||||||
|
def to_cardinal(self, value):
|
||||||
|
result = super(Num2Word_PT_BR, self).to_cardinal(value)
|
||||||
|
|
||||||
|
# Transforms "mil E cento e catorze reais" into "mil, cento e catorze reais"
|
||||||
|
for ext in (
|
||||||
|
'mil', 'milhão', 'milhões', 'bilhão', 'bilhões',
|
||||||
|
'trilhão', 'trilhões', 'quatrilhão', 'quatrilhões'):
|
||||||
|
if re.match('.*{} e \w*ento'.format(ext), result):
|
||||||
|
result = result.replace('{} e'.format(ext), '{},'.format(ext), 1)
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
def to_ordinal(self, value):
|
||||||
|
self.verify_ordinal(value)
|
||||||
|
|
||||||
|
result = []
|
||||||
|
value = str(value)
|
||||||
|
thousand_separator = ''
|
||||||
|
|
||||||
|
for idx, char in enumerate(value[::-1]):
|
||||||
|
if idx and idx % 3 == 0:
|
||||||
|
thousand_separator = self.thousand_separators[idx]
|
||||||
|
|
||||||
|
if char != '0' and thousand_separator:
|
||||||
|
# avoiding "segundo milionésimo milésimo" for 6000000, for instance
|
||||||
|
result.append(thousand_separator)
|
||||||
|
thousand_separator = ''
|
||||||
|
|
||||||
|
result.append(self.ords[idx % 3][int(char)])
|
||||||
|
|
||||||
|
result = ' '.join(result[::-1])
|
||||||
|
result = result.strip()
|
||||||
|
result = re.sub('\s+', ' ', result)
|
||||||
|
|
||||||
|
if result.startswith('primeiro') and value != '1':
|
||||||
|
# avoiding "primeiro milésimo", "primeiro milionésimo" and so on
|
||||||
|
result = result[9:]
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
def to_ordinal_num(self, value):
|
||||||
|
self.verify_ordinal(value)
|
||||||
|
return "%sº" % (value)
|
||||||
|
|
||||||
|
def to_year(self, val, longval=True):
|
||||||
|
if val < 0:
|
||||||
|
return self.to_cardinal(abs(val)) + ' antes de Cristo'
|
||||||
|
return self.to_cardinal(val)
|
||||||
|
|
||||||
|
def to_currency(self, val, longval=True):
|
||||||
|
integer_part, decimal_part = ('%.2f' % val).split('.')
|
||||||
|
|
||||||
|
result = self.to_cardinal(int(integer_part))
|
||||||
|
|
||||||
|
appended_currency = False
|
||||||
|
for ext in (
|
||||||
|
'milhão', 'milhões', 'bilhão', 'bilhões',
|
||||||
|
'trilhão', 'trilhões', 'quatrilhão', 'quatrilhões'):
|
||||||
|
if result.endswith(ext):
|
||||||
|
result += ' de reais'
|
||||||
|
appended_currency = True
|
||||||
|
|
||||||
|
if result in ['um', 'menos um']:
|
||||||
|
result += ' real'
|
||||||
|
appended_currency = True
|
||||||
|
if not appended_currency:
|
||||||
|
result += ' reais'
|
||||||
|
|
||||||
|
if int(decimal_part):
|
||||||
|
cents = self.to_cardinal(int(decimal_part))
|
||||||
|
result += ' e ' + cents
|
||||||
|
|
||||||
|
if cents == 'um':
|
||||||
|
result += ' centavo'
|
||||||
|
else:
|
||||||
|
result += ' centavos'
|
||||||
|
|
||||||
|
return result
|
||||||
312
num2words/lang_RU.py
Normal file
312
num2words/lang_RU.py
Normal file
@@ -0,0 +1,312 @@
|
|||||||
|
# -*- 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
|
||||||
|
u"""
|
||||||
|
>>> from textwrap import fill
|
||||||
|
|
||||||
|
>>> ' '.join([str(i) for i in splitby3('1')])
|
||||||
|
u'1'
|
||||||
|
>>> ' '.join([str(i) for i in splitby3('1123')])
|
||||||
|
u'1 123'
|
||||||
|
>>> ' '.join([str(i) for i in splitby3('1234567890')])
|
||||||
|
u'1 234 567 890'
|
||||||
|
|
||||||
|
>>> print(' '.join([n2w(i) for i in range(10)]))
|
||||||
|
ноль один два три четыре пять шесть семь восемь девять
|
||||||
|
|
||||||
|
>>> print(fill(' '.join([n2w(i+10) for i in range(10)])))
|
||||||
|
десять одиннадцать двенадцать тринадцать четырнадцать пятнадцать
|
||||||
|
шестнадцать семнадцать восемнадцать девятнадцать
|
||||||
|
|
||||||
|
>>> print(fill(' '.join([n2w(i*10) for i in range(10)])))
|
||||||
|
ноль десять двадцать тридцать сорок пятьдесят шестьдесят семьдесят
|
||||||
|
восемьдесят девяносто
|
||||||
|
|
||||||
|
>>> print(n2w(100))
|
||||||
|
сто
|
||||||
|
>>> print(n2w(101))
|
||||||
|
сто один
|
||||||
|
>>> print(n2w(110))
|
||||||
|
сто десять
|
||||||
|
>>> print(n2w(115))
|
||||||
|
сто пятнадцать
|
||||||
|
>>> print(n2w(123))
|
||||||
|
сто двадцать три
|
||||||
|
>>> print(n2w(1000))
|
||||||
|
тысяча
|
||||||
|
>>> print(n2w(1001))
|
||||||
|
тысяча один
|
||||||
|
>>> print(n2w(2012))
|
||||||
|
две тысячи двенадцать
|
||||||
|
|
||||||
|
>>> print(n2w(12519.85))
|
||||||
|
двенадцать тысяч пятьсот девятнадцать запятая восемьдесят пять
|
||||||
|
|
||||||
|
>>> print(fill(n2w(1234567890)))
|
||||||
|
миллиард двести тридцать четыре миллиона пятьсот шестьдесят семь тысяч
|
||||||
|
восемьсот девяносто
|
||||||
|
|
||||||
|
>>> print(fill(n2w(215461407892039002157189883901676)))
|
||||||
|
двести пятнадцать нониллионов четыреста шестьдесят один октиллион
|
||||||
|
четыреста семь септиллионов восемьсот девяносто два секстиллиона
|
||||||
|
тридцать девять квинтиллионов два квадриллиона сто пятьдесят семь
|
||||||
|
триллионов сто восемьдесят девять миллиардов восемьсот восемьдесят три
|
||||||
|
миллиона девятьсот одна тысяча шестьсот семьдесят шесть
|
||||||
|
|
||||||
|
>>> print(fill(n2w(719094234693663034822824384220291)))
|
||||||
|
семьсот девятнадцать нониллионов девяносто четыре октиллиона двести
|
||||||
|
тридцать четыре септиллиона шестьсот девяносто три секстиллиона
|
||||||
|
шестьсот шестьдесят три квинтиллиона тридцать четыре квадриллиона
|
||||||
|
восемьсот двадцать два триллиона восемьсот двадцать четыре миллиарда
|
||||||
|
триста восемьдесят четыре миллиона двести двадцать тысяч двести
|
||||||
|
девяносто один
|
||||||
|
|
||||||
|
>>> print(to_currency(1.0, 'EUR'))
|
||||||
|
один евро, ноль центов
|
||||||
|
|
||||||
|
>>> print(to_currency(1.0, 'RUB'))
|
||||||
|
один рубль, ноль копеек
|
||||||
|
|
||||||
|
>>> print(to_currency(1234.56, 'EUR'))
|
||||||
|
тысяча двести тридцать четыре евро, пятьдесят шесть центов
|
||||||
|
|
||||||
|
>>> print(to_currency(1234.56, 'RUB'))
|
||||||
|
тысяча двести тридцать четыре рубля, пятьдесят шесть копеек
|
||||||
|
|
||||||
|
>>> print(to_currency(10111, 'EUR', seperator=u' и'))
|
||||||
|
сто один евро и одиннадцать центов
|
||||||
|
|
||||||
|
>>> print(to_currency(10121, 'RUB', seperator=u' и'))
|
||||||
|
сто один рубль и двадцать одна копейка
|
||||||
|
|
||||||
|
>>> print(to_currency(10122, 'RUB', seperator=u' и'))
|
||||||
|
сто один рубль и двадцать две копейки
|
||||||
|
|
||||||
|
>>> print(to_currency(10121, 'EUR', seperator=u' и'))
|
||||||
|
сто один евро и двадцать один цент
|
||||||
|
|
||||||
|
>>> print(to_currency(-1251985, cents = False))
|
||||||
|
минус двенадцать тысяч пятьсот девятнадцать евро, 85 центов
|
||||||
|
"""
|
||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
ZERO = (u'ноль',)
|
||||||
|
|
||||||
|
ONES_FEMININE = {
|
||||||
|
1: (u'одна',),
|
||||||
|
2: (u'две',),
|
||||||
|
3: (u'три',),
|
||||||
|
4: (u'четыре',),
|
||||||
|
5: (u'пять',),
|
||||||
|
6: (u'шесть',),
|
||||||
|
7: (u'семь',),
|
||||||
|
8: (u'восемь',),
|
||||||
|
9: (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'девяносто',),
|
||||||
|
}
|
||||||
|
|
||||||
|
HUNDREDS = {
|
||||||
|
1: (u'сто',),
|
||||||
|
2: (u'двести',),
|
||||||
|
3: (u'триста',),
|
||||||
|
4: (u'четыреста',),
|
||||||
|
5: (u'пятьсот',),
|
||||||
|
6: (u'шестьсот',),
|
||||||
|
7: (u'семьсот',),
|
||||||
|
8: (u'восемьсот',),
|
||||||
|
9: (u'девятьсот',),
|
||||||
|
}
|
||||||
|
|
||||||
|
THOUSANDS = {
|
||||||
|
1: (u'тысяча', u'тысячи', u'тысяч'), # 10^3
|
||||||
|
2: (u'миллион', u'миллиона', u'миллионов'), # 10^6
|
||||||
|
3: (u'миллиард', u'миллиарда', u'миллиардов'), # 10^9
|
||||||
|
4: (u'триллион', u'триллиона', u'триллионов'), # 10^12
|
||||||
|
5: (u'квадриллион', u'квадриллиона', u'квадриллионов'), # 10^15
|
||||||
|
6: (u'квинтиллион', u'квинтиллиона', u'квинтиллионов'), # 10^18
|
||||||
|
7: (u'секстиллион', u'секстиллиона', u'секстиллионов'), # 10^21
|
||||||
|
8: (u'септиллион', u'септиллиона', u'септиллионов'), # 10^24
|
||||||
|
9: (u'октиллион', u'октиллиона', u'октиллионов'), #10^27
|
||||||
|
10: (u'нониллион', u'нониллиона', u'нониллионов'), # 10^30
|
||||||
|
}
|
||||||
|
|
||||||
|
CURRENCIES = {
|
||||||
|
'RUB': (
|
||||||
|
(u'рубль', u'рубля', u'рублей'), (u'копейка', u'копейки', u'копеек')
|
||||||
|
),
|
||||||
|
'EUR': (
|
||||||
|
(u'евро', u'евро', u'евро'), (u'цент', u'цента', 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):
|
||||||
|
if (n % 100 < 10 or n % 100 > 20):
|
||||||
|
if n % 10 == 1:
|
||||||
|
form = 0
|
||||||
|
elif (n % 10 > 1 and n % 10 < 5):
|
||||||
|
form = 1
|
||||||
|
else:
|
||||||
|
form = 2
|
||||||
|
else:
|
||||||
|
form = 2
|
||||||
|
return forms[form]
|
||||||
|
|
||||||
|
|
||||||
|
def int2word(n, feminine=False):
|
||||||
|
if n < 0:
|
||||||
|
return ' '.join([u'минус', int2word(abs(n))])
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
if n3 > 0:
|
||||||
|
words.append(HUNDREDS[n3][0])
|
||||||
|
|
||||||
|
if n2 > 1:
|
||||||
|
words.append(TWENTIES[n2][0])
|
||||||
|
|
||||||
|
if n2 == 1:
|
||||||
|
words.append(TENS[n1][0])
|
||||||
|
elif n1 > 0:
|
||||||
|
ones = ONES_FEMININE if i == 1 or feminine and i == 0 else ONES
|
||||||
|
words.append(ones[n1][0])
|
||||||
|
|
||||||
|
if i > 0 and x != 0:
|
||||||
|
words.append(pluralize(x, THOUSANDS[i]))
|
||||||
|
|
||||||
|
return ' '.join(words)
|
||||||
|
|
||||||
|
|
||||||
|
def n2w(n):
|
||||||
|
n = str(n).replace(',', '.')
|
||||||
|
if '.' in n:
|
||||||
|
left, right = n.split('.')
|
||||||
|
return u'%s запятая %s' % (int2word(int(left)), int2word(int(right)))
|
||||||
|
else:
|
||||||
|
return int2word(int(n))
|
||||||
|
|
||||||
|
|
||||||
|
def to_currency(n, currency='EUR', cents=True, seperator=','):
|
||||||
|
if type(n) == int:
|
||||||
|
if n < 0:
|
||||||
|
minus = True
|
||||||
|
else:
|
||||||
|
minus = False
|
||||||
|
|
||||||
|
n = abs(n)
|
||||||
|
left = n / 100
|
||||||
|
right = n % 100
|
||||||
|
else:
|
||||||
|
n = str(n).replace(',', '.')
|
||||||
|
if '.' in n:
|
||||||
|
left, right = n.split('.')
|
||||||
|
else:
|
||||||
|
left, right = n, 0
|
||||||
|
left, right = int(left), int(right)
|
||||||
|
minus = False
|
||||||
|
cr1, cr2 = CURRENCIES[currency]
|
||||||
|
|
||||||
|
if minus:
|
||||||
|
minus_str = "минус "
|
||||||
|
else:
|
||||||
|
minus_str = ""
|
||||||
|
|
||||||
|
if cents:
|
||||||
|
cents_feminine = currency == 'RUB'
|
||||||
|
cents_str = int2word(right, cents_feminine)
|
||||||
|
else:
|
||||||
|
cents_str = "%02d" % right
|
||||||
|
|
||||||
|
return u'%s%s %s%s %s %s' % (
|
||||||
|
minus_str,
|
||||||
|
int2word(left),
|
||||||
|
pluralize(left, cr1),
|
||||||
|
seperator,
|
||||||
|
cents_str,
|
||||||
|
pluralize(right, cr2)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class Num2Word_RU(object):
|
||||||
|
def to_cardinal(self, number):
|
||||||
|
return n2w(number)
|
||||||
|
|
||||||
|
def to_ordinal(self, number):
|
||||||
|
raise NotImplementedError()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import doctest
|
||||||
|
doctest.testmod()
|
||||||
565
num2words/lang_TR.py
Normal file
565
num2words/lang_TR.py
Normal file
@@ -0,0 +1,565 @@
|
|||||||
|
# -*- encoding: utf-8 -*-
|
||||||
|
# Copyright (c) 2003, Taro Ogawa. All Rights Reserved.
|
||||||
|
# Copyright (c) 2013, Savoir-faire Linux inc. All Rights Reserved.
|
||||||
|
# Copyright (c) 2017, Tufan Kaynak, Framras. 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
|
||||||
|
|
||||||
|
|
||||||
|
class Num2Word_TR(object):
|
||||||
|
def __init__(self):
|
||||||
|
self.precision = 2
|
||||||
|
self.negword = u"eksi"
|
||||||
|
self.pointword = u"virgül"
|
||||||
|
self.CURRENCY_UNIT = (u"lira",)
|
||||||
|
self.CURRENCY_SUBUNIT = (u"kuruş",)
|
||||||
|
self.errmsg_nonnum = u"Sadece sayılar yazıya çevrilebilir."
|
||||||
|
self.errmsg_floatord = u"Tam sayı olmayan {} sıralamada kullanılamaz."
|
||||||
|
self.errmsg_negord = u"Pozitif olmayan {} sıralamada kullanılamaz."
|
||||||
|
self.errmsg_toobig = u"abs({}) sayı yazıya çevirmek için çok büyük. Yazıya çevrilebilecek en büyük rakam {}."
|
||||||
|
self.exclude_title = []
|
||||||
|
self.DECIMAL_SIGN = (",",)
|
||||||
|
self.ORDINAL_SIGN = (".",)
|
||||||
|
self.ZERO = (u"sıfır",)
|
||||||
|
self.CARDINAL_ONES = {
|
||||||
|
"1": u"bir",
|
||||||
|
"2": u"iki",
|
||||||
|
"3": u"üç",
|
||||||
|
"4": u"dört",
|
||||||
|
"5": u"beş",
|
||||||
|
"6": u"altı",
|
||||||
|
"7": u"yedi",
|
||||||
|
"8": u"sekiz",
|
||||||
|
"9": u"dokuz"
|
||||||
|
}
|
||||||
|
self.ORDINAL_ONES = {
|
||||||
|
"1": u"birinci",
|
||||||
|
"2": u"ikinci",
|
||||||
|
"3": u"üçüncü",
|
||||||
|
"4": u"dördüncü",
|
||||||
|
"5": u"beşinci",
|
||||||
|
"6": u"altıncı",
|
||||||
|
"7": u"yedinci",
|
||||||
|
"8": u"sekizinci",
|
||||||
|
"9": u"dokuzuncu"
|
||||||
|
}
|
||||||
|
self.CARDINAL_TENS = {
|
||||||
|
"1": u"on",
|
||||||
|
"2": u"yirmi",
|
||||||
|
"3": u"otuz",
|
||||||
|
"4": u"kırk",
|
||||||
|
"5": u"elli",
|
||||||
|
"6": u"altmış",
|
||||||
|
"7": u"yetmiş",
|
||||||
|
"8": u"seksen",
|
||||||
|
"9": u"doksan"
|
||||||
|
}
|
||||||
|
self.ORDINAL_TENS = {
|
||||||
|
"1": u"onuncu",
|
||||||
|
"2": u"yirminci",
|
||||||
|
"3": u"otuzuncu",
|
||||||
|
"4": u"kırkıncı",
|
||||||
|
"5": u"ellinci",
|
||||||
|
"6": u"altmışıncı",
|
||||||
|
"7": u"yetmişinci",
|
||||||
|
"8": u"sekseninci",
|
||||||
|
"9": u"doksanıncı"
|
||||||
|
}
|
||||||
|
self.HUNDREDS = {
|
||||||
|
"2": u"iki",
|
||||||
|
"3": u"üç",
|
||||||
|
"4": u"dört",
|
||||||
|
"5": u"beş",
|
||||||
|
"6": u"altı",
|
||||||
|
"7": u"yedi",
|
||||||
|
"8": u"sekiz",
|
||||||
|
"9": u"dokuz"
|
||||||
|
}
|
||||||
|
self.CARDINAL_HUNDRED = (u"yüz",)
|
||||||
|
self.ORDINAL_HUNDRED = (u"yüzüncü",)
|
||||||
|
self.CARDINAL_TRIPLETS = {
|
||||||
|
1: u"bin",
|
||||||
|
2: u"milyon",
|
||||||
|
3: u"milyar",
|
||||||
|
4: u"trilyon",
|
||||||
|
5: u"katrilyon",
|
||||||
|
6: u"kentilyon"
|
||||||
|
}
|
||||||
|
self.ORDINAL_TRIPLETS = {
|
||||||
|
1: u"bininci",
|
||||||
|
2: u"milyonuncu",
|
||||||
|
3: u"milyarıncı",
|
||||||
|
4: u"trilyonuncu",
|
||||||
|
5: u"katrilyonuncu",
|
||||||
|
6: u"kentilyon"
|
||||||
|
}
|
||||||
|
self.MAXVAL = (10 ** ((len(self.CARDINAL_TRIPLETS) + 1) * 3)) - 1
|
||||||
|
|
||||||
|
self.integers_to_read = []
|
||||||
|
self.total_triplets_to_read = 0
|
||||||
|
self.total_digits_outside_triplets = 0
|
||||||
|
self.order_of_last_zero_digit = 0
|
||||||
|
|
||||||
|
def to_cardinal(self, value):
|
||||||
|
wrd = ""
|
||||||
|
is_cardinal = self.verify_cardinal(value)
|
||||||
|
if is_cardinal:
|
||||||
|
if not int(value) == value:
|
||||||
|
return self.to_cardinal_float(value)
|
||||||
|
self.to_splitnum(value)
|
||||||
|
|
||||||
|
if self.order_of_last_zero_digit >= len(self.integers_to_read[0]):
|
||||||
|
# number like 00 and all 0s and even more, raise error
|
||||||
|
return wrd
|
||||||
|
|
||||||
|
if self.total_triplets_to_read == 1:
|
||||||
|
if self.total_digits_outside_triplets == 2:
|
||||||
|
if self.order_of_last_zero_digit == 1:
|
||||||
|
# number like x0, read cardinal x0 and return
|
||||||
|
wrd += self.CARDINAL_TENS.get(self.integers_to_read[0][0], "")
|
||||||
|
return wrd
|
||||||
|
if self.order_of_last_zero_digit == 0:
|
||||||
|
# number like xy, read cardinal xy and return
|
||||||
|
wrd += self.CARDINAL_TENS.get(self.integers_to_read[0][0], "")
|
||||||
|
wrd += self.CARDINAL_ONES.get(self.integers_to_read[0][1], "")
|
||||||
|
return wrd
|
||||||
|
|
||||||
|
if self.total_digits_outside_triplets == 1:
|
||||||
|
if self.order_of_last_zero_digit == 0:
|
||||||
|
# number like x, read cardinal x and return
|
||||||
|
wrd += self.CARDINAL_ONES.get(self.integers_to_read[0][0], "")
|
||||||
|
return wrd
|
||||||
|
|
||||||
|
if self.total_digits_outside_triplets == 0:
|
||||||
|
if self.order_of_last_zero_digit == 2:
|
||||||
|
# number like x00, read cardinal x00 and return
|
||||||
|
wrd += self.HUNDREDS.get(self.integers_to_read[0][0], "")
|
||||||
|
wrd += self.CARDINAL_HUNDRED[0]
|
||||||
|
return wrd
|
||||||
|
if self.order_of_last_zero_digit == 1:
|
||||||
|
# number like xy0, read cardinal xy0 and return
|
||||||
|
wrd += self.HUNDREDS.get(self.integers_to_read[0][0], "")
|
||||||
|
wrd += self.CARDINAL_HUNDRED[0]
|
||||||
|
wrd += self.CARDINAL_TENS.get(self.integers_to_read[0][1], "")
|
||||||
|
return wrd
|
||||||
|
if self.order_of_last_zero_digit == 0:
|
||||||
|
# number like xyz, read cardinal xyz and return
|
||||||
|
wrd += self.HUNDREDS.get(self.integers_to_read[0][0], "")
|
||||||
|
wrd += self.CARDINAL_HUNDRED[0]
|
||||||
|
wrd += self.CARDINAL_TENS.get(self.integers_to_read[0][1], "")
|
||||||
|
wrd += self.CARDINAL_ONES.get(self.integers_to_read[0][2], "")
|
||||||
|
return wrd
|
||||||
|
|
||||||
|
if self.total_triplets_to_read >= 2:
|
||||||
|
if self.total_digits_outside_triplets == 2:
|
||||||
|
if self.order_of_last_zero_digit == len(self.integers_to_read[0]) - 1:
|
||||||
|
# number like x0 and all 0s, read cardinal x0 0..0 and return
|
||||||
|
wrd += self.CARDINAL_TENS.get(self.integers_to_read[0][0], "")
|
||||||
|
wrd += self.CARDINAL_TRIPLETS[self.total_triplets_to_read - 1]
|
||||||
|
return wrd
|
||||||
|
if self.order_of_last_zero_digit == len(self.integers_to_read[0]) - 2:
|
||||||
|
# number like xy and all 0s, read cardinal xy 0..0 and return
|
||||||
|
wrd += self.CARDINAL_TENS.get(self.integers_to_read[0][0], "")
|
||||||
|
wrd += self.CARDINAL_ONES.get(self.integers_to_read[0][1], "")
|
||||||
|
wrd += self.CARDINAL_TRIPLETS[self.total_triplets_to_read - 1]
|
||||||
|
return wrd
|
||||||
|
if self.order_of_last_zero_digit < len(self.integers_to_read[0]) - 2:
|
||||||
|
# number like xy and others, read cardinal xy n..n and return
|
||||||
|
wrd += self.CARDINAL_TENS.get(self.integers_to_read[0][0], "")
|
||||||
|
wrd += self.CARDINAL_ONES.get(self.integers_to_read[0][1], "")
|
||||||
|
wrd += self.CARDINAL_TRIPLETS[self.total_triplets_to_read - 1]
|
||||||
|
|
||||||
|
if self.total_digits_outside_triplets == 1:
|
||||||
|
if self.order_of_last_zero_digit == len(self.integers_to_read[0]) - 1:
|
||||||
|
# number like x and all 0s, read cardinal x 0..0 and return
|
||||||
|
if not (self.total_triplets_to_read == 2 and self.integers_to_read[0][0] == "1"):
|
||||||
|
wrd += self.CARDINAL_ONES.get(self.integers_to_read[0][0], "")
|
||||||
|
wrd += self.CARDINAL_TRIPLETS[self.total_triplets_to_read - 1]
|
||||||
|
return wrd
|
||||||
|
if self.order_of_last_zero_digit < len(self.integers_to_read[0]) - 1:
|
||||||
|
# number like x and others, read cardinal x n..n and return
|
||||||
|
if not (self.total_triplets_to_read == 2 and self.integers_to_read[0][0] == "1"):
|
||||||
|
wrd += self.CARDINAL_ONES.get(self.integers_to_read[0][0], "")
|
||||||
|
wrd += self.CARDINAL_TRIPLETS[self.total_triplets_to_read - 1]
|
||||||
|
|
||||||
|
if self.total_digits_outside_triplets == 0:
|
||||||
|
if self.order_of_last_zero_digit == len(self.integers_to_read[0]) - 1:
|
||||||
|
# number like x00 and all 0s, read cardinal x00 0..0 and return
|
||||||
|
wrd += self.HUNDREDS.get(self.integers_to_read[0][0], "")
|
||||||
|
wrd += self.CARDINAL_HUNDRED[0]
|
||||||
|
wrd += self.CARDINAL_TRIPLETS[self.total_triplets_to_read - 1]
|
||||||
|
return wrd
|
||||||
|
if self.order_of_last_zero_digit == len(self.integers_to_read[0]) - 2:
|
||||||
|
# number like xy0 and all 0s, read cardinal xy0 0..0 and return
|
||||||
|
wrd += self.HUNDREDS.get(self.integers_to_read[0][0], "")
|
||||||
|
wrd += self.CARDINAL_HUNDRED[0]
|
||||||
|
wrd += self.CARDINAL_TENS.get(self.integers_to_read[0][1], "")
|
||||||
|
wrd += self.CARDINAL_TRIPLETS[self.total_triplets_to_read - 1]
|
||||||
|
return wrd
|
||||||
|
if self.order_of_last_zero_digit == len(self.integers_to_read[0]) - 3:
|
||||||
|
# number like xyz and all 0s, read cardinal xyz 0..0 and return
|
||||||
|
wrd += self.HUNDREDS.get(self.integers_to_read[0][0], "")
|
||||||
|
wrd += self.CARDINAL_HUNDRED[0]
|
||||||
|
wrd += self.CARDINAL_TENS.get(self.integers_to_read[0][1], "")
|
||||||
|
wrd += self.CARDINAL_ONES.get(self.integers_to_read[0][2], "")
|
||||||
|
wrd += self.CARDINAL_TRIPLETS[self.total_triplets_to_read - 1]
|
||||||
|
return wrd
|
||||||
|
if self.order_of_last_zero_digit < len(self.integers_to_read[0]) - 3:
|
||||||
|
# number like xyz and all others, read cardinal xyz n..n
|
||||||
|
wrd += self.HUNDREDS.get(self.integers_to_read[0][0], "")
|
||||||
|
wrd += self.CARDINAL_HUNDRED[0]
|
||||||
|
wrd += self.CARDINAL_TENS.get(self.integers_to_read[0][1], "")
|
||||||
|
if not (self.total_triplets_to_read == 2 and self.integers_to_read[0][2] == "1"):
|
||||||
|
wrd += self.CARDINAL_ONES.get(self.integers_to_read[0][2], "")
|
||||||
|
wrd += self.CARDINAL_TRIPLETS[self.total_triplets_to_read - 1]
|
||||||
|
|
||||||
|
for i in list(range(self.total_triplets_to_read - 1, 0, -1)):
|
||||||
|
reading_triplet_order = self.total_triplets_to_read - i
|
||||||
|
if self.total_digits_outside_triplets == 0:
|
||||||
|
last_read_digit_order = reading_triplet_order * 3
|
||||||
|
else:
|
||||||
|
last_read_digit_order = (reading_triplet_order - 1) * 3 + self.total_digits_outside_triplets
|
||||||
|
|
||||||
|
if not self.integers_to_read[0][last_read_digit_order: last_read_digit_order + 3] == "000":
|
||||||
|
if not self.integers_to_read[0][last_read_digit_order] == "0":
|
||||||
|
wrd += self.HUNDREDS.get(self.integers_to_read[0][last_read_digit_order], "")
|
||||||
|
if self.order_of_last_zero_digit == len(self.integers_to_read[0]) - (
|
||||||
|
last_read_digit_order) - 1:
|
||||||
|
if i == 1:
|
||||||
|
wrd += self.CARDINAL_HUNDRED[0]
|
||||||
|
return wrd
|
||||||
|
elif i > 1:
|
||||||
|
wrd += self.CARDINAL_HUNDRED[0]
|
||||||
|
wrd += self.CARDINAL_TRIPLETS[i - 1]
|
||||||
|
return wrd
|
||||||
|
else:
|
||||||
|
wrd += self.CARDINAL_HUNDRED[0]
|
||||||
|
|
||||||
|
if not self.integers_to_read[0][last_read_digit_order + 1] == "0":
|
||||||
|
if self.order_of_last_zero_digit == len(self.integers_to_read[0]) - (
|
||||||
|
last_read_digit_order) - 2:
|
||||||
|
if i == 1:
|
||||||
|
wrd += self.CARDINAL_TENS.get(
|
||||||
|
self.integers_to_read[0][last_read_digit_order + 1], "")
|
||||||
|
return wrd
|
||||||
|
elif i > 1:
|
||||||
|
wrd += self.CARDINAL_TENS.get(
|
||||||
|
self.integers_to_read[0][last_read_digit_order + 1], "")
|
||||||
|
wrd += self.CARDINAL_TRIPLETS[i - 1]
|
||||||
|
return wrd
|
||||||
|
else:
|
||||||
|
wrd += self.CARDINAL_TENS.get(self.integers_to_read[0][last_read_digit_order + 1],
|
||||||
|
"")
|
||||||
|
|
||||||
|
if not self.integers_to_read[0][last_read_digit_order + 2] == "0":
|
||||||
|
if self.order_of_last_zero_digit == len(self.integers_to_read[0]) - (
|
||||||
|
last_read_digit_order) - 3:
|
||||||
|
if i == 1:
|
||||||
|
wrd += self.CARDINAL_ONES.get(
|
||||||
|
self.integers_to_read[0][last_read_digit_order + 2], "")
|
||||||
|
return wrd
|
||||||
|
if i == 2:
|
||||||
|
if not self.integers_to_read[0][
|
||||||
|
last_read_digit_order: last_read_digit_order + 2] == "00":
|
||||||
|
wrd += self.CARDINAL_ONES.get(
|
||||||
|
self.integers_to_read[0][last_read_digit_order + 2], "")
|
||||||
|
elif not self.integers_to_read[0][last_read_digit_order + 2] == "1":
|
||||||
|
wrd += self.CARDINAL_ONES.get(
|
||||||
|
self.integers_to_read[0][last_read_digit_order + 2], "")
|
||||||
|
wrd += self.CARDINAL_TRIPLETS[i - 1]
|
||||||
|
return wrd
|
||||||
|
if i > 2:
|
||||||
|
wrd += self.CARDINAL_ONES.get(
|
||||||
|
self.integers_to_read[0][last_read_digit_order + 2], "")
|
||||||
|
wrd += self.CARDINAL_TRIPLETS[i - 1]
|
||||||
|
return wrd
|
||||||
|
else:
|
||||||
|
if not self.integers_to_read[0][
|
||||||
|
last_read_digit_order: last_read_digit_order + 2] == "00":
|
||||||
|
wrd += self.CARDINAL_ONES.get(
|
||||||
|
self.integers_to_read[0][last_read_digit_order + 2], "")
|
||||||
|
else:
|
||||||
|
if i == 2:
|
||||||
|
if not self.integers_to_read[0][
|
||||||
|
last_read_digit_order: last_read_digit_order + 2] == "00":
|
||||||
|
wrd += self.CARDINAL_ONES.get(
|
||||||
|
self.integers_to_read[0][last_read_digit_order + 2], "")
|
||||||
|
elif not self.integers_to_read[0][last_read_digit_order + 2] == "1":
|
||||||
|
wrd += self.CARDINAL_ONES.get(
|
||||||
|
self.integers_to_read[0][last_read_digit_order + 2], "")
|
||||||
|
|
||||||
|
wrd += self.CARDINAL_TRIPLETS[i - 1]
|
||||||
|
|
||||||
|
return wrd
|
||||||
|
|
||||||
|
def to_cardinal_float(self, value):
|
||||||
|
self.to_splitnum(value)
|
||||||
|
wrd = ""
|
||||||
|
wrd += self.pointword
|
||||||
|
if len(self.integers_to_read[1]) >= 1:
|
||||||
|
wrd += self.CARDINAL_TENS.get(self.integers_to_read[1][0], "")
|
||||||
|
|
||||||
|
if len(self.integers_to_read[1]) == 2:
|
||||||
|
wrd += self.CARDINAL_ONES.get(self.integers_to_read[1][1], "")
|
||||||
|
|
||||||
|
if self.integers_to_read[0] == "0":
|
||||||
|
wrd = self.ZERO[0] + wrd
|
||||||
|
else:
|
||||||
|
wrd = self.to_cardinal(int(self.integers_to_read[0])) + wrd
|
||||||
|
return wrd
|
||||||
|
|
||||||
|
def verify_cardinal(self, value):
|
||||||
|
iscardinal = True
|
||||||
|
try:
|
||||||
|
if not float(value) == value:
|
||||||
|
iscardinal = False
|
||||||
|
except (ValueError, TypeError):
|
||||||
|
raise TypeError(self.errmsg_nonnum)
|
||||||
|
if abs(value) >= self.MAXVAL:
|
||||||
|
raise OverflowError(self.errmsg_toobig.format(value, self.MAXVAL))
|
||||||
|
return iscardinal
|
||||||
|
|
||||||
|
def verify_ordinal(self, value):
|
||||||
|
isordinal = True
|
||||||
|
try:
|
||||||
|
if not int(value) == value:
|
||||||
|
isordinal = False
|
||||||
|
if not abs(value) == value:
|
||||||
|
raise TypeError(self.errmsg_negord.format(value))
|
||||||
|
except (ValueError, TypeError):
|
||||||
|
raise TypeError(self.errmsg_nonnum)
|
||||||
|
if abs(value) >= self.MAXVAL:
|
||||||
|
raise OverflowError(self.errmsg_toobig.format(value, self.MAXVAL))
|
||||||
|
return isordinal
|
||||||
|
|
||||||
|
def to_ordinal(self, value):
|
||||||
|
wrd = ""
|
||||||
|
isordinal = self.verify_ordinal(value)
|
||||||
|
if isordinal:
|
||||||
|
self.to_splitnum(value)
|
||||||
|
|
||||||
|
if self.order_of_last_zero_digit >= len(self.integers_to_read[0]):
|
||||||
|
# number like 00 and all 0s and even more, raise error
|
||||||
|
return wrd
|
||||||
|
|
||||||
|
if self.total_triplets_to_read == 1:
|
||||||
|
if self.total_digits_outside_triplets == 2:
|
||||||
|
if self.order_of_last_zero_digit == 1:
|
||||||
|
# number like x0, read ordinal x0 and return
|
||||||
|
wrd += self.ORDINAL_TENS.get(self.integers_to_read[0][0], "")
|
||||||
|
return wrd
|
||||||
|
if self.order_of_last_zero_digit == 0:
|
||||||
|
# number like xy, read ordinal xy and return
|
||||||
|
wrd += self.CARDINAL_TENS.get(self.integers_to_read[0][0], "")
|
||||||
|
wrd += self.ORDINAL_ONES.get(self.integers_to_read[0][1], "")
|
||||||
|
return wrd
|
||||||
|
|
||||||
|
if self.total_digits_outside_triplets == 1:
|
||||||
|
if self.order_of_last_zero_digit == 0:
|
||||||
|
# number like x, read ordinal x and return
|
||||||
|
wrd += self.ORDINAL_ONES.get(self.integers_to_read[0][0], "")
|
||||||
|
return wrd
|
||||||
|
|
||||||
|
if self.total_digits_outside_triplets == 0:
|
||||||
|
if self.order_of_last_zero_digit == 2:
|
||||||
|
# number like x00, read ordinal x00 and return
|
||||||
|
wrd += self.HUNDREDS.get(self.integers_to_read[0][0], "")
|
||||||
|
wrd += self.ORDINAL_HUNDRED[0]
|
||||||
|
return wrd
|
||||||
|
if self.order_of_last_zero_digit == 1:
|
||||||
|
# number like xy0, read ordinal xy0 and return
|
||||||
|
wrd += self.HUNDREDS.get(self.integers_to_read[0][0], "")
|
||||||
|
wrd += self.CARDINAL_HUNDRED[0]
|
||||||
|
wrd += self.ORDINAL_TENS.get(self.integers_to_read[0][1], "")
|
||||||
|
return wrd
|
||||||
|
if self.order_of_last_zero_digit == 0:
|
||||||
|
# number like xyz, read ordinal xyz and return
|
||||||
|
wrd += self.HUNDREDS.get(self.integers_to_read[0][0], "")
|
||||||
|
wrd += self.CARDINAL_HUNDRED[0]
|
||||||
|
wrd += self.CARDINAL_TENS.get(self.integers_to_read[0][1], "")
|
||||||
|
if not self.integers_to_read[0][2] == "0":
|
||||||
|
wrd += self.ORDINAL_ONES.get(self.integers_to_read[0][2], "")
|
||||||
|
return wrd
|
||||||
|
|
||||||
|
if self.total_triplets_to_read >= 2:
|
||||||
|
if self.total_digits_outside_triplets == 2:
|
||||||
|
if self.order_of_last_zero_digit == len(self.integers_to_read[0]) - 1:
|
||||||
|
# number like x0 and all 0s, read ordinal x0 0..0 and return
|
||||||
|
wrd += self.CARDINAL_TENS.get(self.integers_to_read[0][0], "")
|
||||||
|
wrd += self.ORDINAL_TRIPLETS[self.total_triplets_to_read - 1]
|
||||||
|
return wrd
|
||||||
|
if self.order_of_last_zero_digit == len(self.integers_to_read[0]) - 2:
|
||||||
|
# number like xy and all 0s, read ordinal xy 0..0 and return
|
||||||
|
wrd += self.CARDINAL_TENS.get(self.integers_to_read[0][0], "")
|
||||||
|
wrd += self.CARDINAL_ONES.get(self.integers_to_read[0][1], "")
|
||||||
|
wrd += self.ORDINAL_TRIPLETS[self.total_triplets_to_read - 1]
|
||||||
|
return wrd
|
||||||
|
if self.order_of_last_zero_digit < len(self.integers_to_read[0]) - 2:
|
||||||
|
# number like xy and others, read cardinal xy n..n and return
|
||||||
|
wrd += self.CARDINAL_TENS.get(self.integers_to_read[0][0], "")
|
||||||
|
wrd += self.CARDINAL_ONES.get(self.integers_to_read[0][1], "")
|
||||||
|
wrd += self.CARDINAL_TRIPLETS[self.total_triplets_to_read - 1]
|
||||||
|
|
||||||
|
if self.total_digits_outside_triplets == 1:
|
||||||
|
if self.order_of_last_zero_digit == len(self.integers_to_read[0]) - 1:
|
||||||
|
# number like x and all 0s, read ordinal x 0..0 and return
|
||||||
|
if not (self.total_triplets_to_read == 2 and self.integers_to_read[0][0] == "1"):
|
||||||
|
wrd += self.CARDINAL_ONES.get(self.integers_to_read[0][0], "")
|
||||||
|
wrd += self.ORDINAL_TRIPLETS[self.total_triplets_to_read - 1]
|
||||||
|
return wrd
|
||||||
|
if self.order_of_last_zero_digit < len(self.integers_to_read[0]) - 1:
|
||||||
|
# number like x and others, read cardinal x n..n and return
|
||||||
|
if not (self.total_triplets_to_read == 2 and self.integers_to_read[0][0] == "1"):
|
||||||
|
wrd += self.CARDINAL_ONES.get(self.integers_to_read[0][0], "")
|
||||||
|
wrd += self.CARDINAL_TRIPLETS[self.total_triplets_to_read - 1]
|
||||||
|
|
||||||
|
if self.total_digits_outside_triplets == 0:
|
||||||
|
if self.order_of_last_zero_digit == len(self.integers_to_read[0]) - 1:
|
||||||
|
# number like x00 and all 0s, read ordinal x00 0..0 and return
|
||||||
|
wrd += self.HUNDREDS.get(self.integers_to_read[0][0], "")
|
||||||
|
wrd += self.CARDINAL_HUNDRED[0]
|
||||||
|
wrd += self.ORDINAL_TRIPLETS[self.total_triplets_to_read - 1]
|
||||||
|
return wrd
|
||||||
|
if self.order_of_last_zero_digit == len(self.integers_to_read[0]) - 2:
|
||||||
|
# number like xy0 and all 0s, read ordinal xy0 0..0 and return
|
||||||
|
wrd += self.HUNDREDS.get(self.integers_to_read[0][0], "")
|
||||||
|
wrd += self.CARDINAL_HUNDRED[0]
|
||||||
|
wrd += self.CARDINAL_TENS.get(self.integers_to_read[0][1], "")
|
||||||
|
wrd += self.ORDINAL_TRIPLETS[self.total_triplets_to_read - 1]
|
||||||
|
return wrd
|
||||||
|
if self.order_of_last_zero_digit == len(self.integers_to_read[0]) - 3:
|
||||||
|
# number like xyz and all 0s, read ordinal xyz 0..0 and return
|
||||||
|
wrd += self.HUNDREDS.get(self.integers_to_read[0][0], "")
|
||||||
|
wrd += self.CARDINAL_HUNDRED[0]
|
||||||
|
wrd += self.CARDINAL_TENS.get(self.integers_to_read[0][1], "")
|
||||||
|
wrd += self.CARDINAL_ONES.get(self.integers_to_read[0][2], "")
|
||||||
|
wrd += self.ORDINAL_TRIPLETS[self.total_triplets_to_read - 1]
|
||||||
|
return wrd
|
||||||
|
if self.order_of_last_zero_digit < len(self.integers_to_read[0]) - 3:
|
||||||
|
# number like xyz and all others, read cardinal xyz n..n
|
||||||
|
wrd += self.HUNDREDS.get(self.integers_to_read[0][0], "")
|
||||||
|
wrd += self.CARDINAL_HUNDRED[0]
|
||||||
|
wrd += self.CARDINAL_TENS.get(self.integers_to_read[0][1], "")
|
||||||
|
if not (self.total_triplets_to_read == 2 and self.integers_to_read[0][2] == "1"):
|
||||||
|
wrd += self.CARDINAL_ONES.get(self.integers_to_read[0][2], "")
|
||||||
|
wrd += self.CARDINAL_TRIPLETS[self.total_triplets_to_read - 1]
|
||||||
|
|
||||||
|
for i in list(range(self.total_triplets_to_read - 1, 0, -1)):
|
||||||
|
reading_triplet_order = self.total_triplets_to_read - i
|
||||||
|
if self.total_digits_outside_triplets == 0:
|
||||||
|
last_read_digit_order = reading_triplet_order * 3
|
||||||
|
else:
|
||||||
|
last_read_digit_order = (reading_triplet_order - 1) * 3 + self.total_digits_outside_triplets
|
||||||
|
|
||||||
|
if not self.integers_to_read[0][last_read_digit_order: last_read_digit_order + 3] == "000":
|
||||||
|
if not self.integers_to_read[0][last_read_digit_order] == "0":
|
||||||
|
if not self.integers_to_read[0][last_read_digit_order] == "1":
|
||||||
|
wrd += self.CARDINAL_ONES.get(self.integers_to_read[0][last_read_digit_order], "")
|
||||||
|
if self.order_of_last_zero_digit == len(self.integers_to_read[0]) - (
|
||||||
|
last_read_digit_order) - 1:
|
||||||
|
if i == 1:
|
||||||
|
wrd += self.ORDINAL_HUNDRED[0]
|
||||||
|
return wrd
|
||||||
|
elif i > 1:
|
||||||
|
wrd += self.CARDINAL_HUNDRED[0]
|
||||||
|
wrd += self.ORDINAL_TRIPLETS[i - 1]
|
||||||
|
return wrd
|
||||||
|
else:
|
||||||
|
wrd += self.CARDINAL_HUNDRED[0]
|
||||||
|
|
||||||
|
if not self.integers_to_read[0][last_read_digit_order + 1] == "0":
|
||||||
|
if self.order_of_last_zero_digit == len(self.integers_to_read[0]) - (
|
||||||
|
last_read_digit_order) - 2:
|
||||||
|
if i == 1:
|
||||||
|
wrd += self.ORDINAL_TENS.get(
|
||||||
|
self.integers_to_read[0][last_read_digit_order + 1], "")
|
||||||
|
return wrd
|
||||||
|
elif i > 1:
|
||||||
|
wrd += self.CARDINAL_TENS.get(
|
||||||
|
self.integers_to_read[0][last_read_digit_order + 1], "")
|
||||||
|
wrd += self.ORDINAL_TRIPLETS[i - 1]
|
||||||
|
return wrd
|
||||||
|
else:
|
||||||
|
wrd += self.CARDINAL_TENS.get(self.integers_to_read[0][last_read_digit_order + 1],
|
||||||
|
"")
|
||||||
|
|
||||||
|
if not self.integers_to_read[0][last_read_digit_order + 2] == "0":
|
||||||
|
if self.order_of_last_zero_digit == len(self.integers_to_read[0]) - (
|
||||||
|
last_read_digit_order) - 3:
|
||||||
|
if i == 1:
|
||||||
|
wrd += self.ORDINAL_ONES.get(
|
||||||
|
self.integers_to_read[0][last_read_digit_order + 2], "")
|
||||||
|
return wrd
|
||||||
|
if i == 2:
|
||||||
|
if not self.integers_to_read[0][
|
||||||
|
last_read_digit_order: last_read_digit_order + 2] == "00":
|
||||||
|
wrd += self.CARDINAL_ONES.get(
|
||||||
|
self.integers_to_read[0][last_read_digit_order + 2], "")
|
||||||
|
elif not self.integers_to_read[0][last_read_digit_order + 2] == "1":
|
||||||
|
wrd += self.CARDINAL_ONES.get(
|
||||||
|
self.integers_to_read[0][last_read_digit_order + 2], "")
|
||||||
|
wrd += self.ORDINAL_TRIPLETS[i - 1]
|
||||||
|
return wrd
|
||||||
|
if i > 2:
|
||||||
|
wrd += self.CARDINAL_ONES.get(
|
||||||
|
self.integers_to_read[0][last_read_digit_order + 2], "")
|
||||||
|
wrd += self.ORDINAL_TRIPLETS[i - 1]
|
||||||
|
return wrd
|
||||||
|
else:
|
||||||
|
if not self.integers_to_read[0][
|
||||||
|
last_read_digit_order: last_read_digit_order + 2] == "00":
|
||||||
|
wrd += self.CARDINAL_ONES.get(
|
||||||
|
self.integers_to_read[0][last_read_digit_order + 2], "")
|
||||||
|
else:
|
||||||
|
if not self.integers_to_read[0][
|
||||||
|
last_read_digit_order: last_read_digit_order + 2] == "00":
|
||||||
|
wrd += self.CARDINAL_ONES.get(
|
||||||
|
self.integers_to_read[0][last_read_digit_order + 2], "")
|
||||||
|
elif not self.integers_to_read[0][last_read_digit_order + 2] == "1":
|
||||||
|
wrd += self.CARDINAL_ONES.get(
|
||||||
|
self.integers_to_read[0][last_read_digit_order + 2], "")
|
||||||
|
|
||||||
|
wrd += self.CARDINAL_TRIPLETS[i - 1]
|
||||||
|
|
||||||
|
return wrd
|
||||||
|
|
||||||
|
def to_splitnum(self, val):
|
||||||
|
float_digits = str(int(val * 10 ** self.precision))
|
||||||
|
if not int(val) == 0:
|
||||||
|
self.integers_to_read = [str(int(val)), float_digits[len(float_digits) - self.precision:]]
|
||||||
|
else:
|
||||||
|
self.integers_to_read = ["0", "0" * (self.precision - len(float_digits)) + float_digits[len(float_digits) - self.precision:]]
|
||||||
|
if len(self.integers_to_read[0]) % 3 > 0:
|
||||||
|
self.total_triplets_to_read = (len(self.integers_to_read[0]) // 3) + 1
|
||||||
|
elif len(self.integers_to_read[0]) % 3 == 0:
|
||||||
|
self.total_triplets_to_read = len(self.integers_to_read[0]) // 3
|
||||||
|
self.total_digits_outside_triplets = len(self.integers_to_read[0]) % 3
|
||||||
|
|
||||||
|
okunacak = list(self.integers_to_read[0][::-1])
|
||||||
|
self.order_of_last_zero_digit = 0
|
||||||
|
found = 0
|
||||||
|
for i in range(len(okunacak) - 1):
|
||||||
|
if int(okunacak[i]) == 0 and found == 0:
|
||||||
|
self.order_of_last_zero_digit = i + 1
|
||||||
|
else:
|
||||||
|
found = 1
|
||||||
|
|
||||||
|
def to_currency(self, value, **kwargs):
|
||||||
|
valueparts = self.to_cardinal(value).split(self.pointword)
|
||||||
|
if len(valueparts) == 1:
|
||||||
|
return valueparts[0] + self.CURRENCY_UNIT[0]
|
||||||
|
if len(valueparts) == 2:
|
||||||
|
return self.CURRENCY_UNIT[0].join(valueparts) + self.CURRENCY_SUBUNIT[0]
|
||||||
316
num2words/lang_UK.py
Normal file
316
num2words/lang_UK.py
Normal file
@@ -0,0 +1,316 @@
|
|||||||
|
# -*- 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
|
||||||
|
u"""
|
||||||
|
>>> from textwrap import fill
|
||||||
|
|
||||||
|
>>> ' '.join([str(i) for i in splitby3('1')])
|
||||||
|
u'1'
|
||||||
|
>>> ' '.join([str(i) for i in splitby3('1123')])
|
||||||
|
u'1 123'
|
||||||
|
>>> ' '.join([str(i) for i in splitby3('1234567890')])
|
||||||
|
u'1 234 567 890'
|
||||||
|
|
||||||
|
>>> print(' '.join([n2w(i) for i in range(10)]))
|
||||||
|
нуль один два три чотири п'ять шiсть сiмь вiсiм дев'ять
|
||||||
|
|
||||||
|
>>> print(fill(' '.join([n2w(i+10) for i in range(10)])))
|
||||||
|
десять одинадцять дванадцять тринадцять чотирнадцять п'ятнадцять
|
||||||
|
шiстнадцять сiмнадцять вiсiмнадцять дев'ятнадцять
|
||||||
|
|
||||||
|
>>> print(fill(' '.join([n2w(i*10) for i in range(10)])))
|
||||||
|
нуль десять двадцять тридцять сорок п'ятдесят шiстдесят сiмдесят
|
||||||
|
вiсiмдесят дев'яносто
|
||||||
|
|
||||||
|
>>> print(n2w(100))
|
||||||
|
сто
|
||||||
|
>>> print(n2w(101))
|
||||||
|
сто один
|
||||||
|
>>> print(n2w(110))
|
||||||
|
сто десять
|
||||||
|
>>> print(n2w(115))
|
||||||
|
сто п'ятнадцять
|
||||||
|
>>> print(n2w(123))
|
||||||
|
сто двадцять три
|
||||||
|
>>> print(n2w(1000))
|
||||||
|
тисяча
|
||||||
|
>>> print(n2w(1001))
|
||||||
|
тисяча один
|
||||||
|
>>> print(n2w(2012))
|
||||||
|
двi тисячi дванадцять
|
||||||
|
|
||||||
|
>>> print(n2w(12519.85))
|
||||||
|
дванадцять тисяч п'ятсот дев'ятнадцять кома вiсiмдесят п'ять
|
||||||
|
|
||||||
|
>>> print(fill(n2w(1234567890)))
|
||||||
|
мiльярд двiстi тридцать чотири мiльйона п'ятсот шiстдесят сiмь тисяч
|
||||||
|
вiсiмсот дев'яносто
|
||||||
|
|
||||||
|
>>> print(fill(n2w(215461407892039002157189883901676)))
|
||||||
|
двiстi п'ятнадцять нонiльйонiв чотириста шiстдесят один октильйон
|
||||||
|
чотириста сiм септильйонiв вiсiмсот дев'яносто два секстильйони
|
||||||
|
тридцять дев'ять квiнтильйонiв два квадрильйони сто п'ятдесят сiм
|
||||||
|
трильйонiв сто вiсiмдесят дев'ять мiльярдiв вiсiмсот вiсiмдесят три
|
||||||
|
мiльйона дев'ятсот одна тисяча шiстсот сiмдесят шiсть
|
||||||
|
|
||||||
|
>>> print(fill(n2w(719094234693663034822824384220291)))
|
||||||
|
сiмсот дев'ятнадцять нонiльйонiв дев'яносто чотири октильйони двiстi
|
||||||
|
тридцять чотири септильйони шiстсот дев'яносто три секстильйони
|
||||||
|
шiстсот шiстдесят три квiнтильйони тридцять чотири квадрильйони
|
||||||
|
вiсiмсот двадцять два трильйони вiсiмсот двадцять чотири мiльярди
|
||||||
|
триста вiсiмдесят чотири мiльйона двiстi двадцять тисяч двiстi
|
||||||
|
дев'яносто один
|
||||||
|
|
||||||
|
>>> print(to_currency(1.0, 'EUR'))
|
||||||
|
один євро, нуль центiв
|
||||||
|
|
||||||
|
>>> print(to_currency(1.0, 'UAH'))
|
||||||
|
одна гривня, нуль копiйок
|
||||||
|
|
||||||
|
>>> print(to_currency(1234.56, 'EUR'))
|
||||||
|
тисяча двiстi тридцять чотири євро, п'ятдесят шiсть центiв
|
||||||
|
|
||||||
|
>>> print(to_currency(1234.56, 'UAH'))
|
||||||
|
тисяча двiстi тридцять чотири гривнi, п'ятдесят шiсть копiйок
|
||||||
|
|
||||||
|
>>> print(to_currency(10111, 'EUR', seperator=u' та'))
|
||||||
|
сто один євро та одинадцять центiв
|
||||||
|
|
||||||
|
>>> print(to_currency(10121, 'UAH', seperator=u' та'))
|
||||||
|
сто одна гривня та двадцять одна копiйка
|
||||||
|
|
||||||
|
>>> print(to_currency(10122, 'UAH', seperator=u' та'))
|
||||||
|
сто одна гривня та двадцять одна копiйка
|
||||||
|
|
||||||
|
>>> print(to_currency(10121, 'EUR', seperator=u' та'))
|
||||||
|
сто один євро та двадцять один цент
|
||||||
|
|
||||||
|
>>> print(to_currency(-1251985, cents = False))
|
||||||
|
мiнус дванадцять тисяч п'ятьсот дев'ятнадцять євро, 85 центiв
|
||||||
|
"""
|
||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
ZERO = (u'нуль',)
|
||||||
|
|
||||||
|
ONES_FEMININE = {
|
||||||
|
1: (u'одна',),
|
||||||
|
2: (u'двi',),
|
||||||
|
3: (u'три',),
|
||||||
|
4: (u'чотири',),
|
||||||
|
5: (u'п\'ять',),
|
||||||
|
6: (u'шiсть',),
|
||||||
|
7: (u'сiм',),
|
||||||
|
8: (u'вiсiм',),
|
||||||
|
9: (u'дев\'ять',),
|
||||||
|
}
|
||||||
|
|
||||||
|
ONES = {
|
||||||
|
1: (u'один',),
|
||||||
|
2: (u'два',),
|
||||||
|
3: (u'три',),
|
||||||
|
4: (u'чотири',),
|
||||||
|
5: (u'п\'ять',),
|
||||||
|
6: (u'шiсть',),
|
||||||
|
7: (u'сiм',),
|
||||||
|
8: (u'вiсiм',),
|
||||||
|
9: (u'дев\'ять',),
|
||||||
|
}
|
||||||
|
|
||||||
|
TENS = {
|
||||||
|
0: (u'десять',),
|
||||||
|
1: (u'одинадцять',),
|
||||||
|
2: (u'дванадцять',),
|
||||||
|
3: (u'тринадцять',),
|
||||||
|
4: (u'чотирнадцять',),
|
||||||
|
5: (u'п\'ятнадцять',),
|
||||||
|
6: (u'шiстнадцять',),
|
||||||
|
7: (u'сiмнадцять',),
|
||||||
|
8: (u'вiсiмнадцять',),
|
||||||
|
9: (u'дев\'ятнадцять',),
|
||||||
|
}
|
||||||
|
|
||||||
|
TWENTIES = {
|
||||||
|
2: (u'двадцять',),
|
||||||
|
3: (u'тридцять',),
|
||||||
|
4: (u'сорок',),
|
||||||
|
5: (u'п\'ятдесят',),
|
||||||
|
6: (u'шiстдесят',),
|
||||||
|
7: (u'сiмдесят',),
|
||||||
|
8: (u'вiсiмдесят',),
|
||||||
|
9: (u'дев\'яносто',),
|
||||||
|
}
|
||||||
|
|
||||||
|
HUNDREDS = {
|
||||||
|
1: (u'сто',),
|
||||||
|
2: (u'двiстi',),
|
||||||
|
3: (u'триста',),
|
||||||
|
4: (u'чотириста',),
|
||||||
|
5: (u'п\'ятсот',),
|
||||||
|
6: (u'шiстсот',),
|
||||||
|
7: (u'сiмсот',),
|
||||||
|
8: (u'вiсiмсот',),
|
||||||
|
9: (u'дев\'ятсот',),
|
||||||
|
}
|
||||||
|
|
||||||
|
THOUSANDS = {
|
||||||
|
1: (u'тисяча', u'тисячi', u'тисяч'), # 10^3
|
||||||
|
2: (u'мiльйон', u'мiльйони', u'мiльйонiв'), # 10^6
|
||||||
|
3: (u'мiльярд', u'мiльярди', u'мiльярдiв'), # 10^9
|
||||||
|
4: (u'трильйон', u'трильйони', u'трильйонiв'), # 10^12
|
||||||
|
5: (u'квадрильйон', u'квадрильйони', u'квадрильйонiв'), # 10^15
|
||||||
|
6: (u'квiнтильйон', u'квiнтильйони', u'квiнтильйонiв'), # 10^18
|
||||||
|
7: (u'секстильйон', u'секстильйони', u'секстильйонiв'), # 10^21
|
||||||
|
8: (u'септильйон', u'септильйони', u'септильйонiв'), # 10^24
|
||||||
|
9: (u'октильйон', u'октильйони', u'октильйонiв'), #10^27
|
||||||
|
10: (u'нонiльйон', u'нонiльйони', u'нонiльйонiв'), # 10^30
|
||||||
|
}
|
||||||
|
|
||||||
|
CURRENCIES = {
|
||||||
|
'UAH': (
|
||||||
|
(u'гривня', u'гривнi', u'гривень'), (u'копiйка', u'копiйки', u'копiйок')
|
||||||
|
),
|
||||||
|
'EUR': (
|
||||||
|
(u'евро', u'евро', u'евро'), (u'цент', u'центи', u'центiв')
|
||||||
|
),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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):
|
||||||
|
#form = 0 if n==1 else 1 if (n % 10 > 1 and n % 10 < 5 and (n % 100 < 10 or n % 100 > 20)) else 2
|
||||||
|
if (n % 100 < 10 or n % 100 > 20):
|
||||||
|
if n % 10 == 1:
|
||||||
|
form = 0
|
||||||
|
elif (n % 10 > 1 and n % 10 < 5):
|
||||||
|
form = 1
|
||||||
|
else:
|
||||||
|
form = 2
|
||||||
|
else:
|
||||||
|
form = 2
|
||||||
|
|
||||||
|
return forms[form]
|
||||||
|
|
||||||
|
|
||||||
|
def int2word(n, feminine=True):
|
||||||
|
if n < 0:
|
||||||
|
return ' '.join([u'мiнус', int2word(abs(n))])
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
if n3 > 0:
|
||||||
|
words.append(HUNDREDS[n3][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):
|
||||||
|
elif n1 > 0:
|
||||||
|
ones = ONES_FEMININE if i == 1 or feminine and i == 0 else ONES
|
||||||
|
words.append(ones[n1][0])
|
||||||
|
|
||||||
|
|
||||||
|
if i > 0 and ((n1+n2+n3) > 0):
|
||||||
|
words.append(pluralize(x, THOUSANDS[i]))
|
||||||
|
|
||||||
|
return ' '.join(words)
|
||||||
|
|
||||||
|
|
||||||
|
def n2w(n):
|
||||||
|
n = str(n).replace(',', '.')
|
||||||
|
if '.' in n:
|
||||||
|
left, right = n.split('.')
|
||||||
|
return u'%s кома %s' % (int2word(int(left)), int2word(int(right)))
|
||||||
|
else:
|
||||||
|
return int2word(int(n))
|
||||||
|
|
||||||
|
|
||||||
|
def to_currency(n, currency='EUR', cents=True, seperator=','):
|
||||||
|
if type(n) == int:
|
||||||
|
if n < 0:
|
||||||
|
minus = True
|
||||||
|
else:
|
||||||
|
minus = False
|
||||||
|
|
||||||
|
n = abs(n)
|
||||||
|
left = n / 100
|
||||||
|
right = n % 100
|
||||||
|
else:
|
||||||
|
n = str(n).replace(',', '.')
|
||||||
|
if '.' in n:
|
||||||
|
left, right = n.split('.')
|
||||||
|
else:
|
||||||
|
left, right = n, 0
|
||||||
|
left, right = int(left), int(right)
|
||||||
|
minus = False
|
||||||
|
cr1, cr2 = CURRENCIES[currency]
|
||||||
|
|
||||||
|
if minus:
|
||||||
|
minus_str = "мiнус "
|
||||||
|
else:
|
||||||
|
minus_str = ""
|
||||||
|
|
||||||
|
if cents:
|
||||||
|
cents_feminine = currency == 'UAH'
|
||||||
|
cents_str = int2word(right, cents_feminine)
|
||||||
|
else:
|
||||||
|
cents_str = "%02d" % right
|
||||||
|
|
||||||
|
return u'%s%s %s%s %s %s' % (
|
||||||
|
minus_str,
|
||||||
|
int2word(left),
|
||||||
|
pluralize(left, cr1),
|
||||||
|
seperator,
|
||||||
|
cents_str,
|
||||||
|
pluralize(right, cr2)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class Num2Word_UK(object):
|
||||||
|
def to_cardinal(self, number):
|
||||||
|
return n2w(number)
|
||||||
|
|
||||||
|
def to_ordinal(self, number):
|
||||||
|
raise NotImplementedError()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import doctest
|
||||||
|
doctest.testmod()
|
||||||
102
num2words/lang_VN.py
Normal file
102
num2words/lang_VN.py
Normal file
@@ -0,0 +1,102 @@
|
|||||||
|
# -*- 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
|
||||||
|
|
||||||
|
|
||||||
|
to_19 = (u'không', u'một', u'hai', u'ba', u'bốn', u'năm', u'sáu',
|
||||||
|
u'bảy', u'tám', u'chín', u'mười', u'mười một', u'mười hai',
|
||||||
|
u'mười ba', u'mười bốn', u'mười lăm', u'mười sáu', u'mười bảy',
|
||||||
|
u'mười tám', u'mười chín')
|
||||||
|
tens = (u'hai mươi', u'ba mươi', u'bốn mươi', u'năm mươi',
|
||||||
|
u'sáu mươi', u'bảy mươi', u'tám mươi', u'chín mươi')
|
||||||
|
denom = ('',
|
||||||
|
u'nghìn', u'triệu', u'tỷ', u'nghìn tỷ', u'trăm nghìn tỷ',
|
||||||
|
'Quintillion', 'Sextillion', 'Septillion', 'Octillion', 'Nonillion',
|
||||||
|
'Decillion', 'Undecillion', 'Duodecillion', 'Tredecillion',
|
||||||
|
'Quattuordecillion', 'Sexdecillion', 'Septendecillion',
|
||||||
|
'Octodecillion', 'Novemdecillion', 'Vigintillion')
|
||||||
|
|
||||||
|
|
||||||
|
class Num2Word_VN(object):
|
||||||
|
|
||||||
|
def _convert_nn(self, val):
|
||||||
|
if val < 20:
|
||||||
|
return to_19[val]
|
||||||
|
for (dcap, dval) in ((k, 20 + (10 * v)) for (v, k) in enumerate(tens)):
|
||||||
|
if dval + 10 > val:
|
||||||
|
if val % 10:
|
||||||
|
a = u'lăm'
|
||||||
|
if to_19[val % 10] == u'một':
|
||||||
|
a = u'mốt'
|
||||||
|
else:
|
||||||
|
a = to_19[val % 10]
|
||||||
|
if to_19[val % 10] == u'năm':
|
||||||
|
a = u'lăm'
|
||||||
|
return dcap + ' ' + a
|
||||||
|
return dcap
|
||||||
|
|
||||||
|
def _convert_nnn(self, val):
|
||||||
|
word = ''
|
||||||
|
(mod, rem) = (val % 100, val // 100)
|
||||||
|
if rem > 0:
|
||||||
|
word = to_19[rem] + u' trăm'
|
||||||
|
if mod > 0:
|
||||||
|
word = word + ' '
|
||||||
|
if mod > 0 and mod < 10:
|
||||||
|
if mod == 5:
|
||||||
|
word = word != '' and word + u'lẻ năm' or word + u'năm'
|
||||||
|
else:
|
||||||
|
word = word != '' and word + u'lẻ ' \
|
||||||
|
+ self._convert_nn(mod) or word + self._convert_nn(mod)
|
||||||
|
if mod >= 10:
|
||||||
|
word = word + self._convert_nn(mod)
|
||||||
|
return word
|
||||||
|
|
||||||
|
def vietnam_number(self, val):
|
||||||
|
if val < 100:
|
||||||
|
return self._convert_nn(val)
|
||||||
|
if val < 1000:
|
||||||
|
return self._convert_nnn(val)
|
||||||
|
for (didx, dval) in ((v - 1, 1000 ** v) for v in range(len(denom))):
|
||||||
|
if dval > val:
|
||||||
|
mod = 1000 ** didx
|
||||||
|
l = val // mod
|
||||||
|
r = val - (l * mod)
|
||||||
|
|
||||||
|
ret = self._convert_nnn(l) + u' ' + denom[didx]
|
||||||
|
if r > 0 and r <= 99:
|
||||||
|
ret = self._convert_nnn(l) + u' ' + denom[didx] + u' lẻ'
|
||||||
|
if r > 0:
|
||||||
|
ret = ret + ' ' + self.vietnam_number(r)
|
||||||
|
return ret
|
||||||
|
|
||||||
|
def number_to_text(self, number):
|
||||||
|
number = '%.2f' % number
|
||||||
|
the_list = str(number).split('.')
|
||||||
|
start_word = self.vietnam_number(int(the_list[0]))
|
||||||
|
final_result = start_word
|
||||||
|
if len(the_list) > 1 and int(the_list[1]) > 0:
|
||||||
|
end_word = self.vietnam_number(int(the_list[1]))
|
||||||
|
final_result = final_result + ' phẩy ' + end_word
|
||||||
|
return final_result
|
||||||
|
|
||||||
|
def to_cardinal(self, number):
|
||||||
|
return self.number_to_text(number)
|
||||||
|
|
||||||
|
def to_ordinal(self, number):
|
||||||
|
return self.to_cardinal(number)
|
||||||
4
setup.py
4
setup.py
@@ -17,7 +17,7 @@ LONG_DESC = open('README.rst', 'rt').read() + '\n\n' + open('CHANGES.rst', 'rt')
|
|||||||
|
|
||||||
setup(
|
setup(
|
||||||
name='num2words',
|
name='num2words',
|
||||||
version='0.5.3',
|
version='0.5.5',
|
||||||
description='Modules to convert numbers to words. Easily extensible.',
|
description='Modules to convert numbers to words. Easily extensible.',
|
||||||
long_description=LONG_DESC,
|
long_description=LONG_DESC,
|
||||||
license='LGPL',
|
license='LGPL',
|
||||||
@@ -29,5 +29,5 @@ setup(
|
|||||||
url='https://github.com/savoirfairelinux/num2words',
|
url='https://github.com/savoirfairelinux/num2words',
|
||||||
packages=find_packages(exclude=['tests']),
|
packages=find_packages(exclude=['tests']),
|
||||||
test_suite='tests',
|
test_suite='tests',
|
||||||
use_2to3=True,
|
classifiers=CLASSIFIERS,
|
||||||
)
|
)
|
||||||
|
|||||||
62
tests/test_ar.py
Normal file
62
tests/test_ar.py
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
# encoding: UTF-8
|
||||||
|
# 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
|
||||||
|
|
||||||
|
TEST_CASES_CARDINAL = (
|
||||||
|
(1, 'واحد'),
|
||||||
|
(2, 'اثنين'),
|
||||||
|
(11, 'أحد عشر'),
|
||||||
|
(12, 'اثناعشر'),
|
||||||
|
(20, 'عشرين'),
|
||||||
|
(21, 'واحد وعشرين'),
|
||||||
|
(26, 'ستة وعشرين'),
|
||||||
|
(30, 'ثلاثين'),
|
||||||
|
(67, 'سبعة وستين'),
|
||||||
|
(70, 'سبعين'),
|
||||||
|
(100, 'مئة'),
|
||||||
|
(101, 'مئة و واحد'),
|
||||||
|
(199, 'مئة و تسعة وتسعين'),
|
||||||
|
(203, 'مئتين و ثلاثة'),
|
||||||
|
(1000, 'ألف'),
|
||||||
|
(1001, 'ألف و واحد'),
|
||||||
|
(1097, 'ألف و سبعة وتسعين'),
|
||||||
|
(1000000, 'مليون'),
|
||||||
|
(1000001, 'مليون و واحد'),
|
||||||
|
)
|
||||||
|
|
||||||
|
TEST_CASES_ORDINAL = (
|
||||||
|
(1, 'أول'),
|
||||||
|
(8, 'ثامن'),
|
||||||
|
(12, 'ثاني عشر'),
|
||||||
|
(100, 'مئة'),
|
||||||
|
)
|
||||||
|
|
||||||
|
class Num2WordsARTest(TestCase):
|
||||||
|
|
||||||
|
def test_number(self):
|
||||||
|
for test in TEST_CASES_CARDINAL:
|
||||||
|
self.assertEqual(num2words(test[0], lang='ar'), test[1])
|
||||||
|
|
||||||
|
def test_ordinal(self):
|
||||||
|
for test in TEST_CASES_ORDINAL:
|
||||||
|
self.assertEqual(
|
||||||
|
num2words(test[0], lang='ar', ordinal=True),
|
||||||
|
test[1]
|
||||||
|
)
|
||||||
51
tests/test_de.py
Normal file
51
tests/test_de.py
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
# -*- encoding: utf-8 -*-
|
||||||
|
# Copyright (c) 2015, 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 Num2WordsDETest(TestCase):
|
||||||
|
def test_ordinal_less_than_twenty(self):
|
||||||
|
self.assertEqual(num2words(7, ordinal=True, lang='de'), "siebte")
|
||||||
|
self.assertEqual(num2words(8, ordinal=True, lang='de'), "achte")
|
||||||
|
self.assertEqual(num2words(12, ordinal=True, lang='de'), "zwölfte")
|
||||||
|
self.assertEqual(num2words(17, ordinal=True, lang='de'), "siebzehnte")
|
||||||
|
|
||||||
|
def test_ordinal_more_than_twenty(self):
|
||||||
|
self.assertEqual(num2words(81, ordinal=True, lang='de'), "einundachtzigste")
|
||||||
|
|
||||||
|
def test_ordinal_at_crucial_number(self):
|
||||||
|
self.assertEqual(num2words(100, ordinal=True, lang='de'), "hundertste")
|
||||||
|
self.assertEqual(num2words(1000, ordinal=True, lang='de'), "tausendste")
|
||||||
|
self.assertEqual(num2words(4000, ordinal=True, lang='de'), "viertausendste")
|
||||||
|
self.assertEqual(num2words(2000000, ordinal=True, lang='de'), "zwei millionenste")
|
||||||
|
self.assertEqual(num2words(5000000000, ordinal=True, lang='de'), "fünf milliardenste")
|
||||||
|
|
||||||
|
def test_cardinal_at_some_numbers(self):
|
||||||
|
self.assertEqual(num2words(2000000, lang='de'), "zwei millionen")
|
||||||
|
self.assertEqual(num2words(4000000000, lang='de'), "vier milliarden")
|
||||||
|
|
||||||
|
def test_cardinal_for_decimal_number(self):
|
||||||
|
self.assertEqual(num2words(3.486, lang='de'), "drei Komma vier acht")
|
||||||
|
|
||||||
|
def test_ordinal_for_negative_numbers(self):
|
||||||
|
self.assertRaises(TypeError, num2words, -12, ordinal=True, lang='de')
|
||||||
|
|
||||||
|
def test_ordinal_for_floating_numbers(self):
|
||||||
|
self.assertRaises(TypeError, num2words, 2.453, ordinal=True, lang='de')
|
||||||
@@ -21,3 +21,10 @@ class Num2WordsENTest(TestCase):
|
|||||||
def test_and_join_199(self):
|
def test_and_join_199(self):
|
||||||
# ref https://github.com/savoirfairelinux/num2words/issues/8
|
# ref https://github.com/savoirfairelinux/num2words/issues/8
|
||||||
self.assertEqual(num2words(199), "one hundred and ninety-nine")
|
self.assertEqual(num2words(199), "one hundred and ninety-nine")
|
||||||
|
|
||||||
|
def test_cardinal_for_float_number(self):
|
||||||
|
# issue 24
|
||||||
|
self.assertEqual(num2words(12.50), "twelve point five zero")
|
||||||
|
self.assertEqual(num2words(12.51), "twelve point five one")
|
||||||
|
self.assertEqual(num2words(12.53), "twelve point five three")
|
||||||
|
self.assertEqual(num2words(12.59), "twelve point five nine")
|
||||||
95
tests/test_es.py
Normal file
95
tests/test_es.py
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
# encoding: UTF-8
|
||||||
|
# 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
|
||||||
|
|
||||||
|
TEST_CASES_CARDINAL = (
|
||||||
|
(1, 'uno'),
|
||||||
|
(2, 'dos'),
|
||||||
|
(3, 'tres'),
|
||||||
|
(11, 'once'),
|
||||||
|
(12, 'doce'),
|
||||||
|
(16, 'dieciseis'),
|
||||||
|
(19, 'diecinueve'),
|
||||||
|
(20, 'veinte'),
|
||||||
|
(21, 'veintiuno'),
|
||||||
|
(26, 'veintiséis'),
|
||||||
|
(28, 'veintiocho'),
|
||||||
|
(30, 'treinta'),
|
||||||
|
(31, 'treinta y uno'),
|
||||||
|
(40, 'cuarenta'),
|
||||||
|
(44, 'cuarenta y cuatro'),
|
||||||
|
(50, 'cincuenta'),
|
||||||
|
(55, 'cincuenta y cinco'),
|
||||||
|
(60, 'sesenta'),
|
||||||
|
(67, 'sesenta y siete'),
|
||||||
|
(70, 'setenta'),
|
||||||
|
(79, 'setenta y nueve'),
|
||||||
|
(100, 'cien'),
|
||||||
|
(101, 'ciento uno'),
|
||||||
|
(199, 'ciento noventa y nueve'),
|
||||||
|
(203, 'doscientos tres'),
|
||||||
|
(287, 'doscientos ochenta y siete'),
|
||||||
|
(300, 'trescientos'),
|
||||||
|
(356, 'trescientos cincuenta y seis'),
|
||||||
|
(400, 'cuatrocientos'),
|
||||||
|
(434, 'cuatrocientos treinta y cuatro'),
|
||||||
|
(578, 'quinientos setenta y ocho'),
|
||||||
|
(689, 'seiscientos ochenta y nueve'),
|
||||||
|
(729, 'setecientos veintinueve'),
|
||||||
|
(894, 'ochocientos noventa y cuatro'),
|
||||||
|
(999, 'novecientos noventa y nueve'),
|
||||||
|
(1000, 'mil'),
|
||||||
|
(1001, 'mil uno'),
|
||||||
|
(1097, 'mil noventa y siete'),
|
||||||
|
(1104, 'mil ciento cuatro'),
|
||||||
|
(1243, 'mil doscientos cuarenta y tres'),
|
||||||
|
(2385, 'dos mil trescientos ochenta y cinco'),
|
||||||
|
(3766, 'tres mil setecientos sesenta y seis'),
|
||||||
|
(4196, 'cuatro mil ciento noventa y seis'),
|
||||||
|
(5846, 'cinco mil ochocientos cuarenta y seis'),
|
||||||
|
(6459, 'seis mil cuatrocientos cincuenta y nueve'),
|
||||||
|
(7232, 'siete mil doscientos treinta y dos'),
|
||||||
|
(8569, 'ocho mil quinientos sesenta y nueve'),
|
||||||
|
(9539, 'nueve mil quinientos treinta y nueve'),
|
||||||
|
(1000000, 'un millón'),
|
||||||
|
(1000001, 'un millón uno'),
|
||||||
|
)
|
||||||
|
|
||||||
|
TEST_CASES_ORDINAL = (
|
||||||
|
(1, 'primero'),
|
||||||
|
(8, 'octavo'),
|
||||||
|
(12, 'décimosegundo'),
|
||||||
|
(14, 'décimo cuarto'),
|
||||||
|
(28, 'vigésimo octavo'),
|
||||||
|
(100, 'centésimo'),
|
||||||
|
)
|
||||||
|
|
||||||
|
class Num2WordsESTest(TestCase):
|
||||||
|
|
||||||
|
def test_number(self):
|
||||||
|
for test in TEST_CASES_CARDINAL:
|
||||||
|
self.assertEqual(num2words(test[0], lang='es'), test[1])
|
||||||
|
|
||||||
|
def test_ordinal(self):
|
||||||
|
for test in TEST_CASES_ORDINAL:
|
||||||
|
self.assertEqual(
|
||||||
|
num2words(test[0], lang='es', ordinal=True),
|
||||||
|
test[1]
|
||||||
|
)
|
||||||
34
tests/test_es_co.py
Normal file
34
tests/test_es_co.py
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
# encoding: UTF-8
|
||||||
|
# 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 num2words import num2words
|
||||||
|
from . import test_es
|
||||||
|
|
||||||
|
|
||||||
|
class Num2WordsESCOTest(test_es.Num2WordsESTest):
|
||||||
|
|
||||||
|
def test_number(self):
|
||||||
|
for test in test_es.TEST_CASES_CARDINAL:
|
||||||
|
self.assertEqual(num2words(test[0], lang='es_CO'), test[1])
|
||||||
|
|
||||||
|
def test_ordinal(self):
|
||||||
|
for test in test_es.TEST_CASES_ORDINAL:
|
||||||
|
self.assertEqual(
|
||||||
|
num2words(test[0], lang='es_CO', ordinal=True),
|
||||||
|
test[1]
|
||||||
|
)
|
||||||
34
tests/test_es_ve.py
Normal file
34
tests/test_es_ve.py
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
# encoding: UTF-8
|
||||||
|
# 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 num2words import num2words
|
||||||
|
from . import test_es
|
||||||
|
|
||||||
|
|
||||||
|
class Num2WordsESVETest(test_es.Num2WordsESTest):
|
||||||
|
|
||||||
|
def test_number(self):
|
||||||
|
for test in test_es.TEST_CASES_CARDINAL:
|
||||||
|
self.assertEqual(num2words(test[0], lang='es_VE'), test[1])
|
||||||
|
|
||||||
|
def test_ordinal(self):
|
||||||
|
for test in test_es.TEST_CASES_ORDINAL:
|
||||||
|
self.assertEqual(
|
||||||
|
num2words(test[0], lang='es_VE', ordinal=True),
|
||||||
|
test[1]
|
||||||
|
)
|
||||||
36
tests/test_fr_ch.py
Normal file
36
tests/test_fr_ch.py
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
# -*- encoding: utf-8 -*-
|
||||||
|
# Copyright (c) 2015, 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 Num2WordsENTest(TestCase):
|
||||||
|
def test_ordinal_special_joins(self):
|
||||||
|
self.assertEqual(num2words(5, ordinal=True, lang='fr_CH'), "cinquième")
|
||||||
|
self.assertEqual(num2words(6, ordinal=True, lang='fr_CH'), "sixième")
|
||||||
|
self.assertEqual(num2words(35, ordinal=True, lang='fr_CH'), "trente-cinquième")
|
||||||
|
self.assertEqual(num2words(9, ordinal=True, lang='fr_CH'), "neuvième")
|
||||||
|
self.assertEqual(num2words(49, ordinal=True, lang='fr_CH'), "quarante-neuvième")
|
||||||
|
self.assertEqual(num2words(71, lang='fr_CH'), "septante et un")
|
||||||
|
self.assertEqual(num2words(81, lang='fr_CH'), "huitante et un")
|
||||||
|
self.assertEqual(num2words(80, lang='fr_CH'), "huitante")
|
||||||
|
self.assertEqual(num2words(880, lang='fr_CH'), "huit cents huitante")
|
||||||
|
self.assertEqual(num2words(91, ordinal=True, lang='fr_CH'), "nonante et unième")
|
||||||
|
self.assertEqual(num2words(53, lang='fr_CH'), "cinquante-trois")
|
||||||
|
|
||||||
27
tests/test_fr_dz.py
Normal file
27
tests/test_fr_dz.py
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
# -*- encoding: utf-8 -*-
|
||||||
|
# Copyright (c) 2015, 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
|
||||||
|
from num2words.lang_FR_DZ import to_currency
|
||||||
|
|
||||||
|
class Num2WordsPLTest(TestCase):
|
||||||
|
def test_currency(self):
|
||||||
|
self.assertEqual(to_currency(1234.12), "mille deux cent trente-quatre dinards virgule douze centimes")
|
||||||
|
self.assertEqual(to_currency(45689.89), "quarante-cinq mille six cent quatre-vingt-neuf dinards virgule quatre-vingt-neuf centimes")
|
||||||
49
tests/test_id.py
Normal file
49
tests/test_id.py
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
# 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 unittest import TestCase
|
||||||
|
|
||||||
|
from num2words import num2words
|
||||||
|
|
||||||
|
class Num2WordsIDTest(TestCase):
|
||||||
|
def test_cardinal_for_natural_number(self):
|
||||||
|
self.assertEqual(num2words(10, lang='id'), "sepuluh")
|
||||||
|
self.assertEqual(num2words(11, lang='id'), "sebelas")
|
||||||
|
self.assertEqual(num2words(108, lang='id'), "seratus delapan")
|
||||||
|
self.assertEqual(num2words(1075, lang='id'), "seribu tujuh puluh lima")
|
||||||
|
self.assertEqual(num2words(1087231, lang='id'), "satu juta delapan puluh tujuh ribu dua ratus tiga puluh satu")
|
||||||
|
self.assertEqual(num2words(1000000408, lang='id'), "satu miliar empat ratus delapan")
|
||||||
|
|
||||||
|
def test_cardinal_for_decimal_number(self):
|
||||||
|
self.assertEqual(num2words(12.234, lang='id'), "dua belas koma dua tiga empat")
|
||||||
|
self.assertEqual(num2words(9.076, lang='id'), "sembilan koma nol tujuh enam")
|
||||||
|
|
||||||
|
def test_cardinal_for_negative_number(self):
|
||||||
|
self.assertEqual(num2words(-923, lang='id'), "min sembilan ratus dua puluh tiga")
|
||||||
|
self.assertEqual(num2words(-0.234, lang='id'), "min nol koma dua tiga empat")
|
||||||
|
|
||||||
|
def test_ordinal_for_natural_number(self):
|
||||||
|
self.assertEqual(num2words(1, ordinal=True, lang='id'), "pertama")
|
||||||
|
self.assertEqual(num2words(10, ordinal=True, lang='id'), "kesepuluh")
|
||||||
|
|
||||||
|
#def test_ordinal_numeric_for_natural_number(self):
|
||||||
|
# self.assertEqual(num2words(1, ordinal=True, lang='id'), "ke-1")
|
||||||
|
# self.assertEqual(num2words(10, ordinal=True, lang='id'), "ke-10")
|
||||||
|
|
||||||
|
def test_ordinal_for_negative_number(self):
|
||||||
|
self.assertRaises(TypeError, num2words, -12, ordinal=True, lang='id')
|
||||||
|
|
||||||
|
def test_ordinal_for_floating_number(self):
|
||||||
|
self.assertRaises(TypeError, num2words, 3.243, ordinal=True, lang='id')
|
||||||
135
tests/test_it.py
Normal file
135
tests/test_it.py
Normal file
@@ -0,0 +1,135 @@
|
|||||||
|
# -*- encoding: utf-8 -*-
|
||||||
|
# Copyright (c) 2015, 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 Num2WordsITTest(TestCase):
|
||||||
|
|
||||||
|
maxDiff = None
|
||||||
|
|
||||||
|
def test_negative(self):
|
||||||
|
number = 648972145
|
||||||
|
pos_crd = num2words(+number, lang="it")
|
||||||
|
neg_crd = num2words(-number, lang="it")
|
||||||
|
pos_ord = num2words(+number, lang="it", ordinal=True)
|
||||||
|
neg_ord = num2words(-number, lang="it", ordinal=True)
|
||||||
|
self.assertEqual("meno " + pos_crd, neg_crd)
|
||||||
|
self.assertEqual("meno " + pos_ord, neg_ord)
|
||||||
|
|
||||||
|
def test_float_to_cardinal(self):
|
||||||
|
self.assertTrue("tre virgola uno quattro uno" in num2words(3.1415, lang="it"))
|
||||||
|
self.assertTrue("meno cinque virgola uno" in num2words(-5.15, lang="it"))
|
||||||
|
self.assertTrue("meno zero virgola uno" in num2words(-0.15, lang="it"))
|
||||||
|
|
||||||
|
def test_float_to_ordinal(self):
|
||||||
|
self.assertTrue("terzo virgola uno quattro uno" in num2words(3.1415, lang="it", ordinal=True))
|
||||||
|
self.assertTrue("meno quinto virgola uno" in num2words(-5.15, lang="it", ordinal=True))
|
||||||
|
self.assertTrue("meno zero virgola uno" in num2words(-0.15, lang="it", ordinal=True))
|
||||||
|
|
||||||
|
def test_0(self):
|
||||||
|
self.assertEqual(num2words(0, lang="it"), "zero")
|
||||||
|
self.assertEqual(num2words(0, lang="it", ordinal=True), "zero")
|
||||||
|
|
||||||
|
def test_1_to_10(self):
|
||||||
|
self.assertEqual(num2words(1, lang="it"), "uno")
|
||||||
|
self.assertEqual(num2words(2, lang="it"), "due")
|
||||||
|
self.assertEqual(num2words(7, lang="it"), "sette")
|
||||||
|
self.assertEqual(num2words(10, lang="it"), "dieci")
|
||||||
|
|
||||||
|
def test_11_to_19(self):
|
||||||
|
self.assertEqual(num2words(11, lang="it"), "undici")
|
||||||
|
self.assertEqual(num2words(13, lang="it"), "tredici")
|
||||||
|
self.assertEqual(num2words(15, lang="it"), "quindici")
|
||||||
|
self.assertEqual(num2words(16, lang="it"), "sedici")
|
||||||
|
self.assertEqual(num2words(19, lang="it"), "diciannove")
|
||||||
|
|
||||||
|
def test_20_to_99(self):
|
||||||
|
self.assertEqual(num2words(20, lang="it"), "venti")
|
||||||
|
self.assertEqual(num2words(23, lang="it"), "ventitré")
|
||||||
|
self.assertEqual(num2words(28, lang="it"), "ventotto")
|
||||||
|
self.assertEqual(num2words(31, lang="it"), "trentuno")
|
||||||
|
self.assertEqual(num2words(40, lang="it"), "quaranta")
|
||||||
|
self.assertEqual(num2words(66, lang="it"), "sessantasei")
|
||||||
|
self.assertEqual(num2words(92, lang="it"), "novantadue")
|
||||||
|
|
||||||
|
def test_100_to_999(self):
|
||||||
|
self.assertEqual(num2words(100, lang="it"), "cento")
|
||||||
|
self.assertEqual(num2words(111, lang="it"), "centoundici")
|
||||||
|
self.assertEqual(num2words(150, lang="it"), "centocinquanta")
|
||||||
|
self.assertEqual(num2words(196, lang="it"), "centonovantasei")
|
||||||
|
self.assertEqual(num2words(200, lang="it"), "duecento")
|
||||||
|
self.assertEqual(num2words(210, lang="it"), "duecentodieci")
|
||||||
|
self.assertEqual(num2words(701, lang="it"), "settecentouno")
|
||||||
|
|
||||||
|
def test_1000_to_9999(self):
|
||||||
|
self.assertEqual(num2words(1000, lang="it"), "mille")
|
||||||
|
self.assertEqual(num2words(1001, lang="it"), "milleuno")
|
||||||
|
self.assertEqual(num2words(1500, lang="it"), "millecinquecento")
|
||||||
|
self.assertEqual(num2words(7378, lang="it"), "settemilatrecentosettantotto")
|
||||||
|
self.assertEqual(num2words(2000, lang="it"), "duemila")
|
||||||
|
self.assertEqual(num2words(2100, lang="it"), "duemilacento")
|
||||||
|
self.assertEqual(num2words(6870, lang="it"), "seimilaottocentosettanta")
|
||||||
|
self.assertEqual(num2words(10000, lang="it"), "diecimila")
|
||||||
|
self.assertEqual(num2words(98765, lang="it"), "novantottomilasettecentosessantacinque")
|
||||||
|
self.assertEqual(num2words(100000, lang="it"), "centomila")
|
||||||
|
self.assertEqual(num2words(523456, lang="it"), "cinquecentoventitremilaquattrocentocinquantasei")
|
||||||
|
|
||||||
|
def test_big(self):
|
||||||
|
self.assertEqual(num2words(1000000, lang="it"), "un milione")
|
||||||
|
self.assertEqual(num2words(1000007, lang="it"), "un milione e sette")
|
||||||
|
self.assertEqual(num2words(1200000, lang="it"), "un milione e duecentomila")
|
||||||
|
self.assertEqual(num2words(3000000, lang="it"), "tre milioni")
|
||||||
|
self.assertEqual(num2words(3000005, lang="it"), "tre milioni e cinque")
|
||||||
|
self.assertEqual(num2words(3800000, lang="it"), "tre milioni e ottocentomila")
|
||||||
|
self.assertEqual(num2words(1000000000, lang="it"), "un miliardo")
|
||||||
|
self.assertEqual(num2words(1000000017, lang="it"), "un miliardo e diciassette")
|
||||||
|
self.assertEqual(num2words(2000000000, lang="it"), "due miliardi")
|
||||||
|
self.assertEqual(num2words(2000001000, lang="it"), "due miliardi e mille")
|
||||||
|
self.assertEqual(num2words(1234567890, lang="it"), "un miliardo, duecentotrentaquattro milioni e cinquecentosessantasettemilaottocentonovanta")
|
||||||
|
self.assertEqual(num2words(1000000000000, lang="it"), "un bilione")
|
||||||
|
self.assertEqual(num2words(123456789012345678901234567890, lang="it"), "centoventitré quadriliardi, quattrocentocinquantasei quadrilioni, settecentottantanove triliardi, dodici trilioni, trecentoquarantacinque biliardi, seicentosettantotto bilioni, novecentouno miliardi, duecentotrentaquattro milioni e cinquecentosessantasettemilaottocentonovanta")
|
||||||
|
|
||||||
|
def test_nth_1_to_99(self):
|
||||||
|
self.assertEqual(num2words(1, lang="it", ordinal=True), "primo")
|
||||||
|
self.assertEqual(num2words(8, lang="it", ordinal=True), "ottavo")
|
||||||
|
self.assertEqual(num2words(23, lang="it", ordinal=True), "ventitreesimo")
|
||||||
|
self.assertEqual(num2words(47, lang="it", ordinal=True), "quarantasettesimo")
|
||||||
|
self.assertEqual(num2words(99, lang="it", ordinal=True), "novantanovesimo")
|
||||||
|
|
||||||
|
def test_nth_100_to_999(self):
|
||||||
|
self.assertEqual(num2words(100, lang="it", ordinal=True), "centesimo")
|
||||||
|
self.assertEqual(num2words(112, lang="it", ordinal=True), "centododicesimo")
|
||||||
|
self.assertEqual(num2words(120, lang="it", ordinal=True), "centoventesimo")
|
||||||
|
self.assertEqual(num2words(316, lang="it", ordinal=True), "trecentosedicesimo")
|
||||||
|
self.assertEqual(num2words(700, lang="it", ordinal=True), "settecentesimo")
|
||||||
|
self.assertEqual(num2words(803, lang="it", ordinal=True), "ottocentotreesimo")
|
||||||
|
self.assertEqual(num2words(923, lang="it", ordinal=True), "novecentoventitreesimo")
|
||||||
|
|
||||||
|
def test_nth_1000_to_999999(self):
|
||||||
|
self.assertEqual(num2words(1000, lang="it", ordinal=True), "millesimo")
|
||||||
|
self.assertEqual(num2words(1001, lang="it", ordinal=True), "milleunesimo")
|
||||||
|
self.assertEqual(num2words(1003, lang="it", ordinal=True), "milletreesimo")
|
||||||
|
self.assertEqual(num2words(1200, lang="it", ordinal=True), "milleduecentesimo")
|
||||||
|
self.assertEqual(num2words(8640, lang="it", ordinal=True), "ottomilaseicentoquarantesimo")
|
||||||
|
self.assertEqual(num2words(14000, lang="it", ordinal=True), "quattordicimillesimo")
|
||||||
|
self.assertEqual(num2words(123456, lang="it", ordinal=True), "centoventitremilaquattrocentocinquantaseiesimo")
|
||||||
|
self.assertEqual(num2words(987654, lang="it", ordinal=True), "novecentottantasettemilaseicentocinquantaquattresimo")
|
||||||
|
|
||||||
|
def test_nth_big(self):
|
||||||
|
self.assertEqual(num2words(1000000001, lang="it", ordinal=True), "un miliardo e unesimo")
|
||||||
|
self.assertEqual(num2words(123456789012345678901234567890, lang="it", ordinal=True), "centoventitré quadriliardi, quattrocentocinquantasei quadrilioni, settecentottantanove triliardi, dodici trilioni, trecentoquarantacinque biliardi, seicentosettantotto bilioni, novecentouno miliardi, duecentotrentaquattro milioni e cinquecentosessantasettemilaottocentonovantesimo")
|
||||||
53
tests/test_nl.py
Normal file
53
tests/test_nl.py
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
# -*- encoding: utf-8 -*-
|
||||||
|
# Copyright (c) 2015, 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 Num2WordsNLTest(TestCase):
|
||||||
|
def test_ordinal_less_than_twenty(self):
|
||||||
|
self.assertEqual(num2words(7, ordinal=True, lang='nl'), "zevende")
|
||||||
|
self.assertEqual(num2words(8, ordinal=True, lang='nl'), "achtste")
|
||||||
|
self.assertEqual(num2words(12, ordinal=True, lang='nl'), "twaalfde")
|
||||||
|
self.assertEqual(num2words(17, ordinal=True, lang='nl'), "zeventiende")
|
||||||
|
|
||||||
|
def test_ordinal_more_than_twenty(self):
|
||||||
|
self.assertEqual(num2words(81, ordinal=True, lang='nl'), "eenentachtigste")
|
||||||
|
|
||||||
|
def test_ordinal_at_crucial_number(self):
|
||||||
|
self.assertEqual(num2words(100, ordinal=True, lang='nl'), "honderdste")
|
||||||
|
self.assertEqual(num2words(1000, ordinal=True, lang='nl'), "duizendste")
|
||||||
|
self.assertEqual(num2words(4000, ordinal=True, lang='nl'), "vierduizendste")
|
||||||
|
self.assertEqual(num2words(2000000, ordinal=True, lang='nl'), "twee miljoenste")
|
||||||
|
self.assertEqual(num2words(5000000000, ordinal=True, lang='nl'), "vijf miljardste")
|
||||||
|
|
||||||
|
def test_cardinal_at_some_numbers(self):
|
||||||
|
self.assertEqual(num2words(82, lang='nl'), u'twee\xebntachtig')
|
||||||
|
self.assertEqual(num2words(1013, lang='nl'), "duizenddertien")
|
||||||
|
self.assertEqual(num2words(2000000, lang='nl'), "twee miljoen")
|
||||||
|
self.assertEqual(num2words(4000000000, lang='nl'), "vier miljard")
|
||||||
|
|
||||||
|
def test_cardinal_for_decimal_number(self):
|
||||||
|
self.assertEqual(num2words(3.486, lang='nl'), "drie komma vier acht")
|
||||||
|
|
||||||
|
def test_ordinal_for_negative_numbers(self):
|
||||||
|
self.assertRaises(TypeError, num2words, -12, ordinal=True, lang='nl')
|
||||||
|
|
||||||
|
def test_ordinal_for_floating_numbers(self):
|
||||||
|
self.assertRaises(TypeError, num2words, 2.453, ordinal=True, lang='nl')
|
||||||
49
tests/test_pl.py
Normal file
49
tests/test_pl.py
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
# -*- encoding: utf-8 -*-
|
||||||
|
# Copyright (c) 2015, 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
|
||||||
|
from num2words.lang_PL import to_currency
|
||||||
|
|
||||||
|
class Num2WordsPLTest(TestCase):
|
||||||
|
def test_cardinal(self):
|
||||||
|
self.assertEqual(num2words(100, lang='pl'), "sto")
|
||||||
|
self.assertEqual(num2words(101, lang='pl'), "sto jeden")
|
||||||
|
self.assertEqual(num2words(110, lang='pl'), "sto dziesięć")
|
||||||
|
self.assertEqual(num2words(115, lang='pl'), "sto piętnaście")
|
||||||
|
self.assertEqual(num2words(123, lang='pl'), "sto dwadzieścia trzy")
|
||||||
|
self.assertEqual(num2words(1000, lang='pl'), "tysiąc")
|
||||||
|
self.assertEqual(num2words(1001, lang='pl'), "tysiąc jeden")
|
||||||
|
self.assertEqual(num2words(2012, lang='pl'), "dwa tysiące dwanaście")
|
||||||
|
self.assertEqual(num2words(12519.85, lang='pl'), "dwanaście tysięcy pięćset dziewiętnaście przecinek osiemdziesiąt pięć")
|
||||||
|
self.assertEqual(num2words(123.50, lang='pl'), "sto dwadzieścia trzy przecinek pięć")
|
||||||
|
self.assertEqual(num2words(1234567890, lang='pl'), "miliard dwieście trzydzieści cztery miliony pięćset sześćdziesiąt siedem tysięcy osiemset dziewięćdzisiąt")
|
||||||
|
self.assertEqual(num2words(215461407892039002157189883901676, lang='pl'), "dwieście piętnaście kwintylionów czterysta sześćdziesiąt jeden kwadryliardów czterysta siedem kwadrylionów osiemset dziewięćdzisiąt dwa tryliardy trzydzieści dziewięć trylionów dwa biliardy sto pięćdziesiąt siedem bilionów sto osiemdziesiąt dziewięć miliardów osiemset osiemdziesiąt trzy miliony dziewęćset jeden tysięcy sześćset siedemdziesiąt sześć")
|
||||||
|
self.assertEqual(num2words(719094234693663034822824384220291, lang='pl'), "siedemset dziewiętnaście kwintylionów dziewięćdzisiąt cztery kwadryliardy dwieście trzydzieści cztery kwadryliony sześćset dziewięćdzisiąt trzy tryliardy sześćset sześćdziesiąt trzy tryliony trzydzieści cztery biliardy osiemset dwadzieścia dwa biliony osiemset dwadzieścia cztery miliardy trzysta osiemdziesiąt cztery miliony dwieście dwadzieścia tysięcy dwieście dziewięćdzisiąt jeden")
|
||||||
|
|
||||||
|
def test_currency(self):
|
||||||
|
self.assertEqual(to_currency(1.0, 'EUR'), "jeden euro, zero centów")
|
||||||
|
self.assertEqual(to_currency(1.0, 'PLN'), "jeden złoty, zero groszy")
|
||||||
|
self.assertEqual(to_currency(1234.56, 'EUR'), "tysiąc dwieście trzydzieści cztery euro, pięćdziesiąt sześć centów")
|
||||||
|
self.assertEqual(to_currency(1234.56, 'PLN'), "tysiąc dwieście trzydzieści cztery złote, pięćdziesiąt sześć groszy")
|
||||||
|
self.assertEqual(to_currency(10111, 'EUR', seperator=' i'), "sto jeden euro i jedenaście centów")
|
||||||
|
self.assertEqual(to_currency(10121, 'PLN', seperator=' i'), "sto jeden złotych i dwadzieścia jeden groszy")
|
||||||
|
self.assertEqual(to_currency(-1251985, cents = False), "minus dwanaście tysięcy pięćset dziewiętnaście euro, 85 centów")
|
||||||
|
self.assertEqual(to_currency(123.50, 'PLN', seperator=' i'), "sto dwadzieścia trzy złote i pięćdziesiąt groszy")
|
||||||
|
self.assertEqual(to_currency(1950, cents = False), "dziewiętnaście euro, 50 centów")
|
||||||
219
tests/test_pt_BR.py
Normal file
219
tests/test_pt_BR.py
Normal file
@@ -0,0 +1,219 @@
|
|||||||
|
# -*- encoding: utf-8 -*-
|
||||||
|
# Copyright (c) 2015, 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 decimal import Decimal
|
||||||
|
from unittest import TestCase
|
||||||
|
|
||||||
|
from num2words import num2words
|
||||||
|
from num2words.lang_PT_BR import Num2Word_PT_BR
|
||||||
|
|
||||||
|
|
||||||
|
class Num2WordsPTBRTest(TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
super(Num2WordsPTBRTest, self).setUp()
|
||||||
|
self.n2w = Num2Word_PT_BR()
|
||||||
|
|
||||||
|
def test_cardinal_integer(self):
|
||||||
|
self.assertEqual(num2words(1, lang='pt_BR'), 'um')
|
||||||
|
self.assertEqual(num2words(2, lang='pt_BR'), 'dois')
|
||||||
|
self.assertEqual(num2words(3, lang='pt_BR'), 'três')
|
||||||
|
self.assertEqual(num2words(4, lang='pt_BR'), 'quatro')
|
||||||
|
self.assertEqual(num2words(5, lang='pt_BR'), 'cinco')
|
||||||
|
self.assertEqual(num2words(6, lang='pt_BR'), 'seis')
|
||||||
|
self.assertEqual(num2words(7, lang='pt_BR'), 'sete')
|
||||||
|
self.assertEqual(num2words(8, lang='pt_BR'), 'oito')
|
||||||
|
self.assertEqual(num2words(9, lang='pt_BR'), 'nove')
|
||||||
|
self.assertEqual(num2words(10, lang='pt_BR'), 'dez')
|
||||||
|
self.assertEqual(num2words(11, lang='pt_BR'), 'onze')
|
||||||
|
self.assertEqual(num2words(12, lang='pt_BR'), 'doze')
|
||||||
|
self.assertEqual(num2words(13, lang='pt_BR'), 'treze')
|
||||||
|
self.assertEqual(num2words(14, lang='pt_BR'), 'catorze')
|
||||||
|
self.assertEqual(num2words(15, lang='pt_BR'), 'quinze')
|
||||||
|
self.assertEqual(num2words(16, lang='pt_BR'), 'dezesseis')
|
||||||
|
self.assertEqual(num2words(17, lang='pt_BR'), 'dezessete')
|
||||||
|
self.assertEqual(num2words(18, lang='pt_BR'), 'dezoito')
|
||||||
|
self.assertEqual(num2words(19, lang='pt_BR'), 'dezenove')
|
||||||
|
self.assertEqual(num2words(20, lang='pt_BR'), 'vinte')
|
||||||
|
|
||||||
|
self.assertEqual(num2words(21, lang='pt_BR'), 'vinte e um')
|
||||||
|
self.assertEqual(num2words(22, lang='pt_BR'), 'vinte e dois')
|
||||||
|
self.assertEqual(num2words(35, lang='pt_BR'), 'trinta e cinco')
|
||||||
|
self.assertEqual(num2words(99, lang='pt_BR'), 'noventa e nove')
|
||||||
|
|
||||||
|
self.assertEqual(num2words(100, lang='pt_BR'), 'cem')
|
||||||
|
self.assertEqual(num2words(101, lang='pt_BR'), 'cento e um')
|
||||||
|
self.assertEqual(num2words(128, lang='pt_BR'), 'cento e vinte e oito')
|
||||||
|
self.assertEqual(num2words(713, lang='pt_BR'), 'setecentos e treze')
|
||||||
|
|
||||||
|
self.assertEqual(num2words(1000, lang='pt_BR'), 'mil')
|
||||||
|
self.assertEqual(num2words(1001, lang='pt_BR'), 'mil e um')
|
||||||
|
self.assertEqual(num2words(1111, lang='pt_BR'), 'mil, cento e onze')
|
||||||
|
self.assertEqual(num2words(2114, lang='pt_BR'), 'dois mil, cento e catorze')
|
||||||
|
self.assertEqual(num2words(73421, lang='pt_BR'), 'setenta e três mil, quatrocentos e vinte e um')
|
||||||
|
|
||||||
|
self.assertEqual(num2words(100000, lang='pt_BR'), 'cem mil')
|
||||||
|
self.assertEqual(num2words(250050, lang='pt_BR'), 'duzentos e cinquenta mil e cinquenta')
|
||||||
|
self.assertEqual(num2words(6000000, lang='pt_BR'), 'seis milhões')
|
||||||
|
self.assertEqual(num2words(19000000000, lang='pt_BR'), 'dezenove bilhões')
|
||||||
|
self.assertEqual(num2words(145000000002, lang='pt_BR'), 'cento e quarenta e cinco bilhões e dois')
|
||||||
|
|
||||||
|
def test_cardinal_integer_negative(self):
|
||||||
|
self.assertEqual(num2words(-1, lang='pt_BR'), 'menos um')
|
||||||
|
self.assertEqual(num2words(-256, lang='pt_BR'), 'menos duzentos e cinquenta e seis')
|
||||||
|
self.assertEqual(num2words(-1000, lang='pt_BR'), 'menos mil')
|
||||||
|
self.assertEqual(num2words(-1000000, lang='pt_BR'), 'menos um milhão')
|
||||||
|
self.assertEqual(num2words(-1234567, lang='pt_BR'), 'menos um milhão, duzentos e trinta e quatro mil, quinhentos e sessenta e sete')
|
||||||
|
|
||||||
|
def test_cardinal_float(self):
|
||||||
|
self.assertEqual(num2words(Decimal('1.00'), lang='pt_BR'), 'um')
|
||||||
|
self.assertEqual(num2words(Decimal('1.01'), lang='pt_BR'), 'um vírgula zero um')
|
||||||
|
self.assertEqual(num2words(Decimal('1.035'), lang='pt_BR'), 'um vírgula zero três')
|
||||||
|
self.assertEqual(num2words(Decimal('1.35'), lang='pt_BR'), 'um vírgula três cinco')
|
||||||
|
self.assertEqual(num2words(Decimal('3.14159'), lang='pt_BR'), 'três vírgula um quatro')
|
||||||
|
self.assertEqual(num2words(Decimal('101.22'), lang='pt_BR'), 'cento e um vírgula dois dois')
|
||||||
|
self.assertEqual(num2words(Decimal('2345.75'), lang='pt_BR'), 'dois mil, trezentos e quarenta e cinco vírgula sete cinco')
|
||||||
|
|
||||||
|
def test_cardinal_float_negative(self):
|
||||||
|
self.assertEqual(num2words(Decimal('-2.34'), lang='pt_BR'), 'menos dois vírgula três quatro')
|
||||||
|
self.assertEqual(num2words(Decimal('-9.99'), lang='pt_BR'), 'menos nove vírgula nove nove')
|
||||||
|
self.assertEqual(num2words(Decimal('-7.01'), lang='pt_BR'), 'menos sete vírgula zero um')
|
||||||
|
self.assertEqual(num2words(Decimal('-222.22'), lang='pt_BR'), 'menos duzentos e vinte e dois vírgula dois dois')
|
||||||
|
|
||||||
|
def test_ordinal(self):
|
||||||
|
self.assertEqual(num2words(1, lang='pt_BR', ordinal=True), 'primeiro')
|
||||||
|
self.assertEqual(num2words(2, lang='pt_BR', ordinal=True), 'segundo')
|
||||||
|
self.assertEqual(num2words(3, lang='pt_BR', ordinal=True), 'terceiro')
|
||||||
|
self.assertEqual(num2words(4, lang='pt_BR', ordinal=True), 'quarto')
|
||||||
|
self.assertEqual(num2words(5, lang='pt_BR', ordinal=True), 'quinto')
|
||||||
|
self.assertEqual(num2words(6, lang='pt_BR', ordinal=True), 'sexto')
|
||||||
|
self.assertEqual(num2words(7, lang='pt_BR', ordinal=True), 'sétimo')
|
||||||
|
self.assertEqual(num2words(8, lang='pt_BR', ordinal=True), 'oitavo')
|
||||||
|
self.assertEqual(num2words(9, lang='pt_BR', ordinal=True), 'nono')
|
||||||
|
self.assertEqual(num2words(10, lang='pt_BR', ordinal=True), 'décimo')
|
||||||
|
self.assertEqual(num2words(11, lang='pt_BR', ordinal=True), 'décimo primeiro')
|
||||||
|
self.assertEqual(num2words(12, lang='pt_BR', ordinal=True), 'décimo segundo')
|
||||||
|
self.assertEqual(num2words(13, lang='pt_BR', ordinal=True), 'décimo terceiro')
|
||||||
|
self.assertEqual(num2words(14, lang='pt_BR', ordinal=True), 'décimo quarto')
|
||||||
|
self.assertEqual(num2words(15, lang='pt_BR', ordinal=True), 'décimo quinto')
|
||||||
|
self.assertEqual(num2words(16, lang='pt_BR', ordinal=True), 'décimo sexto')
|
||||||
|
self.assertEqual(num2words(17, lang='pt_BR', ordinal=True), 'décimo sétimo')
|
||||||
|
self.assertEqual(num2words(18, lang='pt_BR', ordinal=True), 'décimo oitavo')
|
||||||
|
self.assertEqual(num2words(19, lang='pt_BR', ordinal=True), 'décimo nono')
|
||||||
|
self.assertEqual(num2words(20, lang='pt_BR', ordinal=True), 'vigésimo')
|
||||||
|
|
||||||
|
self.assertEqual(num2words(21, lang='pt_BR', ordinal=True), 'vigésimo primeiro')
|
||||||
|
self.assertEqual(num2words(22, lang='pt_BR', ordinal=True), 'vigésimo segundo')
|
||||||
|
self.assertEqual(num2words(35, lang='pt_BR', ordinal=True), 'trigésimo quinto')
|
||||||
|
self.assertEqual(num2words(99, lang='pt_BR', ordinal=True), 'nonagésimo nono')
|
||||||
|
|
||||||
|
self.assertEqual(num2words(100, lang='pt_BR', ordinal=True), 'centésimo')
|
||||||
|
self.assertEqual(num2words(101, lang='pt_BR', ordinal=True), 'centésimo primeiro')
|
||||||
|
self.assertEqual(num2words(128, lang='pt_BR', ordinal=True), 'centésimo vigésimo oitavo')
|
||||||
|
self.assertEqual(num2words(713, lang='pt_BR', ordinal=True), 'septigentésimo décimo terceiro')
|
||||||
|
|
||||||
|
self.assertEqual(num2words(1000, lang='pt_BR', ordinal=True), 'milésimo')
|
||||||
|
self.assertEqual(num2words(1001, lang='pt_BR', ordinal=True), 'milésimo primeiro')
|
||||||
|
self.assertEqual(num2words(1111, lang='pt_BR', ordinal=True), 'milésimo centésimo décimo primeiro')
|
||||||
|
self.assertEqual(num2words(2114, lang='pt_BR', ordinal=True), 'segundo milésimo centésimo décimo quarto')
|
||||||
|
self.assertEqual(num2words(73421, lang='pt_BR', ordinal=True), 'septuagésimo terceiro milésimo quadrigentésimo vigésimo primeiro')
|
||||||
|
|
||||||
|
self.assertEqual(num2words(100000, lang='pt_BR', ordinal=True), 'centésimo milésimo')
|
||||||
|
self.assertEqual(num2words(250050, lang='pt_BR', ordinal=True), 'ducentésimo quinquagésimo milésimo quinquagésimo')
|
||||||
|
self.assertEqual(num2words(6000000, lang='pt_BR', ordinal=True), 'sexto milionésimo')
|
||||||
|
self.assertEqual(num2words(19000000000, lang='pt_BR', ordinal=True), 'décimo nono bilionésimo')
|
||||||
|
self.assertEqual(num2words(145000000002, lang='pt_BR', ordinal=True), 'centésimo quadragésimo quinto bilionésimo segundo')
|
||||||
|
|
||||||
|
def test_currency_integer(self):
|
||||||
|
self.assertEqual(self.n2w.to_currency(1), 'um real')
|
||||||
|
self.assertEqual(self.n2w.to_currency(2), 'dois reais')
|
||||||
|
self.assertEqual(self.n2w.to_currency(3), 'três reais')
|
||||||
|
self.assertEqual(self.n2w.to_currency(4), 'quatro reais')
|
||||||
|
self.assertEqual(self.n2w.to_currency(5), 'cinco reais')
|
||||||
|
self.assertEqual(self.n2w.to_currency(6), 'seis reais')
|
||||||
|
self.assertEqual(self.n2w.to_currency(7), 'sete reais')
|
||||||
|
self.assertEqual(self.n2w.to_currency(8), 'oito reais')
|
||||||
|
self.assertEqual(self.n2w.to_currency(9), 'nove reais')
|
||||||
|
self.assertEqual(self.n2w.to_currency(10), 'dez reais')
|
||||||
|
self.assertEqual(self.n2w.to_currency(11), 'onze reais')
|
||||||
|
self.assertEqual(self.n2w.to_currency(12), 'doze reais')
|
||||||
|
self.assertEqual(self.n2w.to_currency(13), 'treze reais')
|
||||||
|
self.assertEqual(self.n2w.to_currency(14), 'catorze reais')
|
||||||
|
self.assertEqual(self.n2w.to_currency(15), 'quinze reais')
|
||||||
|
self.assertEqual(self.n2w.to_currency(16), 'dezesseis reais')
|
||||||
|
self.assertEqual(self.n2w.to_currency(17), 'dezessete reais')
|
||||||
|
self.assertEqual(self.n2w.to_currency(18), 'dezoito reais')
|
||||||
|
self.assertEqual(self.n2w.to_currency(19), 'dezenove reais')
|
||||||
|
self.assertEqual(self.n2w.to_currency(20), 'vinte reais')
|
||||||
|
|
||||||
|
self.assertEqual(self.n2w.to_currency(21), 'vinte e um reais')
|
||||||
|
self.assertEqual(self.n2w.to_currency(22), 'vinte e dois reais')
|
||||||
|
self.assertEqual(self.n2w.to_currency(35), 'trinta e cinco reais')
|
||||||
|
self.assertEqual(self.n2w.to_currency(99), 'noventa e nove reais')
|
||||||
|
|
||||||
|
self.assertEqual(self.n2w.to_currency(100), 'cem reais')
|
||||||
|
self.assertEqual(self.n2w.to_currency(101), 'cento e um reais')
|
||||||
|
self.assertEqual(self.n2w.to_currency(128), 'cento e vinte e oito reais')
|
||||||
|
self.assertEqual(self.n2w.to_currency(713), 'setecentos e treze reais')
|
||||||
|
|
||||||
|
self.assertEqual(self.n2w.to_currency(1000), 'mil reais')
|
||||||
|
self.assertEqual(self.n2w.to_currency(1001), 'mil e um reais')
|
||||||
|
self.assertEqual(self.n2w.to_currency(1111), 'mil, cento e onze reais')
|
||||||
|
self.assertEqual(self.n2w.to_currency(2114), 'dois mil, cento e catorze reais')
|
||||||
|
self.assertEqual(self.n2w.to_currency(73421), 'setenta e três mil, quatrocentos e vinte e um reais')
|
||||||
|
|
||||||
|
self.assertEqual(self.n2w.to_currency(100000), 'cem mil reais')
|
||||||
|
self.assertEqual(self.n2w.to_currency(250050), 'duzentos e cinquenta mil e cinquenta reais')
|
||||||
|
self.assertEqual(self.n2w.to_currency(6000000), 'seis milhões de reais')
|
||||||
|
self.assertEqual(self.n2w.to_currency(19000000000), 'dezenove bilhões de reais')
|
||||||
|
self.assertEqual(self.n2w.to_currency(145000000002), 'cento e quarenta e cinco bilhões e dois reais')
|
||||||
|
|
||||||
|
def test_currency_integer_negative(self):
|
||||||
|
self.assertEqual(self.n2w.to_currency(-1), 'menos um real')
|
||||||
|
self.assertEqual(self.n2w.to_currency(-256), 'menos duzentos e cinquenta e seis reais')
|
||||||
|
self.assertEqual(self.n2w.to_currency(-1000), 'menos mil reais')
|
||||||
|
self.assertEqual(self.n2w.to_currency(-1000000), 'menos um milhão de reais')
|
||||||
|
self.assertEqual(self.n2w.to_currency(-1234567), 'menos um milhão, duzentos e trinta e quatro mil, quinhentos e sessenta e sete reais')
|
||||||
|
|
||||||
|
def test_currency_float(self):
|
||||||
|
self.assertEqual(self.n2w.to_currency(Decimal('1.00')), 'um real')
|
||||||
|
self.assertEqual(self.n2w.to_currency(Decimal('1.01')), 'um real e um centavo')
|
||||||
|
self.assertEqual(self.n2w.to_currency(Decimal('1.035')), 'um real e três centavos')
|
||||||
|
self.assertEqual(self.n2w.to_currency(Decimal('1.35')), 'um real e trinta e cinco centavos')
|
||||||
|
self.assertEqual(self.n2w.to_currency(Decimal('3.14159')), 'três reais e catorze centavos')
|
||||||
|
self.assertEqual(self.n2w.to_currency(Decimal('101.22')), 'cento e um reais e vinte e dois centavos')
|
||||||
|
self.assertEqual(self.n2w.to_currency(Decimal('2345.75')), 'dois mil, trezentos e quarenta e cinco reais e setenta e cinco centavos')
|
||||||
|
|
||||||
|
def test_currency_float_negative(self):
|
||||||
|
self.assertEqual(self.n2w.to_currency(Decimal('-2.34')), 'menos dois reais e trinta e quatro centavos')
|
||||||
|
self.assertEqual(self.n2w.to_currency(Decimal('-9.99')), 'menos nove reais e noventa e nove centavos')
|
||||||
|
self.assertEqual(self.n2w.to_currency(Decimal('-7.01')), 'menos sete reais e um centavo')
|
||||||
|
self.assertEqual(self.n2w.to_currency(Decimal('-222.22')), 'menos duzentos e vinte e dois reais e vinte e dois centavos')
|
||||||
|
|
||||||
|
def test_year(self):
|
||||||
|
self.assertEqual(self.n2w.to_year(1001), 'mil e um')
|
||||||
|
self.assertEqual(self.n2w.to_year(1789), 'mil, setecentos e oitenta e nove')
|
||||||
|
self.assertEqual(self.n2w.to_year(1942), 'mil, novecentos e quarenta e dois')
|
||||||
|
self.assertEqual(self.n2w.to_year(1984), 'mil, novecentos e oitenta e quatro')
|
||||||
|
self.assertEqual(self.n2w.to_year(2000), 'dois mil')
|
||||||
|
self.assertEqual(self.n2w.to_year(2001), 'dois mil e um')
|
||||||
|
self.assertEqual(self.n2w.to_year(2016), 'dois mil e dezesseis')
|
||||||
|
|
||||||
|
def test_year_negative(self):
|
||||||
|
self.assertEqual(self.n2w.to_year(-30), 'trinta antes de Cristo')
|
||||||
|
self.assertEqual(self.n2w.to_year(-744), 'setecentos e quarenta e quatro antes de Cristo')
|
||||||
|
self.assertEqual(self.n2w.to_year(-10000), 'dez mil antes de Cristo')
|
||||||
33
tests/test_ru.py
Normal file
33
tests/test_ru.py
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
# -*- encoding: utf-8 -*-
|
||||||
|
# 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 unittest import TestCase
|
||||||
|
|
||||||
|
from num2words import num2words
|
||||||
|
|
||||||
|
class Num2WordsRUTest(TestCase):
|
||||||
|
|
||||||
|
def test_cardinal(self):
|
||||||
|
self.assertEqual(num2words(5, lang='ru'), u"пять")
|
||||||
|
self.assertEqual(num2words(15, lang='ru'), u"пятнадцать")
|
||||||
|
self.assertEqual(num2words(154, lang='ru'), u"сто пятьдесят четыре")
|
||||||
|
self.assertEqual(num2words(1135, lang='ru'), u"одна тысяча сто тридцать пять")
|
||||||
|
self.assertEqual(num2words(418531, lang='ru'), u"четыреста восемнадцать тысяч пятьсот тридцать один")
|
||||||
|
self.assertEqual(num2words(1000139, lang='ru'), u"один миллион сто тридцать девять")
|
||||||
|
|
||||||
|
def test_floating_point(self):
|
||||||
|
self.assertEqual(num2words(5.2, lang='ru'), u"пять запятая два")
|
||||||
|
self.assertEqual(num2words(561.42, lang='ru'), u"пятьсот шестьдесят один запятая сорок два")
|
||||||
121
tests/test_tr.py
Normal file
121
tests/test_tr.py
Normal file
@@ -0,0 +1,121 @@
|
|||||||
|
# -*- encoding: utf-8 -*-
|
||||||
|
# Copyright (c) 2013, Savoir-faire Linux inc. All Rights Reserved.
|
||||||
|
# Copyright (c) 2017, Tufan Kaynak, Framras. 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 unittest import TestCase
|
||||||
|
|
||||||
|
from num2words import num2words
|
||||||
|
|
||||||
|
|
||||||
|
class Num2WordsTRTest(TestCase):
|
||||||
|
def test_tr(self):
|
||||||
|
# ref https://github.com/savoirfairelinux/num2words/issues/8
|
||||||
|
|
||||||
|
self.assertEqual(num2words(1, True, "tr"), u"birinci")
|
||||||
|
self.assertEqual(num2words(2, True, "tr"), u"ikinci")
|
||||||
|
self.assertEqual(num2words(9, True, "tr"), u"dokuzuncu")
|
||||||
|
self.assertEqual(num2words(10, True, "tr"), u"onuncu")
|
||||||
|
self.assertEqual(num2words(11, True, "tr"), u"onbirinci")
|
||||||
|
self.assertEqual(num2words(44, True, "tr"), u"kırkdördüncü")
|
||||||
|
self.assertEqual(num2words(100, True, "tr"), u"yüzüncü")
|
||||||
|
self.assertEqual(num2words(101, True, "tr"), u"yüzbirinci")
|
||||||
|
self.assertEqual(num2words(103, True, "tr"), u"yüzüçüncü")
|
||||||
|
self.assertEqual(num2words(110, True, "tr"), u"yüzonuncu")
|
||||||
|
self.assertEqual(num2words(111, True, "tr"), u"yüzonbirinci")
|
||||||
|
self.assertEqual(num2words(1000, True, "tr"), u"bininci")
|
||||||
|
self.assertEqual(num2words(1001, True, "tr"), u"binbirinci")
|
||||||
|
self.assertEqual(num2words(1010, True, "tr"), u"binonuncu")
|
||||||
|
self.assertEqual(num2words(1011, True, "tr"), u"binonbirinci")
|
||||||
|
self.assertEqual(num2words(1100, True, "tr"), u"binyüzüncü")
|
||||||
|
self.assertEqual(num2words(1110, True, "tr"), u"binyüzonuncu")
|
||||||
|
self.assertEqual(num2words(2341, True, "tr"), u"ikibinüçyüzkırkbirinci")
|
||||||
|
self.assertEqual(num2words(10000, True, "tr"), u"onbininci")
|
||||||
|
self.assertEqual(num2words(10010, True, "tr"), u"onbinonuncu")
|
||||||
|
self.assertEqual(num2words(10100, True, "tr"), u"onbinyüzüncü")
|
||||||
|
self.assertEqual(num2words(10110, True, "tr"), u"onbinyüzonuncu")
|
||||||
|
self.assertEqual(num2words(11000, True, "tr"), u"onbirbininci")
|
||||||
|
self.assertEqual(num2words(35000, True, "tr"), u"otuzbeşbininci")
|
||||||
|
self.assertEqual(num2words(116331, True, "tr"), u"yüzonaltıbinüçyüzotuzbirinci")
|
||||||
|
self.assertEqual(num2words(116330, True, "tr"), u"yüzonaltıbinüçyüzotuzuncu")
|
||||||
|
self.assertEqual(num2words(100000, True, "tr"), u"yüzbininci")
|
||||||
|
self.assertEqual(num2words(501000, True, "tr"), u"beşyüzbirbininci")
|
||||||
|
self.assertEqual(num2words(1000111, True, "tr"), u"birmilyonyüzonbirinci")
|
||||||
|
self.assertEqual(num2words(111000111, True, "tr"), u"yüzonbirmilyonyüzonbirinci")
|
||||||
|
self.assertEqual(num2words(111001111, True, "tr"), u"yüzonbirmilyonbinyüzonbirinci")
|
||||||
|
self.assertEqual(num2words(111111111, True, "tr"), u"yüzonbirmilyonyüzonbirbinyüzonbirinci")
|
||||||
|
self.assertEqual(num2words(100001000, True, "tr"), u"yüzmilyonbininci")
|
||||||
|
self.assertEqual(num2words(100001001, True, "tr"), u"yüzmilyonbinbirinci")
|
||||||
|
self.assertEqual(num2words(100010000, True, "tr"), u"yüzmilyononbininci")
|
||||||
|
self.assertEqual(num2words(100010001, True, "tr"), u"yüzmilyononbinbirinci")
|
||||||
|
self.assertEqual(num2words(100011000, True, "tr"), u"yüzmilyononbirbininci")
|
||||||
|
self.assertEqual(num2words(100011001, True, "tr"), u"yüzmilyononbirbinbirinci")
|
||||||
|
self.assertEqual(num2words(101011001, True, "tr"), u"yüzbirmilyononbirbinbirinci")
|
||||||
|
self.assertEqual(num2words(101011010, True, "tr"), u"yüzbirmilyononbirbinonuncu")
|
||||||
|
self.assertEqual(num2words(1101011010, True, "tr"), u"birmilyaryüzbirmilyononbirbinonuncu")
|
||||||
|
self.assertEqual(num2words(101101011010, True, "tr"), u"yüzbirmilyaryüzbirmilyononbirbinonuncu")
|
||||||
|
self.assertEqual(num2words(1000000000001, True, "tr"), u"birtrilyonbirinci")
|
||||||
|
|
||||||
|
self.assertEqual(num2words(1, False, "tr"), u"bir")
|
||||||
|
self.assertEqual(num2words(2, False, "tr"), u"iki")
|
||||||
|
self.assertEqual(num2words(9, False, "tr"), u"dokuz")
|
||||||
|
self.assertEqual(num2words(10, False, "tr"), u"on")
|
||||||
|
self.assertEqual(num2words(11, False, "tr"), u"onbir")
|
||||||
|
self.assertEqual(num2words(44, False, "tr"), u"kırkdört")
|
||||||
|
self.assertEqual(num2words(100, False, "tr"), u"yüz")
|
||||||
|
self.assertEqual(num2words(101, False, "tr"), u"yüzbir")
|
||||||
|
self.assertEqual(num2words(103, False, "tr"), u"yüzüç")
|
||||||
|
self.assertEqual(num2words(110, False, "tr"), u"yüzon")
|
||||||
|
self.assertEqual(num2words(111, False, "tr"), u"yüzonbir")
|
||||||
|
self.assertEqual(num2words(1000, False, "tr"), u"bin")
|
||||||
|
self.assertEqual(num2words(1001, False, "tr"), u"binbir")
|
||||||
|
self.assertEqual(num2words(1010, False, "tr"), u"binon")
|
||||||
|
self.assertEqual(num2words(1011, False, "tr"), u"binonbir")
|
||||||
|
self.assertEqual(num2words(1100, False, "tr"), u"binyüz")
|
||||||
|
self.assertEqual(num2words(1110, False, "tr"), u"binyüzon")
|
||||||
|
self.assertEqual(num2words(2341, False, "tr"), u"ikibinüçyüzkırkbir")
|
||||||
|
self.assertEqual(num2words(10000, False, "tr"), u"onbin")
|
||||||
|
self.assertEqual(num2words(10010, False, "tr"), u"onbinon")
|
||||||
|
self.assertEqual(num2words(10100, False, "tr"), u"onbinyüz")
|
||||||
|
self.assertEqual(num2words(10110, False, "tr"), u"onbinyüzon")
|
||||||
|
self.assertEqual(num2words(11000, False, "tr"), u"onbirbin")
|
||||||
|
self.assertEqual(num2words(35000, False, "tr"), u"otuzbeşbin")
|
||||||
|
self.assertEqual(num2words(116331, False, "tr"), u"yüzonaltıbinüçyüzotuzbir")
|
||||||
|
self.assertEqual(num2words(116330, False, "tr"), u"yüzonaltıbinüçyüzotuz")
|
||||||
|
self.assertEqual(num2words(500000, False, "tr"), u"beşyüzbin")
|
||||||
|
self.assertEqual(num2words(501000, False, "tr"), u"beşyüzbirbin")
|
||||||
|
self.assertEqual(num2words(1000111, False, "tr"), u"birmilyonyüzonbir")
|
||||||
|
self.assertEqual(num2words(111000111, False, "tr"), u"yüzonbirmilyonyüzonbir")
|
||||||
|
self.assertEqual(num2words(111001111, False, "tr"), u"yüzonbirmilyonbinyüzonbir")
|
||||||
|
self.assertEqual(num2words(111111111, False, "tr"), u"yüzonbirmilyonyüzonbirbinyüzonbir")
|
||||||
|
self.assertEqual(num2words(100001000, False, "tr"), u"yüzmilyonbin")
|
||||||
|
self.assertEqual(num2words(100001001, False, "tr"), u"yüzmilyonbinbir")
|
||||||
|
self.assertEqual(num2words(100010000, False, "tr"), u"yüzmilyononbin")
|
||||||
|
self.assertEqual(num2words(100010001, False, "tr"), u"yüzmilyononbinbir")
|
||||||
|
self.assertEqual(num2words(100011000, False, "tr"), u"yüzmilyononbirbin")
|
||||||
|
self.assertEqual(num2words(100011001, False, "tr"), u"yüzmilyononbirbinbir")
|
||||||
|
self.assertEqual(num2words(101011001, False, "tr"), u"yüzbirmilyononbirbinbir")
|
||||||
|
self.assertEqual(num2words(101011010, False, "tr"), u"yüzbirmilyononbirbinon")
|
||||||
|
self.assertEqual(num2words(1101011010, False, "tr"), u"birmilyaryüzbirmilyononbirbinon")
|
||||||
|
self.assertEqual(num2words(101101011010, False, "tr"), u"yüzbirmilyaryüzbirmilyononbirbinon")
|
||||||
|
self.assertEqual(num2words(1000000000001, False, "tr"), u"birtrilyonbir")
|
||||||
|
self.assertEqual(num2words(0.01, False, "tr"), u"sıfırvirgülbir")
|
||||||
|
self.assertEqual(num2words(0.1, False, "tr"), u"sıfırvirgülon")
|
||||||
|
self.assertEqual(num2words(0.21, False, "tr"), u"sıfırvirgülyirmibir")
|
||||||
|
self.assertEqual(num2words(1.01, False, "tr"), u"birvirgülbir")
|
||||||
|
self.assertEqual(num2words(1.1, False, "tr"), u"birvirgülon")
|
||||||
|
self.assertEqual(num2words(1.21, False, "tr"), u"birvirgülyirmibir")
|
||||||
|
self.assertEqual(num2words(101101011010.02, False, "tr"), u"yüzbirmilyaryüzbirmilyononbirbinonvirgüliki")
|
||||||
|
self.assertEqual(num2words(101101011010.2, False, "tr"), u"yüzbirmilyaryüzbirmilyononbirbinonvirgülyirmi")
|
||||||
29
tests/test_uk.py
Normal file
29
tests/test_uk.py
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
# -*- encoding: utf-8 -*-
|
||||||
|
# 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 unittest import TestCase
|
||||||
|
|
||||||
|
from num2words import num2words
|
||||||
|
|
||||||
|
class Num2WordsUKTest(TestCase):
|
||||||
|
def test_and_join_199(self):
|
||||||
|
self.assertEqual(num2words(187,lang='uk'), u"сто вiсiмдесят сiм")
|
||||||
|
|
||||||
|
def test_cardinal_for_float_number(self):
|
||||||
|
self.assertEqual(num2words(12.40,lang='uk'), u"дванадцять кома чотири")
|
||||||
|
self.assertEqual(num2words(17.31,lang='uk'), u"сiмнадцять кома тридцять одна")
|
||||||
|
self.assertEqual(num2words(14.13,lang='uk'), u"чотирнадцять кома тринадцять")
|
||||||
|
self.assertEqual(num2words(12.31,lang='uk'), u"дванадцять кома тридцять одна")
|
||||||
106
tests/test_vn.py
Normal file
106
tests/test_vn.py
Normal file
@@ -0,0 +1,106 @@
|
|||||||
|
# -*- encoding: utf-8 -*-
|
||||||
|
# Copyright (c) 2015, 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 Num2WordsVNTest(TestCase):
|
||||||
|
|
||||||
|
|
||||||
|
def test_0(self):
|
||||||
|
self.assertEqual(num2words(0, lang="vi_VN"), "không")
|
||||||
|
|
||||||
|
def test_1_to_10(self):
|
||||||
|
self.assertEqual(num2words(1, lang="vi_VN"), "một")
|
||||||
|
self.assertEqual(num2words(2, lang="vi_VN"), "hai")
|
||||||
|
self.assertEqual(num2words(7, lang="vi_VN"), "bảy")
|
||||||
|
self.assertEqual(num2words(10, lang="vi_VN"), "mười")
|
||||||
|
|
||||||
|
def test_11_to_19(self):
|
||||||
|
self.assertEqual(num2words(11, lang="vi_VN"), "mười một")
|
||||||
|
self.assertEqual(num2words(13, lang="vi_VN"), "mười ba")
|
||||||
|
self.assertEqual(num2words(14, lang="vi_VN"), "mười bốn")
|
||||||
|
self.assertEqual(num2words(15, lang="vi_VN"), "mười lăm")
|
||||||
|
self.assertEqual(num2words(16, lang="vi_VN"), "mười sáu")
|
||||||
|
self.assertEqual(num2words(19, lang="vi_VN"), "mười chín")
|
||||||
|
|
||||||
|
def test_20_to_99(self):
|
||||||
|
self.assertEqual(num2words(20, lang="vi_VN"), "hai mươi")
|
||||||
|
self.assertEqual(num2words(23, lang="vi_VN"), "hai mươi ba")
|
||||||
|
self.assertEqual(num2words(28, lang="vi_VN"), "hai mươi tám")
|
||||||
|
self.assertEqual(num2words(31, lang="vi_VN"), "ba mươi mốt")
|
||||||
|
self.assertEqual(num2words(40, lang="vi_VN"), "bốn mươi")
|
||||||
|
self.assertEqual(num2words(66, lang="vi_VN"), "sáu mươi sáu")
|
||||||
|
self.assertEqual(num2words(92, lang="vi_VN"), "chín mươi hai")
|
||||||
|
|
||||||
|
def test_100_to_999(self):
|
||||||
|
self.assertEqual(num2words(100, lang="vi_VN"), "một trăm")
|
||||||
|
self.assertEqual(num2words(150, lang="vi_VN"), "một trăm năm mươi")
|
||||||
|
self.assertEqual(num2words(196, lang="vi_VN"), "một trăm chín mươi sáu")
|
||||||
|
self.assertEqual(num2words(200, lang="vi_VN"), "hai trăm")
|
||||||
|
self.assertEqual(num2words(210, lang="vi_VN"), "hai trăm mười")
|
||||||
|
|
||||||
|
def test_1000_to_9999(self):
|
||||||
|
self.assertEqual(num2words(1000, lang="vi_VN"), "một nghìn")
|
||||||
|
self.assertEqual(num2words(1500, lang="vi_VN"), "một nghìn năm trăm")
|
||||||
|
self.assertEqual(num2words(7378, lang="vi_VN"), "bảy nghìn ba trăm bảy mươi tám")
|
||||||
|
self.assertEqual(num2words(2000, lang="vi_VN"), "hai nghìn")
|
||||||
|
self.assertEqual(num2words(2100, lang="vi_VN"), "hai nghìn một trăm")
|
||||||
|
self.assertEqual(num2words(6870, lang="vi_VN"), "sáu nghìn tám trăm bảy mươi")
|
||||||
|
self.assertEqual(num2words(10000, lang="vi_VN"), "mười nghìn")
|
||||||
|
self.assertEqual(num2words(100000, lang="vi_VN"), "một trăm nghìn")
|
||||||
|
self.assertEqual(num2words(523456, lang="vi_VN"), "năm trăm hai mươi ba nghìn bốn trăm năm mươi sáu")
|
||||||
|
|
||||||
|
def test_big(self):
|
||||||
|
self.assertEqual(num2words(1000000, lang="vi_VN"), "một triệu")
|
||||||
|
self.assertEqual(num2words(1200000, lang="vi_VN"), "một triệu hai trăm nghìn")
|
||||||
|
self.assertEqual(num2words(3000000, lang="vi_VN"), "ba triệu")
|
||||||
|
self.assertEqual(num2words(3800000, lang="vi_VN"), "ba triệu tám trăm nghìn")
|
||||||
|
self.assertEqual(num2words(1000000000, lang="vi_VN"), "một tỷ")
|
||||||
|
self.assertEqual(num2words(2000000000, lang="vi_VN"), "hai tỷ")
|
||||||
|
self.assertEqual(num2words(2000001000, lang="vi_VN"), "hai tỷ một nghìn")
|
||||||
|
self.assertEqual(num2words(1234567890, lang="vi_VN"), "một tỷ hai trăm ba mươi bốn triệu năm trăm sáu mươi bảy nghìn tám trăm chín mươi")
|
||||||
|
|
||||||
|
|
||||||
|
def test_decimal_number(self):
|
||||||
|
self.assertEqual(num2words(1000.11, lang="vi_VN"), "một nghìn phẩy mười một")
|
||||||
|
self.assertEqual(num2words(1000.21, lang="vi_VN"), "một nghìn phẩy hai mươi mốt")
|
||||||
|
|
||||||
|
def test_special_number(self):
|
||||||
|
"""
|
||||||
|
Some number will have some specail rule
|
||||||
|
"""
|
||||||
|
self.assertEqual(num2words(21, lang="vi_VN"), "hai mươi mốt")
|
||||||
|
self.assertEqual(num2words(25, lang="vi_VN"), "hai mươi lăm")
|
||||||
|
# >100
|
||||||
|
self.assertEqual(num2words(101, lang="vi_VN"), "một trăm lẻ một")
|
||||||
|
self.assertEqual(num2words(105, lang="vi_VN"), "một trăm lẻ năm")
|
||||||
|
self.assertEqual(num2words(701, lang="vi_VN"), "bảy trăm lẻ một")
|
||||||
|
self.assertEqual(num2words(705, lang="vi_VN"), "bảy trăm lẻ năm")
|
||||||
|
|
||||||
|
# >1000
|
||||||
|
self.assertEqual(num2words(1001, lang="vi_VN"), "một nghìn lẻ một")
|
||||||
|
self.assertEqual(num2words(1005, lang="vi_VN"), "một nghìn lẻ năm")
|
||||||
|
self.assertEqual(num2words(98765, lang="vi_VN"), "chín mươi tám nghìn bảy trăm sáu mươi lăm")
|
||||||
|
|
||||||
|
# > 1000000
|
||||||
|
self.assertEqual(num2words(3000005, lang="vi_VN"), "ba triệu lẻ năm")
|
||||||
|
self.assertEqual(num2words(1000007, lang="vi_VN"), "một triệu lẻ bảy")
|
||||||
|
|
||||||
|
# > 1000000000
|
||||||
|
self.assertEqual(num2words(1000000017, lang="vi_VN"), "một tỷ lẻ mười bảy")
|
||||||
|
self.assertEqual(num2words(1000101017, lang="vi_VN"), "một tỷ một trăm lẻ một nghìn lẻ mười bảy")
|
||||||
Reference in New Issue
Block a user