【发布时间】:2014-06-07 09:05:56
【问题描述】:
我有一个包含很多 cmets/句子的文本文件,我想以某种方式找到文档本身中重复的最常见的短语。我试着用 NLTK 摆弄一下,我发现了这个线程:How to extract common / significant phrases from a series of text entries
但是,在尝试之后,我得到了如下奇怪的结果:
>>> finder.apply_freq_filter(3)
>>> finder.nbest(bigram_measures.pmi, 10)
[('m', 'e'), ('t', 's')]
在另一个“这很有趣”短语很常见的文件中,我得到一个空列表 []。
我该怎么做呢?
这是我的完整代码:
import nltk
from nltk.collocations import *
bigram_measures = nltk.collocations.BigramAssocMeasures()
trigram_measures = nltk.collocations.TrigramAssocMeasures()
# change this to read in your data
finder = BigramCollocationFinder.from_words('MkXVM6ad9nI.txt')
# only bigrams that appear 3+ times
finder.apply_freq_filter(3)
# return the 10 n-grams with the highest PMI
print finder.nbest(bigram_measures.pmi, 10)
【问题讨论】: