3

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?

augustomen
  • 8,977
  • 3
  • 43
  • 63
  • 1
    Actually it's not that easy, in french you say "quatre vingt dix neuf" for 99, which directly translates to "four twenty ten nine" (four times twenty plus ten plus nine). – palvarez Oct 03 '19 at 12:32
  • 3
    Oh actually w2n supports french https://pypi.org/project/num2words/ – palvarez Oct 03 '19 at 12:33
  • If there isn't anything out there that does this, you could probably write your own converter, inspired by the w2n. But the rules would have to change to match French ones (and they are very different and not very simple) – Sorix Oct 03 '19 at 12:34
  • I didnt have a clue about numbering in french until now (I've googled https://study.com/academy/lesson/how-to-count-to-100-in-french.html), and I would have to say it is quite a challenging task :) My idea would be to make dictionary of common values, based on which I would check the input first, and if 0 results similar to input are found, I would start with dictionary element combinations according to algorithms described on that page with french counting :) – user5214530 Oct 03 '19 at 12:35
  • @palvarez Yeah but that's the opposite of what I want (words to numbers). – augustomen Oct 03 '19 at 12:35
  • @palvarez that seems to be converting numbers to words which is definitely easier to do – Sorix Oct 03 '19 at 12:35
  • num2words do the opposite – Yassine Faris Oct 03 '19 at 12:36
  • If you read French or try translating this https://www.commentcamarche.net/forum/affich-31266007-convertir-mot-en-chiffre , it might give you an idea from which you could start – Sorix Oct 03 '19 at 12:38
  • Just an additional comment for English: I decided not to use w2n because of all its error cases. Instead, I'm using https://github.com/exogen/text2num – augustomen Oct 03 '19 at 16:08

4 Answers4

2

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

Community
  • 1
  • 1
Sam Mason
  • 15,216
  • 1
  • 41
  • 60
  • Well that's not a very descriptive library name, but it works. `text2num` is very strict regarding the expected input, like `text2num('dix-neuf')` works but `text2num('Dix-neuf')` and `text2num('dix neuf')` both don't (even with `relaxed=True`). But `alpha2digits()` seems quite flexible. – augustomen Oct 03 '19 at 15:30
  • it's not my library, but maybe they'd accept some pull requests! `alpha2digit` uses regular expressions when looking for words rather than just `split`ting the string. it also calls `lower` on each word. maybe you could try this? – Sam Mason Oct 03 '19 at 15:35
  • 1
    `alpha2digits` works perfectly in my context. The comment on text2num is more a flag for those who read this answer in the future than a criticism. Thanks for the suggestion! – augustomen Oct 03 '19 at 15:56
1

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")
Egidius
  • 971
  • 1
  • 10
  • 22
0

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

Lucas Damian
  • 178
  • 1
  • 2
  • 11
0

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") 
Joe McKenna
  • 135
  • 5
  • I think this would take a long time, a lot of lines, and even would not cover all numbers. There are different libraries suggested in this post and they are helpful. – Egidius Aug 17 '21 at 10:40