我试图实现一个python函数,该函数通过使用wiki中的公式返回文本的Flesch Kincaid可读性测试:Flesch reading-ease test
我也遇到了一个与我同样目标的问题,得到的答案是:Converting Readability formula into python function
因此,作为一种指导,我对我的实现采用了类似的方法,并以以下代码结束:
import nltk
import collections
nltk.download('punkt')
nltk.download('gutenberg')
nltk.download('brown')
# DO NOT MODIFY THE CODE BELOW #
import re
VC = re.compile('[aeiou]+[^aeiou]+', re.I)
def count_syllables(word):
return len(VC.findall(word))
def compute_fres(text):
"""Return the FRES of a text.
>>> emma = nltk.corpus.gutenberg.raw('austen-emma.txt')
>>> compute_fres(emma) # doctest: +ELLIPSIS
99.40...
"""
tToken = nltk.word_tokenize(text)
for f in nltk.corpus.gutenberg.fileids():
nsents = len(nltk.corpus.gutenberg.sents(f))
nwords = len(nltk.corpus.gutenberg.words(f))
nsyllables = sum(count_syllables(w) for w in words)
fres = 206.835 - 1.015 * (nwords / nsents) - 84.6 * (nsyllables / nwords)
return len(fres.findall(tToken))
然后我运行了我的代码,得到了以下错误消息:
^{pr2}$像其他用户的帖子一样,我的目标是通过实现正确的函数来通过doctest。我也可以假设他/她和我在同一个编程类。在
我对python有点生疏,所以我不确定我做错了什么。在
目前没有回答
相关问题 更多 >
编程相关推荐