2015-12-11 48 views
3

我看到多個問題和答案,說NLTK搭配不能超越雙克和三克。nltk quadgram搭配發現者

例如,這一個 - How to get n-gram collocations and association in python nltk?

我看到有一個叫

nltk.QuadgramCollocationFinder

東西

類似

nltk.BigramCollocationFinder和nltk.TrigramCollocationFinder

但是在同一時間不能看到類似的東西

nltk.collocations.QuadgramAssocMeasures()

類似 nltk.collocations.BigramAssocMeasures()和nltk.collocations.TrigramAssocMeasures()

是什麼NLTK的目的。 QuadgramCollocationFinder,如果它不可能(沒有黑客)找到超過bi和tri克的n-gram。

也許我錯過了一些東西。

感謝,

添加代碼和更新問題,按照從Alvas輸入,這現在工作

import nltk 
from nltk.collocations import * 
from nltk.corpus import PlaintextCorpusReader 
from nltk.metrics.association import QuadgramAssocMeasures 

bigram_measures = nltk.collocations.BigramAssocMeasures() 
trigram_measures = nltk.collocations.TrigramAssocMeasures() 
quadgram_measures = QuadgramAssocMeasures() 

the_filter = lambda *w: 'crazy' not in w 

finder = BigramCollocationFinder.from_words(corpus) 
finder.apply_freq_filter(3) 
finder.apply_ngram_filter(the_filter) 
print (finder.nbest(bigram_measures.likelihood_ratio, 10)) 


finder = QuadgramCollocationFinder.from_words(corpus) 
finder.apply_freq_filter(3) 
finder.apply_ngram_filter(the_filter) 
print(finder.nbest(quadgram_measures.likelihood_ratio,10)) 
+0

更新您的NLTK'PIP安裝-U nltk',你應該可以通過'from nltk.metrics.association import QuadgramAssocMeasures'獲得QuadgramAssocMeasures https://github.com/nltk/nltk/blob/develop/nltk/metrics/association.py#L2 98 – alvas

+0

非常感謝陛下!這現在起作用。假設我已經擁有它,沒必要做pip安裝。爲什麼大家都在說,超越卦不起作用? NLTK更新了Quadgrams,因爲在stackoverflow上的其他問題也許現在NLTK也有Quadgrams? – Kumar

+0

父親對我來說有點太過分了,叫我alvas'會做; P。是的,NLTK在過去的2-3年裏得到了巨大的改進。 'QuadgramCollocationFinder'和'QuadgramAssocMeasures'有點新鮮。但來自http://stackoverflow.com/questions/18672082/how-to-get-n-gram-collocations-and-association-in-python-nltk的其他答案是試圖說,是沒有簡單解決方案來實現一個通用的NgramCollocationFinder,'from_words(cls,words)'函數的公式對於ngram的每個順序都是不同的。 – alvas

回答

2

repo

from nltk.metrics.association import QuadgramAssocMeasures 
+0

謝謝你的建議。雖然可以ü請讓我知道爲什麼Bi和Trigrams措施是nltk.collocations的一部分,爲什麼QuadgramAssocMeasures從nltk.metrics.association中導入 – Kumar

+0

您可以在'nltk.collocations'中找到'BigramAssocMeasures'的原因是因爲import在https://github.com/nltk/nltk/blob/develop/nltk/collocations.py#L39。「BigramAssocMeasures」的真正位置實際上是在'nltk.metrics.association'中。所以這是一個功能,但不是一個錯誤。 – alvas

+0

不用擔心,在1-2周內,'QuadgramAssocMeasures'也應該添加到'nltk.collocations'中。還有其他更重要的錯誤修復=) – alvas