Text Readability Analyzer
Analyze text readability with Flesch score, average sentence length, and more.
Questions Fréquentes
Implémentation du Code
import re
def text_statistics(text: str) -> dict:
"""Compute common text statistics."""
chars_with_spaces = len(text)
chars_without_spaces = len(text.replace(" ", "").replace("\t", "").replace("\n", ""))
# Words: split on whitespace, discard empty tokens
words = [w for w in re.split(r"\s+", text.strip()) if w]
word_count = len(words)
# Sentences: split on . ! ? followed by whitespace or end of string
sentences = [s for s in re.split(r"[.!?]+(?:\s|$)", text.strip()) if s.strip()]
sentence_count = len(sentences) if sentences else 0
# Paragraphs: blocks separated by one or more blank lines
paragraphs = [p for p in re.split(r"\n{2,}", text.strip()) if p.strip()]
paragraph_count = len(paragraphs) if paragraphs else 0
lines = text.splitlines()
line_count = len(lines)
avg_word_length = (
sum(len(re.sub(r"[^\w]", "", w)) for w in words) / word_count
if word_count else 0
)
# Reading time: ~238 wpm for adults (2023 meta-analysis)
reading_time_sec = round(word_count / 238 * 60)
return {
"chars_with_spaces": chars_with_spaces,
"chars_without_spaces": chars_without_spaces,
"words": word_count,
"sentences": sentence_count,
"paragraphs": paragraph_count,
"lines": line_count,
"avg_word_length": round(avg_word_length, 2),
"reading_time_sec": reading_time_sec,
}
sample = """The quick brown fox jumps over the lazy dog.
Pack my box with five dozen liquor jugs!
How vexingly quick daft zebras jump?
"""
stats = text_statistics(sample)
for key, value in stats.items():
print(f"{key:25}: {value}")Comments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.