我有以下代碼。我知道我可以使用apply_freq_filter
函數來濾除少於頻率計數的搭配。然而,在我決定爲過濾設置什麼頻率之前,我不知道如何獲取文檔中所有n元組元組的頻率(在我的例子中是雙元組)。正如你所看到的,我正在使用nltk collocations類。在Python中計算n-gram頻率nltk
import nltk
from nltk.collocations import *
line = ""
open_file = open('a_text_file','r')
for val in open_file:
line += val
tokens = line.split()
bigram_measures = nltk.collocations.BigramAssocMeasures()
finder = BigramCollocationFinder.from_words(tokens)
finder.apply_freq_filter(3)
print finder.nbest(bigram_measures.pmi, 100)
您是否嘗試過'finder.ngram_fd.viewitems()'? –
謝謝finder.ngram_fd.viewitems()的作品! – Rkz