本文整理匯總了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)