【问题标题】:nltk quadgram collocation findernltk四边形搭配查找器
【发布时间】: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 QuadgramAssocMeasures github.com/nltk/nltk/blob/develop/nltk/metrics/… 获得 QuadgramAssocMeasures
  • 非常感谢陛下!这现在有效。假设我已经拥有它,则不必进行 pip 安装。为什么每个人都说三字之外的东西不起作用? NLTK 已经更新了 Quadgrams,因为关于 stackoverflow 的其他问题可能现在 NLTK 也有 Quadgrams?
  • Sire 对我来说有点过分了,打电话给我alvas 会做 ;P 。是的,NLTK 在过去的 2-3 年里有了很大的改进。 QuadgramCollocationFinderQuadgramAssocMeasures 有点新。但是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


【解决方案1】:

来自repo

from nltk.metrics.association import QuadgramAssocMeasures

【讨论】:

  • 感谢您的建议。虽然你能告诉我为什么 Bi 和 Trigrams 度量是 nltk.collocations 的一部分,以及为什么 QuadgramAssocMeasures 是从 nltk.metrics.association 导入的
  • 之所以能在nltk.collocations 中找到BigramAssocMeasures 是因为github.com/nltk/nltk/blob/develop/nltk/collocations.py#L39 的导入。 BigramAssocMeasures的真实位置其实在nltk.metrics.association。所以这是一种特性,但不是错误。
  • 不用担心,在 1-2 周内,QuadgramAssocMeasures 也应该添加到 nltk.collocations。还有其他更重要的错误需要修复 =)
  • 所以一旦 QuadgramAssocMeasures 转移到 nltk.collocations...它会像今天这样工作吗,猜猜那会贬值吗?再次感谢..
  • 顺便说一句,刚刚观察到标记化中的某处似乎也在吐出标点符号......所以我看到 ("'", 's', 'really', 'helpful') 而不是 ("那是”,“真的”,“有帮助”,“信息”)
猜你喜欢
  • 2012-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-22
  • 1970-01-01
  • 1970-01-01
  • 2015-08-21
  • 2017-04-29
相关资源
最近更新 更多