Giter VIP home page Giter VIP logo

num2words's Introduction

num2words library - Convert numbers to words in multiple languages

image

image

image

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 GitHub. Contributions are welcome.

Installation

The easiest way to install num2words is to use pip:

pip install num2words

Otherwise, you can download the source package and then execute:

python setup.py install

The test suite in this library is new, so it's rather thin, but it can be run with:

python setup.py test

To run the full CI test suite which includes linting and multiple python environments:

pip install tox
tox

Usage

Command line:

$ num2words 10001
ten thousand and one
$ num2words 24,120.10
twenty-four thousand, one hundred and twenty point one
$ num2words 24,120.10 -l es
veinticuatro mil ciento veinte punto uno
$num2words 2.14 -l es --to currency
dos euros con catorce céntimos

In code there's only one function to use:

>>> from num2words import num2words
>>> num2words(42)
forty-two
>>> num2words(42, to='ordinal')
forty-second
>>> num2words(42, lang='fr')
quarante-deux

Besides the numerical argument, there are two main optional arguments, to: and lang:

to: The converter to use. Supported values are:

  • cardinal (default)
  • ordinal
  • ordinal_num
  • year
  • currency

lang: The language in which to convert the number. Supported values are:

  • en (English, default)
  • am (Amharic)
  • ar (Arabic)
  • az (Azerbaijani)
  • by (Belarusian)
  • ce (Chechen)
  • cy (Welsh)
  • cz (Czech)
  • de (German)
  • dk (Danish)
  • en_GB (English - Great Britain)
  • en_IN (English - India)
  • en_NG (English - Nigeria)
  • es (Spanish)
  • es_CO (Spanish - Colombia)
  • es_CR (Spanish - Costa Rica)
  • es_VE (Spanish - Venezuela)
  • es_GT (Spanish - Guatemala)
  • eu (EURO)
  • fa (Farsi)
  • fi (Finnish)
  • fr (French)
  • fr_CH (French - Switzerland)
  • fr_BE (French - Belgium)
  • fr_DZ (French - Algeria)
  • he (Hebrew)
  • hu (Hungarian)
  • id (Indonesian)
  • is (Icelandic)
  • it (Italian)
  • ja (Japanese)
  • kn (Kannada)
  • ko (Korean)
  • kz (Kazakh)
  • lt (Lithuanian)
  • lv (Latvian)
  • no (Norwegian)
  • pl (Polish)
  • pt (Portuguese)
  • pt_BR (Portuguese - Brazilian)
  • sl (Slovene)
  • sr (Serbian)
  • sv (Swedish)
  • ro (Romanian)
  • ru (Russian)
  • te (Telugu)
  • tg (Tajik)
  • tr (Turkish)
  • th (Thai)
  • vi (Vietnamese)
  • nl (Dutch)
  • uk (Ukrainian)

You can supply values like fr_FR; if the country doesn't exist but the language does, the code will fall back to the base language (i.e. fr). 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')

Additionally, some converters and languages support other optional arguments that are needed to make the converter useful in practice.

Wiki

For additional information on some localization please check the Wiki. And feel free to propose wiki enhancement.

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

num2words's People

Contributors

cyriaka90 avatar dalwadani avatar decherd avatar erozqba avatar eyaler avatar gonsalet avatar hedhoud avatar hkalbasi avatar ieski avatar isnani avatar jeronymous avatar jheinecke avatar kkonieczny avatar kszys avatar marias210 avatar monroy95 avatar mrodriguezg1991 avatar mromdhane avatar olix20 avatar peternordstrombookbeat avatar pierreduchemin avatar piranna avatar sergeyzhol avatar shulcsm avatar skiby avatar toofun666 avatar trangoul avatar venu1807 avatar williamjmorenor avatar zenogantner avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

num2words's Issues

issue after converting number to words

Hi,

Thanks for your lib that helps me so much to convert number to words. But, I have some following issues:

num2words(12.50, ordinal=False, lang='en')
u'twelve point five zero'

num2words(12.51, ordinal=False, lang='en')
u'twelve point five zero'
num2words(12.52, ordinal=False, lang='en')
u'twelve point five one'
num2words(12.53, ordinal=False, lang='en')
u'twelve point five two'
num2words(12.54, ordinal=False, lang='en')
u'twelve point five three'

num2words(12.55, ordinal=False, lang='en')
u'twelve point five five'
num2words(12.56, ordinal=False, lang='en')
u'twelve point five six'
num2words(12.57, ordinal=False, lang='en')
u'twelve point five seven'
num2words(12.58, ordinal=False, lang='en')
u'twelve point five eight'

num2words(12.59, ordinal=False, lang='en')
u'twelve point five eight'

Can you help me to tackle them?
Thanks a lot!
Best regards,
Tien

unicode problem with python2.7, lang="de"

Hello, thanks for this module!

I came across a unicode problem. You can see this works with python3:

$ python3 -c "import num2words; print(num2words.num2words(0.5, lang='de'))"
null Komma fünf null

but fails with python 2.7:

$ python2.7 -c "import num2words; print(num2words.num2words(0.5, lang='de'))"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/num2words/__init__.py", line 50, in num2words
    return converter.to_cardinal(number)
  File "/usr/local/lib/python2.7/site-packages/num2words/base.py", line 93, in to_cardinal
    return self.to_cardinal_float(value)
  File "/usr/local/lib/python2.7/site-packages/num2words/base.py", line 127, in to_cardinal_float
    out.append(str(self.to_cardinal(curr)))
UnicodeEncodeError: 'ascii' codec can't encode character u'\xfc' in position 1: ordinal not in range(128)

