🛠️ToolsShed

Alliteration Detector

Detect and highlight alliterative word groups in your text.

常见问题

代码实现

import re

def detect_alliteration(text: str, min_words: int = 2) -> list[dict]:
    """Detect alliterative groups in text."""
    words = re.findall(r"\b[a-zA-Z]+\b", text)
    groups = []
    i = 0
    while i < len(words):
        letter = words[i][0].lower()
        group = [words[i]]
        j = i + 1
        while j < len(words) and words[j][0].lower() == letter:
            group.append(words[j])
            j += 1
        if len(group) >= min_words:
            groups.append({"letter": letter.upper(), "words": group})
        i = j if j > i + 1 else i + 1
    return groups

text = "Peter Piper picked a peck of pickled peppers"
results = detect_alliteration(text)
for g in results:
    print(f"{g['letter']}: {' '.join(g['words'])}")

Comments & Feedback

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