假设我有100条tweet。
在这些推文中,我需要摘录:1)食品名称,2)饮料名称。
tweet示例:
"Yesterday I had a coca cola, and a hot dog for lunch, and some bana split for desert. I liked the coke, but the banana in the banana split dessert was ripe"
我有两本词典要用。一个是食品名称,一个是饮料名称。
食品名称词典中的示例:
“热狗”
“香蕉”
“香蕉分割”
饮料名称词典中的示例:
“可乐”
“可乐”
“可口可乐”
我应该能够提取的内容:
[[["coca cola", "beverage"], ["hot dog", "food"], ["banana split", "food"]],
[["coke", "beverage"], ["banana", "food"], ["banana split", "food"]]]
词典中的名字可以是1-5个单词。如何使用我的词典从tweets中提取n-gram呢?在
不确定到目前为止您尝试了什么,下面是一个在
nltk
和dict()
中使用ngrams
的解决方案输出:
^{pr2}$方法2(避免使用“可口可乐”和“可乐”)
输出:
注意这里不需要
nltk
。在这里有一个简单的解决方案:
分句函数取自此处:Splitting a sentence by ending characters
相关问题 更多 >
编程相关推荐