【问题标题】:Python - How to find all number words in a phrase? [closed]Python - 如何查找短语中的所有数字词? [关闭]
【发布时间】:2016-03-17 20:34:47
【问题描述】:

我想知道你怎么可能检测到一个短语中的所有数字词。例如

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

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

【问题讨论】:

  • 这不是一件容易的事。如果没有你的代码来改进这里的任何人都无法做很多事情。
  • 嗯,“三十亿”可以匹配为“三”和“十亿”
  • 如果你知道你的输入,你可能想看看word2number

标签: python regex numbers word


【解决方案1】:

这没有捷径可走,因为 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))

在这个例子中,我只是输入了获得结果所需的字数,但如果您希望结果准确,您将填写很多字(数字)

【讨论】:

  • 谢谢!我想这是我最接近的。
猜你喜欢
  • 2015-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-15
  • 2012-11-06
  • 2014-01-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多