Python-如何查找短语中的所有数字词?

Python-如何查找短语中的所有数字词?,python,regex,numbers,word,Python,Regex,Numbers,Word,我想知道你怎么可能发现一个短语中的所有数字词。比如说 math_str = "one times one plus sin(one hundred fifty three) minus three billion" getNumberWords(math_str) #Returns one, one, one hundred fifty three, three billion 是否有正则表达式模式或其他什么 这没有捷径,因为python不懂英语或人类语言,所以需要有一个被视为数字单词的单词列

我想知道你怎么可能发现一个短语中的所有数字词。比如说

math_str = "one times one plus sin(one hundred fifty three) minus three billion"
getNumberWords(math_str) #Returns one, one, one hundred fifty three, three billion

是否有正则表达式模式或其他什么

这没有捷径,因为python不懂英语或人类语言,所以需要有一个被视为数字单词的单词列表

math_str = "one times one plus sin(one hundred fifty three) minus three billion"
allowed = ['one', 'three', 'fifty', 'hundred', 'thousand', 'million', 'billion']

def getNumberWords(math_str):
    math_str = math_str.replace('(', ' ')
    math_str = math_str.replace(')', ' ')
    math_str = math_str.split()

    return [word for word in math_str if word in allowed]

print(getNumberWords(math_str))

在本例中,我只输入了获得结果所需的字数,但如果希望结果准确,则需要填写大量的字数(数字)

这不是一项容易的任务。如果没有你的代码来改进,这里的任何人都做不了什么。好吧,“三十亿”可以匹配为“三十亿”和“十亿”,如果你知道你的输入,你可能想看看谢谢!我想这是最接近我的了。