| """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)} | |