我需要帮助解决我遇到的这个问题。我需要编写一个从文本返回 FRES(Flesch 阅读轻松测试)的函数。给出公式:
换句话说,我的任务就是把这个公式变成一个Python函数。
这是来自的代码我之前的问题 https://stackoverflow.com/questions/49251629/converting-readability-formula-into-python-function/49259380?noredirect=1#comment85541910_49259380:
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
我的任务是通过文档测试并取得 99.40 的成绩...
我也不允许更改以下代码,因为它是随任务本身一起提供给我的:
import re
VC = re.compile('[aeiou]+[^aeiou]+', re.I)
def count_syllables(word):
return len(VC.findall(word))
我觉得我已经很接近了,但不确定为什么我得到了不同的结果。任何帮助都感激不尽。