【问题标题】:Converting Readability formula into python function将可读性公式转换为python函数
【发布时间】:2018-03-14 11:33:04
【问题描述】:

我得到了一个名为 FRES(Flesch 阅读轻松测试)的公式,用于衡量文档的可读性:

我的任务是编写一个返回文本 FRES 的 python 函数。因此,我需要将这个公式转换为 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
from itertools import chain
from nltk.corpus import gutenberg
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...
    """

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))

这个问题非常棘手,但至少现在它给了我一个结果而不是错误消息,但不知道为什么它给了我一个不同的结果。

任何帮助将不胜感激。谢谢。

【问题讨论】:

标签: python regex nltk tokenize flesch-kincaid


【解决方案1】:

顺便说一句,这里有 textstat 库。

from textstat.textstat import textstat
from nltk.corpus import gutenberg

for filename in gutenberg.fileids():
    print(filename, textstat.flesch_reading_ease(filename))

如果你一心想自己编写代码,首先你必须

  • 判断标点是否为单词
  • 定义如何计算编号。单词中的音节。

如果标点符号是一个单词并且音节由您问题中的正则表达式计算,那么:

import re
from itertools import chain
from nltk.corpus import gutenberg

def num_syllables_per_word(word):
    return len(re.findall('[aeiou]+[^aeiou]+', word))

for filename in gutenberg.fileids():
    sents = gutenberg.sents(filename)
    words = gutenberg.words(filename) # i.e. list(chain(*sents))
    num_sents = len(sents)
    num_words = len(words)
    num_syllables = sum(num_syllables_per_word(w) for w in words)
    score = 206.835 - 1.015 * (num_words / num_sents) - 84.6 * (num_syllables / num_words)
    print(filename, score)

【讨论】:

  • 好吧,正如我提到的,我不允许编辑 def count_syllables(word):,我在 gutenberg.fileids(): 中为文件名实现了你的编码,并得到了这个结果预期:99.40 ...实际:92.84866041488623
  • 我也尝试了另一个选项并导入 textstat 并实现该功能并得到-44
  • 我不确定textstats 是如何工作的,所以我不确定那是什么。最有可能的是,预期和实际之间的差异来自两个假设,(1)决定什么是单词(例如,标点符号是单词吗?(2)你如何计算音节。顺便说一句,你从哪里得到“预期”价值?这是作业吗?
  • 假设2不是已经成立了吗?通过 def count_syllables(word)?我也根本没有调用 from itertools 导入链
  • 好吧,每次我改变一些东西以适应我没有得到任何结果和错误的假设,我现在拥有的代码是我最接近通过 doctest 的代码
猜你喜欢
  • 1970-01-01
  • 2020-07-23
  • 1970-01-01
  • 1970-01-01
  • 2017-08-29
  • 1970-01-01
  • 2022-07-06
  • 1970-01-01
  • 2018-07-28
相关资源
最近更新 更多