鱼C论坛

 找回密码
 立即注册
查看: 2831|回复: 2

[已解决]可读性等级

[复制链接]
发表于 2023-4-5 15:28:42 | 显示全部楼层
  1. import re

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

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

  9.     # 计算字母数量
  10.     letters = re.findall(r'[a-zA-Z]', text)
  11.     n_letters = len(letters)

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

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

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

  19.     # 输出结果
  20.     if grade < 1:
  21.         return 'Before Grade 1'
  22.     elif grade >= 16:
  23.         return 'Grade 16+'
  24.     else:
  25.         return f'Grade {grade}'
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-9-22 10:51

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表