Punteggio di Leggibilità
Analizza la leggibilità del testo con Flesch-Kincaid e altre formule.
SMOG grade requires at least 30 sentences.
Domande Frequenti
Implementazione del Codice
import re
import math
def count_syllables(word: str) -> int:
word = word.lower().strip(".,!?;:")
if len(word) <= 3:
return 1
word = re.sub(r'e$', '', word)
vowels = re.findall(r'[aeiouy]+', word)
return max(1, len(vowels))
def readability_scores(text: str) -> dict:
sentences = len(re.findall(r'[.!?]+', text)) or 1
words_list = re.findall(r'\b\w+\b', text)
words = len(words_list) or 1
syllables = sum(count_syllables(w) for w in words_list)
complex_words = sum(1 for w in words_list if count_syllables(w) >= 3)
flesch_ease = 206.835 - 1.015 * (words / sentences) - 84.6 * (syllables / words)
fk_grade = 0.39 * (words / sentences) + 11.8 * (syllables / words) - 15.59
gunning_fog = 0.4 * ((words / sentences) + 100 * (complex_words / words))
return {
"flesch_reading_ease": round(flesch_ease, 1),
"flesch_kincaid_grade": round(fk_grade, 1),
"gunning_fog_index": round(gunning_fog, 1),
"word_count": words,
"sentence_count": sentences,
"syllable_count": syllables,
}
sample = "The quick brown fox jumps over the lazy dog. It was a beautiful day in the park."
print(readability_scores(sample))Comments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.