File size: 1,429 Bytes
ec15abf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"""
readability.py — Flesch-Kincaid grade level (no dependencies)
=============================================================
Gives an OBJECTIVE before/after reading grade so the simplification's value is
measurable, not just claimed. That measurable delta is the demo's "wow" and a
useful evaluation metric for the model card.
"""
from __future__ import annotations
import re

_VOWELS = "aeiouy"


def _syllables(word: str) -> int:
    word = re.sub(r"[^a-z]", "", word.lower())
    if not word:
        return 0
    count, prev_vowel = 0, False
    for ch in word:
        is_vowel = ch in _VOWELS
        if is_vowel and not prev_vowel:
            count += 1
        prev_vowel = is_vowel
    if word.endswith("e") and count > 1:
        count -= 1
    return max(1, count)


def _counts(text: str):
    sentences = [s for s in re.split(r"[.!?]+", text) if s.strip()]
    words = re.findall(r"[A-Za-z]+", text)
    n_sent = max(1, len(sentences))
    n_word = max(1, len(words))
    n_syl = sum(_syllables(w) for w in words)
    return n_sent, n_word, n_syl


def flesch_kincaid_grade(text: str) -> float:
    n_sent, n_word, n_syl = _counts(text)
    grade = 0.39 * (n_word / n_sent) + 11.8 * (n_syl / n_word) - 15.59
    return round(max(0.0, grade), 1)


def reading_ease(text: str) -> float:
    n_sent, n_word, n_syl = _counts(text)
    return round(206.835 - 1.015 * (n_word / n_sent) - 84.6 * (n_syl / n_word), 1)