當前位置: 首頁>>代碼示例>>Python>>正文


Python nltk.probability方法代碼示例

本文整理匯總了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) 
開發者ID:foxbook,項目名稱:atap,代碼行數:12,代碼來源:model.py

示例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) 
開發者ID:foxbook,項目名稱:atap,代碼行數:16,代碼來源:model.py


注:本文中的nltk.probability方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。