如何将法语单词转换成整数?

2024-06-28 19:30:27 发布

您现在位置:Python中文网/ 问答频道 /正文

关于将英语单词转换成数字,特别是使用库w2n或其他自定义算法,已经回答了其他问题。你知道吗

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

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

我法语说得不流利,但这肯定不仅仅是为了翻译https://github.com/akshaynagpal/w2n/blob/master/word2number/w2n.py中的单词,对吧?你知道吗


Tags: tohttpsgithub算法语言number数字整数
3条回答

您可以使用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')

现在你可以做一个数字列表,把字母转换成整数

创建一个法语单词和数字等价物的数据库。你知道吗


把它载入内存。你知道吗


使用“find in set”命令查找与输入的单词相等的数字。你知道吗


要从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") 

只是在google上搜索了一下,发现了一个名为text2num的类似项目:

provides functions and parser classes for:

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

他们的演示:

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

回馈95,这似乎是正确的

相关问题 更多 >