【发布时间】:2015-12-11 18:53:28
【问题描述】:
我看到多个问题和答案说 NLTK 搭配不能超出双和三克。
例如这个 - 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 的目的是什么?如果它不可能(没有 hack)找到双元和三元以外的 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))
【问题讨论】:
-
更新您的 NLTK
pip install -U nltk,您应该能够使用from nltk.metrics.association import QuadgramAssocMeasuresgithub.com/nltk/nltk/blob/develop/nltk/metrics/… 获得 QuadgramAssocMeasures -
非常感谢陛下!这现在有效。假设我已经拥有它,则不必进行 pip 安装。为什么每个人都说三字之外的东西不起作用? NLTK 已经更新了 Quadgrams,因为关于 stackoverflow 的其他问题可能现在 NLTK 也有 Quadgrams?
-
Sire 对我来说有点过分了,打电话给我
alvas会做 ;P 。是的,NLTK 在过去的 2-3 年里有了很大的改进。QuadgramCollocationFinder和QuadgramAssocMeasures有点新。但是stackoverflow.com/questions/18672082/… 的另一个答案想说的是,没有简单的解决方案来实现一个通用的 NgramCollocationFinder,from_words(cls, words)函数的公式对于每个 ngram 顺序都是不同的。 -
看一下trigram的列联表:github.com/nltk/nltk/blob/develop/nltk/metrics/…,现在看一下quagram:github.com/nltk/nltk/blob/develop/nltk/metrics/…随着ngram的阶数增加,列联表变得更加复杂。边际表也是如此:github.com/nltk/nltk/blob/develop/nltk/metrics/…
-
ok Sire Alvas -;) 以后会叫你 Alvas...我会看看 github
标签: python nlp nltk n-gram collocation