我知道如何使用NLTK得到bigram和trigram的搭配,我将它们应用到我自己的语料库中。代码如下。
我唯一的问题是如何打印出带有PMI值的birgram?我多次搜索NLTK文档。要么我漏掉了什么要么就不存在了。
import nltk
from nltk.collocations import *
myFile = open("large.txt", 'r').read()
myList = myFile.split()
myCorpus = nltk.Text(myList)
trigram_measures = nltk.collocations.TrigramAssocMeasures()
finder = TrigramCollocationFinder.from_words((myCorpus))
finder.apply_freq_filter(3)
print finder.nbest(trigram_measures.pmi, 500000)
我想你在找
score_ngram
。无论如何,你不需要打印功能。你就自己嚼吧。。。如果查看
nlkt.collocations.TrigramCollocationFinder
(请参见http://www.nltk.org/_modules/nltk/collocations.html)的源代码,您会发现它返回一个TrigramCollocationFinder().score_ngrams
:因此您可以直接调用
score_ngrams()
,而不必获取nbest
,因为它无论如何都会返回一个排序列表尝试:
[输出]:
相关问题 更多 >
编程相关推荐