đŸ› ïžToolsShed

Syllable Counter

Count syllables in words and text, analyze readability metrics, and check stress patterns.

HĂ€ufig gestellte Fragen

Code-Implementierung

import re

def count_syllables(word: str) -> int:
    word = word.lower().strip(".,!?;:'"")
    if not word:
        return 0
    # Special case: silent 'e' at end
    if word.endswith('e') and len(word) > 2:
        word = word[:-1]
    # Count vowel groups
    vowels = "aeiouy"
    count = 0
    prev_was_vowel = False
    for char in word:
        is_vowel = char in vowels
        if is_vowel and not prev_was_vowel:
            count += 1
        prev_was_vowel = is_vowel
    return max(1, count)

def count_syllables_in_text(text: str) -> dict:
    words = re.findall(r"[a-zA-Z']+", text)
    total = sum(count_syllables(w) for w in words)
    return {
        "words": len(words),
        "syllables": total,
        "avg_per_word": round(total / len(words), 2) if words else 0
    }

text = "The quick brown fox jumps over the lazy dog"
result = count_syllables_in_text(text)
print(f"Words: {result['words']}")
print(f"Syllables: {result['syllables']}")
print(f"Avg per word: {result['avg_per_word']}")

Comments & Feedback

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