3

关于将英语单词转换为数字的其他问题已经得到解答,特别是使用库w2n或其他自定义算法。

但是我不知道如何将法语(或一般来说,任何语言的)单词转换为整数,例如:

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

我不是一个流利的法语演讲者,但它肯定不仅仅是试图翻译https://github.com/akshaynagpal/w2n/blob/master/word2number/w2n.py中的单词对吗?

4

4 回答 4

2

只是用谷歌搜索了一下,发现了一个名称非常相似的名为text2num的项目:

提供函数和解析器类:

  • 解析以法语单词表示的数字并将其转换为整数值;

他们的演示:

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

回馈95,这似乎是正确的

于 2019-10-03T12:56:16.943 回答
1

text2num 非常棒!

这是示例;

  1. 确保你已经安装了 text2num,因为现在有 text2num 2.4.0。
    pip install text2num
  1. 导入库。
    from text_to_num import text2num
    
    text2num('quatre-vingt-quinze', "fr")
于 2021-08-12T09:05:11.647 回答
0

您可以使用textblob. 但这并不安全,因为如果您提出“太多请求”,它们可能会被阻止。

信息:https ://textblob.readthedocs.io/en/dev/ 你可以这样做:

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')

现在您可以制作一个数字列表来将字母转换为整数

于 2019-10-03T12:40:20.333 回答
0

创建法语单词和数字等价物的数据库。


将其加载到内存中。


使用“在集合中查找”命令查找等于输入单词的数字。


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

使用 python REG EX 代码进行“搜索”示例:


import re

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

使用 python 字典进行“搜索”示例:


thisdict =  {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
if "model" in thisdict:
print("Yes, 'model' is one of the keys in the thisdict dictionary") 
于 2019-10-03T13:07:23.913 回答