【问题标题】:How to find common phrases from a text document如何从文本文档中查找常用短语
【发布时间】: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)

【问题讨论】:

    标签: python nltk


    【解决方案1】:

    我没有使用过nltk,但我怀疑问题在于from_words 接受字符串或令牌(?)对象。

    类似于

    with open('MkXVM6ad9nI.txt') as wordfile:
        text = wordfile.read)
    
    tokens = nltk.wordpunct_tokenize(text)
    finder = BigramCollocationFinder.from_words(tokens)
    

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

    【讨论】:

    • 谢谢!那是我的问题。
    猜你喜欢
    • 1970-01-01
    • 2021-04-29
    • 2018-08-22
    • 2010-12-28
    • 2011-12-19
    • 1970-01-01
    • 1970-01-01
    • 2013-11-06
    • 1970-01-01
    相关资源
    最近更新 更多