The problem being the call to str() which in python3 returns a unicode string, but in 2.7 str() can only return ASCII bytes. (In 2.7 unicode() would need to be called instead of str() to get a unicode string)

how to use to_year() functions

I see in code there are calls to to_year() function, example:
print(n2w.to_year(2001))

in Num2Word_DE class. What's the right way to access this function?

Py3.3 canot run

Py3.3 embedded, for editor like Sublime text (so I cannot use Py3.4), cannot run. Err on importing num2words.

File "num2words\lang_EN.py", line 50
def merge(self, (ltext, lnum), (rtext, rnum)):
SyntaxError: invalid syntax

Bugs in tanslation from 30 to 40 in spanish

In [23]: for n in range(29, 41):
print num2words.num2words(n, lang='es')
....:
vientinueve
vientinueve y uno
vientinueve y dos
vientinueve y tres
vientinueve y cuatro
vientinueve y cinco
vientinueve y seis
vientinueve y siete
vientinueve y ocho
vientinueve y nueve
vientinueve y diez
cuarenta

Added TR translation

I have uploaded file for TR translation. File name is lang_TR.py
I recently startep using ERPNEXT and I need the Turkish translation for numbers.

Test file test_tr.py to follow.

Support Python 2.5

Please add support to in Python 2.5, i have a old system to use it. I get this error:

In [12]: import num2words

SyntaxError: future feature unicode_literals is not defined (lang_EN.py, line 17)

u before result

Hi,

Alway shows the 'u' vowel at the start of each result, is it normal?

num2words(111999, lang='es')
u'ciento once mil novecientos noventa y nueve'

Wrong float interpretation

I'm running the example as:

num2words(11)
u'eleven'

And then:

num2words(11.10)
u'eleven point zero nine'

Somehow there is a rounding somewhere doing it wrong.

Installation on windows server

Hi,

We installed num2word on a windows server but the module l10n_ca_account_check_writing doesn't find it. Is there a special install for windows server? or special code modification?

Thanks

french ordinals

Hello again!

from num2words import num2words
print num2words(5, lang="fr", ordinal=True)
print num2words(9, lang="fr", ordinal=True)

produces:

cinqième
neufième

Expected result:

cinquième
neuvième

Source: I am French.
Thanks!

Missing negative cases in PL language

int2word function is missing case when value is negative -> returning exception
ValueError: invalid literal for int() with base 10: '-'

Proposal fix:
if n < 0: return ' '.join([u'minus', int2word(abs(n))])

This is the same as for RU language except Polish word :)

floating point?

Doesn't seem to support floating point well.

>>> num2words(111.3)
u'one hundred and eleven point two nine'
>>> num2words(.1234)
u'zero point one two'

"one hundred, ninety-eight" vs. "one hundred ninety-nine" vs. "one hundred and ninety-eight"

Comparing the outputs of Taro Ogawa's original library (namely num2word_EN.to_card) and yours, I found some recurring discrepancies:

one hundred and ninety-eight
one hundred, ninety-eight (the "and" is replaced by a comma)

one hundred and ninety-nine
one hundred ninety-nine (the "and" is suppressed)

two hundred and ninety-eight
two hundred, ninety-eight

two hundred and ninety-nine
two hundred, ninety-nine

...

Taro Ogawa's gives the expected result (21124) on the 17th problem of the Projet Euler: http://projecteuler.net/problem=17. Yours give 21070. Using lang="en" or lang="en_GB" makes no difference.

0.5.4 installation is failing

root@avi-dev:/tmp/tt# /tmp/tt/bin/pip install num2words
Downloading/unpacking num2words
Downloading num2words-0.5.4-py2-none-any.whl (94kB): 94kB downloaded
Installing collected packages: num2words
Cleaning up...
num2words is in an unsupported or invalid wheel
Storing debug log for failure in /root/.pip/pip.log

to_currency with cents false seem to be broken

to_currency form that takes currency as argument with cents=False seem to produce unexpected results when there is not enough precision available.

For instance num2words(Decimal('38.4'), lang='lv', to='currency', seperator=' un', cents=False, currency='EUR) results in Trīsdesmit astoņi euro un 04 centi.

Is this expected behaviour? All converters that support this form seem to be affected.

left, right = n.split('.') seems to be too naive for right part since it doesn't respect position.

Ein vs. Eins in German

Hi,
Currently num2words(1, lang='de') gives "eins" which is correct in certain contexts (e.g. if one refers to the digit one or "Page 1" --> "Seite Eins") but wrong in other cases (if it is followed by a noun, e.g. "1 Meter'" => Ein Meter", "Eins Meter" would be wrong). I'm not sure how this can be handled properly, maybe by an additional flag so that the user can decide weather "eins" or "ein" should be returned? If this is to much work I would suggest changing it to "ein", because from my feeling this variant is used more often. But of course this depends on the context, maybe other German speaker can comment on this as well.

Implement Norwegian language support

Hi, I have started translating the german version into norwegian, but not entirely sure what everything is and what to translate. Is there a language guide I could follow?

Vietnam lang conversion is wrong !

in Vietnam lang :
145001000 read correctly is : "một trăm bốn lăm triệu không trăm lẻ một ngàn"
you can fix it :)
+> test case wrong :
self.assertEqual(num2words(523056, lang="vi_VN"), "năm trăm hai mươi ba nghìn năm mươi sáu")
+> fix right : self.assertEqual(num2words(523056, lang="vi_VN"), "năm trăm hai mươi ba nghìn không trăm năm mươi sáu")

P/S : sorry my english is bad !

Include license files in the pypi tarball

Hi

I am packaging num2words in Fedora, I see you are using the GPL license but you do not include the license file in the pypi tarball, can you please add the sile so I have not to patch your app at the moment of create the rpm

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.