Python Forum

Full Version: convert numbers to words
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i want to convert numbers into words. i wrote code in C to do this a couple decades ago. i have also converted it to Pike many years ago. i have also already converted it to Python, but this was as a command. now, i am thinking about a function. what i want to know is, if this is duplicating anything that is built into Python. the code needs a lot of work since the Python version was written when i was beginning in Python, so i will just re-write the command to use the function and put all my re-coding effort into the function.

if there is any other version "out there" i am hoping it is multi-lingual for at least several languages. my version is English-only and i have no experience developing internationalized code.

should i also support float, decimal.Decimal, and even complex numbers? i want to have an optional 2nd argument that is the value to return if the given number value exceeds the maximum value that can be converted.

i think if the given value is float and has a whole number value, i should append " point zero" which would indicate it is float with no fraction. what do you think? this would need to be done however "they" do it for languages beyond English.
this can be done without much effort using NLTK
list can be customized by category
A full (huge) corpus is part of the package.
see: https://www.nltk.org/_modules/nltk/corpu...dlist.html
installation

I have used this 'package' it's a lot of fun.
NLTK seems to do a lot more than i want. i want just conversion to words, not from words. but i am sure there are many other needs for conversion from words. the code i once wrote in Pike only does US English so i could hard code much of it. i have translated it to C and Python3. but i would like to do many more languages. converting from words could be an interesting problem, especially with multi-lingual support. just doing those i am familiar with would be hard enough even if i leave out support for Roman Numerals.

see: http://ipal.org/py/fun/num_to_words_en_us.py
size: 4921 bytes
sha256: 98ac98ca8b4348d731d9723e771b531f76b3adfae1825c1f3080a08448c4a54a
that URL was a function-only file. it has the important code. if you want to get the command code you get it here:

see: http://ipal.net/py/cmd/numwords.py
size: 8549 bytes
sha256: deff582f8ed71aabaec9911d9ff376e4788d218624175208836aa6057e18b900