How to convert French words to integers?

Viewed 901

Other questions have been answered regarding converting English words to numbers, particularly using the library w2n or other custom algorithms.

However I don't know how to convert French (or generically speaking, any language's) words to integers, such as:

>>> word_to_number('quarante-quatre')
44

I'm not a fluent speaker of French, but it's certainly not just trying to translate the words in https://github.com/akshaynagpal/w2n/blob/master/word2number/w2n.py right?

4 Answers

just googled a bit and found a very similarly named project called text2num that:

provides functions and parser classes for:

  • parsing numbers expressed as words in French and convert them to integer values;

their demo:

from text_to_num import text2num
text2num('quatre-vingt-quinze')

gives 95 back, which seems about right

text2num works fantastic!

Here is the example;

  1. Make sure you have text2num is installed as of now there is text2num 2.4.0.
    pip install text2num
  1. Import the library.
    from text_to_num import text2num
    
    text2num('quatre-vingt-quinze', "fr")

You can use textblob. But it is not so safe, since they can be blocked if you make "too many requests".

information: https://textblob.readthedocs.io/en/dev/ You could do something like this:

from textblob import TextBlob

def word_to_number(text):
    blob= TextBlob(text)
    print(blob.translate(from_lang="fr",to="en"))
word_to_number('quarante-quatre')

And now you can make a list of numbers to transform letters to integers

Create a database of french words and the numeric equivalents.


Load it into memory.


Use the 'find in set' command to find the numbers equal to words input.


database to create from http://www.french-linguistics.co.uk/tutorials/numbers/


Numbers 0-19
0   zéro
1   un
2   deux
3   trois
4   quatre
5   cinq
6   six
7   sept
8   huit
9   neuf
10  dix

Use python REG EX code for 'search' example:


import re

txt = "The rain in Spain"
x = re.search("^The.*Spain$", txt)

Use python dictionaries for 'search' example:


thisdict =  {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
if "model" in thisdict:
print("Yes, 'model' is one of the keys in the thisdict dictionary") 
Related