本文整理汇总了Python中nltk.probability方法的典型用法代码示例。如果您正苦于以下问题:Python nltk.probability方法的具体用法?Python nltk.probability怎么用?Python nltk.probability使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类nltk
的用法示例。
在下文中一共展示了nltk.probability方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: logscore
# 需要导入模块: import nltk [as 别名]
# 或者: from nltk import probability [as 别名]
def logscore(self, word, context):
"""
For a given string representation of a word, and a word context,
computes the log probability of this word in this context.
"""
score = self.score(word, context)
if score == 0.0:
return float("-inf")
return log(score, 2)
示例2: entropy
# 需要导入模块: import nltk [as 别名]
# 或者: from nltk import probability [as 别名]
def entropy(self, text):
"""
Calculate the approximate cross-entropy of the n-gram model for a
given text represented as a list of comma-separated strings.
This is the average log probability of each word in the text.
"""
normed_text = (self._check_against_vocab(word) for word in text)
entropy = 0.0
processed_ngrams = 0
for ngram in self.ngram_counter.to_ngrams(normed_text):
context, word = tuple(ngram[:-1]), ngram[-1]
entropy += self.logscore(word, context)
processed_ngrams += 1
return - (entropy / processed_ngrams)