文本分析与可视化技术深度解析
1. n-gram语言模型基础
在自然语言处理中,n-gram语言模型是一种重要的工具。我们可以为其创建熵(entropy)方法,通过计算NgramCounter中每个n-gram的平均对数概率来实现。以下是具体的熵计算函数:
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)
超级会员免费看
订阅专栏 解锁全文

被折叠的 条评论
为什么被折叠?



