如何从文本文档中查找常用短语

问题描述 投票:0回答:2

我有一个包含很多评论/句子的文本文件,我想以某种方式找到文档本身中重复的最常见短语。我尝试用 NLTK 摆弄一下,发现了这个线程:如何从一系列文本条目中提取常见/重要短语

但是,尝试之后,我得到了如下奇怪的结果:

>>> 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)
python nltk
2个回答
6
投票

我没有使用过

nltk
,但我怀疑问题在于
from_words
接受字符串或标记(?)对象。

类似的东西

with open('MkXVM6ad9nI.txt') as wordfile:
    text = wordfile.read)

tokens = nltk.wordpunct_tokenize(text)
finder = BigramCollocationFinder.from_words(tokens)

可能会起作用,尽管也可能有专门的文件 API。


0
投票

或者,您可以尝试:
finder = BigramCollocationFinder.from_words(text.split())

© www.soinside.com 2019 - 2024. All rights reserved.