如何将估计器传入NLTK的ngramodel?

2024-05-23 16:50:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用NLTK训练一个使用Laplace估计器的二元模型。ngramodel的构造者是:

def __init__(self, n, train, pad_left=True, pad_right=False,
             estimator=None, *estimator_args, **estimator_kwargs):

经过一番研究,我发现一个有效的语法是:

^{pr2}$

虽然这似乎是正确的,但我对最后两个论点感到困惑。主要是,为什么“估计器”参数是lambda函数,以及如何与laplaceproblist交互?


Tags: 模型selfrightnonefalsetrueinitdef
1条回答
网友
1楼 · 发布于 2024-05-23 16:50:16

目前,您可以使用lambda函数从分布中返回Freqdist,例如

from nltk.model import NgramModel
from nltk.corpus import brown
from nltk.probability import LaplaceProbDist

est = lambda fdist: LaplaceProbDist(fdist)

corpus = brown.words(categories='news')[:100]
lm = NgramModel(3, corpus, estimator=est)


print lm
print (corpus[8], corpus[9], corpus[12] )
print (lm.prob(corpus[12], [corpus[8], corpus[9]]) )
print

[出来]:

^{pr2}$

但是请注意,NLTK中包含LanguageModel对象的model包是“正在构建中的”,因此当稳定版本出现时,上面的代码可能无法工作。在

要更新与model包相关的问题,请定期检查以下问题:

相关问题 更多 >