🛠️ToolsShed

Analizzatore Lunghezza Frasi

Analizza la distribuzione della lunghezza delle frasi in qualsiasi testo.

Domande Frequenti

Implementazione del Codice

import re
from collections import Counter

def analyze_sentence_lengths(text: str) -> dict:
    sentences = [s.strip() for s in re.split(r'[.!?]+', text) if s.strip()]
    lengths = [len(re.findall(r'\b\w+\b', s)) for s in sentences]

    if not lengths:
        return {}

    categories = {"short": 0, "medium": 0, "long": 0, "very_long": 0}
    for length in lengths:
        if length <= 10:
            categories["short"] += 1
        elif length <= 20:
            categories["medium"] += 1
        elif length <= 30:
            categories["long"] += 1
        else:
            categories["very_long"] += 1

    return {
        "total_sentences": len(sentences),
        "avg_length": round(sum(lengths) / len(lengths), 1),
        "min_length": min(lengths),
        "max_length": max(lengths),
        "categories": categories,
        "sentence_lengths": list(zip(sentences, lengths)),
    }

text = """
The cat sat. The quick brown fox jumped over the lazy sleeping dog near the river.
This is a medium length sentence with some words.
"""
result = analyze_sentence_lengths(text)
for sentence, length in result["sentence_lengths"]:
    print(f"{length:3d} words: {sentence[:50]}...")

Comments & Feedback

Comments are powered by Giscus. Sign in with GitHub to leave a comment.