我正在寻找一个 python 库/算法/论文来从自由文本中提取杂货列表。
例如:
"One salad and two beers"
应转换为:
{'salad':1, 'beer': 2}
最佳答案
In [1]: from word2number import w2n
In [2]: print w2n.word_to_num("One")
1
In [3]: print w2n.word_to_num("Two")
2
In [4]: print w2n.word_to_num("Thirty five")
35
您可以使用此包和您可以根据需要实现的其他内容转换为数字。
安装这个包。
pip install word2number
更新
你可以这样实现。
from word2number import w2n
result = {}
input = "One salad and two beers"
b = input.split()
for i in b:
if type(w2n.word_to_num(i)) is int:
result[b[b.index(i)+1]] = w2n.word_to_num(i)
结果
{'beers': 2, 'salad': 1}
关于python - 从自由文本中提取购物 list ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38419395/