Change language

Python | Number to words using num2words

| | | |

Installation
You can easily install num2words using pip.

 pip install num2words 

Consider the following two excerpts from various files taken from 20 newsgroups, the popular NLP database. The preprocessing of 20 newsgroups continued to be of interest.

In article, Martin Preston writes: Why not use the PD C library for reading / writing TIFF files? It took me a good 20 minutes to start using them in your own app.
ISCIS VIII is the eighth of a series of meetings which have brought together computer scientists and engineers from about twenty countries. This year’s conference will be held in the beautiful Mediterranean resort city of Antalya, in a region rich in natural as well as historical sites.

In the two excerpts above, you can see that the number “20»Appears in both numerical and alphabetical form. Simply performing preprocessing steps that include tokenization, lemmatization, etc. will not be able to map "20" and "twenty" to the same stem, which has contextual meaning. Fortunately, we have a built-in library num2words that solves this problem in one line.

Below is an example of using the tool.

from num2words import num2words

 
# Most common usage.

print (num2words ( 36 ))

 
# Other options, depending on the type of article.

print (num2words ( 36 , to = ’ordinal’ ))

print (num2words ( 36 , to = ’ordinal_num’ ))

print (num2words ( 36 , to = ’year’ ))

print (num2words ( 36 , to = ’ currency’ ))

 
# Language support.

print (num2words ( 36 , lang = ’es’ ))

Exit:

 thirty-six thirty-sixth 36th zero euro, thirty-six cents treinta y seis 

Therefore, in the preprocessing step, you can convert ALL numeric values ​​to words for greater precision in subsequent steps.

Links: https://pypi.org/project/num2words/

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically