列出 txt 文件的搭配

List the collocations for a txt file

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

感谢任何帮助。

你可以试试这个:

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})

希望对您有所帮助。

谢谢大家。能够用

得到它
nltk.Text(mycorpus.words('Dracula.txt')).collocations()