Nlp 在nltk语言模型中定义和使用新的平滑方法
我试图为语言模型提供并测试新的平滑方法。我正在使用nltk工具,不想从头开始重新定义一切。那么,有没有办法在nltk模型中定义和使用我自己的平滑方法呢 编辑: 我正在尝试这样做:Nlp 在nltk语言模型中定义和使用新的平滑方法,nlp,nltk,smoothing,language-model,Nlp,Nltk,Smoothing,Language Model,我试图为语言模型提供并测试新的平滑方法。我正在使用nltk工具,不想从头开始重新定义一切。那么,有没有办法在nltk模型中定义和使用我自己的平滑方法呢 编辑: 我正在尝试这样做: def my_平滑方法(模型): #一些使用模型(MLE)计数的代码 model=nltk.lm.MLE(n,平滑法=my\u平滑法) 模型拟合(火车) ,您可以看到MLE的定义。如您所见,没有平滑函数的选项(但在同一文件中还有其他函数,可能其中一些函数适合您的需要?) InterpolatedLanguageMode
def my_平滑方法(模型):
#一些使用模型(MLE)计数的代码
model=nltk.lm.MLE(n,平滑法=my\u平滑法)
模型拟合(火车)
,您可以看到MLE的定义。如您所见,没有平滑函数的选项(但在同一文件中还有其他函数,可能其中一些函数适合您的需要?)
InterpolatedLanguageModel(参见上面的同一文件)接受平滑分类器,该分类器需要实现alpha_gamma(单词、上下文)和unigram_分数(单词),并且是平滑的子类:
model = nltk.lm.InterpolatedLanguageModel(smoothing_cls=my_smoothing_method, order)
因此,如果您真的需要向MLE类添加功能,您可以这样做,但我不确定这是否是一个好主意:
class MLE_with_smoothing(LanguageModel):
"""Class for providing MLE ngram model scores.
Inherits initialization from BaseNgramModel.
"""
def unmasked_score(self, word, context=None):
"""Returns the MLE score for a word given a context.
Args:
- word is expcected to be a string
- context is expected to be something reasonably convertible to a tuple
"""
freq = self.context_counts(context).freq(word)
#Do some smothing
return
到目前为止你试过什么?你能展示一个在你挣扎的地方被剪断的代码吗?@chefhose一个伪代码被添加到问题中。