Python顶级Bigrams

2024-06-30 15:48:33 发布

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

import nltk
from nltk.collocations import *

tokens = ['a','b','c','d','b','c','a','b','c']
tokens2 = [['a','b','c','d'],['b','c','a','b','c']]
bigrams = nltk.bigrams(tokens)

fdist = nltk.FreqDist(bigrams)
for i,j in fdist.items():
    print i,j``

print fdist.most_common(2)

上面的代码适用于像tokens一样的输入,但是当我使用tokens2时它会抛出一个错误。最后,我应该让它返回前2个双元组时,给了一组令牌。我们将不胜感激。在


Tags: infromimportmostforitemscommonprint
1条回答
网友
1楼 · 发布于 2024-06-30 15:48:33

如果您有一个令牌列表列表(比如token2

import collections
cnt = collections.Counter()

for toks in token2:
    cnt.update(nltk.bigrams(toks))

print(cnt.most_common(2))

会有用的。如果你所拥有的是完全不同的,比如像tokens,或者你提到的set,一切都可能改变——但是我们无法读懂你的想法,所以你最好编辑你的Q来准确地解释你的目标是什么!在

相关问题 更多 >