1

我想列出 NLTK 为 Dracula.txt 报告的搭配。我该怎么做呢?我可以通过将其添加到我的语料库中来找到词频。我还有一个变量 DracWords dracWords = mycorpus.words('Dracula.txt'),其中包含 Dracula 文本中的单词。从这里我可以做频率分布,但我现在想要的是列出它的搭配。

任何帮助表示赞赏。

4

2 回答 2

1

你可以试试这个:

from collections import Counter

text = 'List the collocations for a txt file'
words = text.split()
nextword = iter(words)
next(nextword)

print(Counter(zip(words, nextword)))

你会得到:

Counter({('txt', 'file'): 1, ('List', 'the'): 1, ('collocations', 'for'): 1, ('for', 'a'): 1, ('the', 'collocations'): 1, ('a', 'txt'): 1})

希望这可以帮助。

于 2017-01-24T07:17:45.890 回答
1

谢谢大家。能够得到它

nltk.Text(mycorpus.words('Dracula.txt')).collocations()
于 2017-01-24T07:52:19.240 回答