🛠️ToolsShed

Haiku Checker

Check if your text follows the traditional haiku 5-7-5 syllable structure with per-line analysis.

About Haiku

A haiku is a form of Japanese poetry with three lines following a 5-7-5 syllable pattern. The syllable counter uses English vowel-group heuristics and may not be 100% accurate for all words.

Sıkça Sorulan Sorular

Kod Uygulaması

import re

def count_syllables(word: str) -> int:
    """Count syllables in an English word using vowel-group heuristic."""
    word = word.lower().strip()
    word = re.sub(r"[^a-z]", "", word)
    if not word:
        return 0
    # Count vowel groups
    count = len(re.findall(r"[aeiouy]+", word))
    # Adjust for silent-e ending
    if word.endswith("e") and count > 1:
        count -= 1
    return max(1, count)

def count_line_syllables(line: str) -> int:
    words = re.findall(r"[a-zA-Z'-]+", line)
    return sum(count_syllables(w) for w in words)

def check_haiku(text: str) -> dict:
    lines = [l.strip() for l in text.strip().split("\n") if l.strip()]
    if len(lines) != 3:
        return {"valid": False, "error": f"Expected 3 lines, got {len(lines)}"}

    counts = [count_line_syllables(line) for line in lines]
    expected = [5, 7, 5]
    valid = counts == expected

    return {
        "valid": valid,
        "lines": [
            {"text": lines[i], "syllables": counts[i], "expected": expected[i]}
            for i in range(3)
        ],
    }

haiku = """
An old silent pond
A frog jumps into the pond
Splash! Silence again
"""

result = check_haiku(haiku)
print(f"Valid haiku: {result['valid']}")
for line in result["lines"]:
    status = "✓" if line["syllables"] == line["expected"] else "✗"
    print(f"  {status} {line['syllables']}/{line['expected']} syllables: {line['text']}")

Comments & Feedback

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