我需要帮助解决我遇到的这个问题。我需要编写一个从文本返回 FRES(Flesch 易读测试)的函数。给定公式:
换句话说,我的任务是把这个公式变成一个 python 函数。
这是我上一个问题的代码:
import nltk
import collections
nltk.download('punkt')
nltk.download('gutenberg')
nltk.download('brown')
nltk.download('averaged_perceptron_tagger')
nltk.download('universal_tagset')
import re
VC = re.compile('[aeiou]+[^aeiou]+', re.I)
def count_syllables(word):
return len(VC.findall(word))
from itertools import chain
from nltk.corpus import gutenberg
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...
"""
for filename in gutenberg.fileids():
sents = gutenberg.sents(filename)
words = gutenberg.words(filename)
num_sents = len(sents)
num_words = len(words)
num_syllables = sum(count_syllables(w) for w in words)
score = 206.835 - 1.015 * (num_words / num_sents) - 84.6 * (num_syllables / num_words)
return(score)
这是我得到的结果:
Failure
Expected :99.40...
Actual :92.84866041488623
**********************************************************************
File "C:/Users/PycharmProjects/a1/a1.py", line 60, in a1.compute_fres
Failed example:
compute_fres(emma) # doctest: +ELLIPSIS
Expected:
99.40...
Got:
92.84866041488623
我的任务是通过 doctest 并得到 99.40 ...我也不允许更改以下代码,因为它是随任务本身提供给我的:
import re
VC = re.compile('[aeiou]+[^aeiou]+', re.I)
def count_syllables(word):
return len(VC.findall(word))
我觉得我已经接近了,但不知道为什么我会得到不同的结果。任何帮助都感激不尽。