【发布时间】:2012-12-31 03:31:34
【问题描述】:
我有以下代码。我知道我可以使用apply_freq_filter 函数来过滤掉少于频率计数的搭配。但是,在我决定为过滤设置什么频率之前,我不知道如何获取文档中所有 n-gram 元组(在我的情况下为 bi-gram)的频率。如您所见,我使用的是 nltk collocations 类。
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() 工作!