Haiku Checker
检查文本是否遵循传统5-7-5音节的俳句结构,并进行逐行分析。
关于俳句
俳句是一种日本诗歌形式,有三行遵循 5-7-5 音节模式。音节计数器使用英语元音组启发式算法,可能不会对所有单词都 100% 准确。
关于此工具
俳句检查工具是一个验证你的文本是否遵循传统5-7-5音数结构的实用工具。俳句是一种源于日本的古老诗歌形式,以其简洁和优雅著称,音数计数是这种形式的基本要素。该工具独立分析每一行,计算音数并提供详细反馈,让你能够准确看到你的创作与经典格式的契合程度。
要使用俳句检查工具,只需将你的三行诗粘贴到输入框中,然后点击分析按钮。工具将显示每一行的音数,并指示它们是否符合5-7-5的模式。这对于学习俳句创作的作家、在文学课中研究日本诗歌的学生,或任何想在与他人分享前完善作品的人都特别有用。
常见问题
代码实现
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.