pdl666 发表于 2023-4-5 15:27:00

可读性等级

题目描述

不同书的阅读群体不一样。例如,一本书中可能有许多很长的复杂单词;而较长的单词可能与较高的阅读水平有关。同样,较长的句子也可能与较高的阅读水平相关。研究者开发了许多可读性测试,给出了计算文本阅读水平的公式化过程。其中一个可读性测试是Coleman Liau指标:文本的Coleman Liau指标旨在划分理解文本所需的阅读水平等级。Coleman Liau指标公式如下

index = 0.0588 * L - 0.296 * S - 15.8

其中,L是文本中每100个单词的平均字母数,S是文本中每100个单词的平均句子数。

考虑以下文本:

Congratulations! Today is your day. You're off to Great Places! You're off and away!
该文本有65个字母,4个句子,14个单词。文本中每100个单词的平均字母数是L=65/14*100=464.29;文本中每100个单词的平均句子数是 S=4/14*100=28.57。 代入Coleman Liau指标公式,并向最近的整数取整,我们得到可读性指数为3级。



本题输入参数为字符串,返回可读性等级。



实现要求:

(1)    若计算结果小于1,输出“Before Grade 1”;

(2)    若计算结果大于或等于16,输出“Grade 16+”;

(3)    除(1)和(2)外,输出“Grade X”,X为相应等级;

(4)    字母包括大写字母和小写字母(不考虑数字和标点符号);

(5)    以空格分隔作为标准区分单词,如;

It was a bright cold day in April, and the clocks were striking thirteen. Winston Smith, his chin nuzzled into his breast in an effort to escape the vile wind, slipped quickly through the glass doors of Victory Mansions, though not quickly enough to prevent a swirl of gritty dust from entering along with him.
55 words
(6)    句号(.)、感叹号(!)或问号(?)表示句子的结尾。如

Mr. and Mrs. Dursley, of number four Privet Drive, were proud to say that they were perfectly normal, thank you very much.

3 sentences

输入

字符串


输出

可读性等级
输入样例1
Congratulations! Today is your day. You're off to Great Places! You're off and away!
输出样例1
Grade 3

歌者文明清理员 发表于 2023-4-5 15:28:42

import re

def readability(text):
    # 计算单词数量
    words = re.findall(r'\b\w+\b', text)
    n_words = len(words)

    # 计算句子数量
    sentences = re.findall(r'[.!?]+', text)
    n_sentences = len(sentences)

    # 计算字母数量
    letters = re.findall(r'', text)
    n_letters = len(letters)

    # 每 100 个单词平均字母数
    L = n_letters / n_words * 100

    # 每 100 个单词平均句子数
    S = n_sentences / n_words * 100

    # 计算 Coleman Liau 指标
    index = 0.0588 * L - 0.296 * S - 15.8
    grade = round(index)

    # 输出结果
    if grade < 1:
      return 'Before Grade 1'
    elif grade >= 16:
      return 'Grade 16+'
    else:
      return f'Grade {grade}'

isdkz 发表于 2023-4-5 15:29:44

def coleman_liau_index(text):
    letters = 0
    words = 0
    sentences = 0

    for char in text:
      if char.isalpha():
            letters += 1
      elif char.isspace():
            words += 1
      elif char in ".!?":
            sentences += 1

    words += 1

    L = (letters / words) * 100
    S = (sentences / words) * 100
    index = round(0.0588 * L - 0.296 * S - 15.8)

    if index < 1:
      return "Before Grade 1"
    elif index >= 16:
      return "Grade 16+"
    else:
      return f"Grade {index}"


# Test case
input_text = input()
print(coleman_liau_index(input_text))
页: [1]
查看完整版本: 可读性等级