File size: 604 Bytes
61848b4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 | """Tool: text_stats — Compute statistics about text."""
import re
from collections import Counter
def text_stats(text: str):
"""Compute word count, char count, sentence count, avg word length, top words."""
words = re.findall(r"\b\w+\b", text.lower())
sentences = [s for s in re.split(r"[.!?]+", text) if s.strip()]
word_lens = [len(w) for w in words]
return {"word_count": len(words), "char_count": len(text),
"sentence_count": len(sentences),
"avg_word_length": sum(word_lens)/max(1,len(word_lens)),
"top_words": Counter(words).most_common(5)}
|