我正在分析NLTK包中的起始地址语料库,作为python编程入门课程的一部分。我想知道语料库中每一篇文章的平均句子长度(这样我以后可以比较它们),但我似乎被困在这里了。在
我创建了这个函数:
def averageSentence(text):
sents = inaugural.sents(fileids=['fileid_here.txt']
avg = sum(len(word) for word in sents) / len(sents)
print(avg)
它(如果我是正确的)应该给出一篇文章的平均句子长度。现在,我知道我需要一个循环。我不应该用我刚刚定义的这个函数来创建一个相对简单直接的for循环吗?这很令人沮丧。在
编辑:这是我所取得的成就:
^{pr2}$
尝试:
注意,当分母足够大时+1并不重要。在
Mirco在所有文本中平均句子长度
以下代码是一行代码,但不鼓励使用,因为您可能已经实现了生成器两次:
^{2}$所有文本的Marco平均句子长度:
每篇文章的平均句子长度:
所有文本的平均宏平均字长:
相关问题 更多 >
编程相关推荐