mirror of
https://github.com/bblaz/num2words.git
synced 2025-12-06 06:42:25 +00:00
Added README and re-designed the API
This commit is contained in:
48
README.rst
Normal file
48
README.rst
Normal file
@@ -0,0 +1,48 @@
|
||||
num2words - Convert numbers to words in multiple languages
|
||||
==========================================================
|
||||
|
||||
``num2words`` is a library that converts numbers like ``42`` to words like ``forty-two``. It
|
||||
supports multiple languages (English, French, Spanish, German and Lithuanian) and can even generate
|
||||
ordinal numbers like ``forty-second`` (altough this last feature is a bit buggy at the moment).
|
||||
|
||||
The project is hosted on https://github.com/savoirfairelinux/num2words
|
||||
|
||||
Usage
|
||||
-----
|
||||
|
||||
There's only one function to use::
|
||||
|
||||
>>> from num2words import num2words
|
||||
>>> num2words(42)
|
||||
forty-two
|
||||
>>> num2words(42, ordinal=True)
|
||||
forty-second
|
||||
>>> num2words(42, lang='fr')
|
||||
quarante-deux
|
||||
|
||||
Besides the numerical argument, there's two optional arguments.
|
||||
|
||||
**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 languages are ``en``, ``fr``,
|
||||
``de``, ``es``, ``lt`` and ``en_GB``. You can supply values like ``fr_FR``, the code will be
|
||||
correctly interpreted. If you supply an unsupported language, ``NotImplementedError`` is raised.
|
||||
Therefore, if you want to call ``num2words`` with a fallback, you can do::
|
||||
|
||||
try:
|
||||
return num2words(42, lang=mylang)
|
||||
except NotImplementedError:
|
||||
return num2words(42, lang='en')
|
||||
|
||||
History
|
||||
-------
|
||||
|
||||
``num2words`` is based on an old library , ``pynum2word`` created by Taro Ogawa in 2003.
|
||||
Unfortunately, the library stopped being maintained and the author can't be reached. There was
|
||||
another developer, Marius Grigaitis, who in 2011 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
|
||||
``num2words``.
|
||||
|
||||
Virgil Dupras, Savoir-faire Linux
|
||||
@@ -29,34 +29,32 @@ History:
|
||||
0.2: n2w, to_card, to_ord, to_ordnum now imported correctly
|
||||
'''
|
||||
from __future__ import unicode_literals
|
||||
import locale as _locale
|
||||
|
||||
# Correct omissions in locale:
|
||||
# Bugrep these...
|
||||
_locdict = { "English_Australia" : "en_AU", }
|
||||
import num2word_EN
|
||||
import num2word_EN_GB
|
||||
import num2word_FR
|
||||
import num2word_DE
|
||||
import num2word_ES
|
||||
import num2word_LT
|
||||
|
||||
CONVERTER_CLASSES = {
|
||||
'en': num2word_EN.Num2Word_EN(),
|
||||
'en_GB': num2word_EN_GB.Num2Word_EN_GB(),
|
||||
'fr': num2word_FR.Num2Word_FR(),
|
||||
'de': num2word_DE.Num2Word_DE(),
|
||||
'es': num2word_ES.Num2Word_ES(),
|
||||
'lt': num2word_LT.Num2Word_LT(),
|
||||
}
|
||||
|
||||
_modules = []
|
||||
for _loc in [_locale.getlocale(), _locale.getdefaultlocale()]:
|
||||
_lang = _loc[0]
|
||||
if _lang:
|
||||
_lang = _locdict.get(_lang, _lang)
|
||||
_lang = _lang.upper()
|
||||
|
||||
_modules.append("num2word_" + _lang)
|
||||
_modules.append("num2word_" + _lang.split("_")[0])
|
||||
|
||||
for _module in _modules:
|
||||
try:
|
||||
n2wmod = __import__(_module)
|
||||
break
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
try:
|
||||
n2w, to_card, to_ord, to_ordnum, to_year = (n2wmod.n2w, n2wmod.to_card,
|
||||
n2wmod.to_ord, n2wmod.to_ordnum,
|
||||
n2wmod.to_year)
|
||||
except NameError:
|
||||
raise ImportError("Could not import any of these modules: %s"
|
||||
% (", ".join(_modules)))
|
||||
def num2words(number, ordinal=False, lang='en'):
|
||||
# We try the full language first
|
||||
if lang not in CONVERTER_CLASSES:
|
||||
# ... and then try only the first 2 letters
|
||||
lang = lang[:2]
|
||||
if lang not in CONVERTER_CLASSES:
|
||||
raise NotImplementedError()
|
||||
converter = CONVERTER_CLASSES[lang]
|
||||
if ordinal:
|
||||
return converter.to_ordinal(number)
|
||||
else:
|
||||
return converter.to_cardinal(number)
|
||||
|
||||
@@ -227,7 +227,12 @@ def to_currency(n, currency='LTL', cents = True):
|
||||
return u'%s%s %s, %s %s' % (minus_str, int2word(left), pluralize(left, cr1),
|
||||
cents_str, pluralize(right, cr2))
|
||||
|
||||
to_card = n2w
|
||||
class Num2Word_LT(object):
|
||||
def to_cardinal(self, number):
|
||||
return n2w(number)
|
||||
|
||||
def to_ordinal(self, number):
|
||||
raise NotImplementedError()
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
|
||||
Reference in New Issue
Block a user