Project 7: Haiku Composer 🌸¶

Difficulty: 🟢 Beginner

Run cells top to bottom. Each step builds on the previous one and saves real files under outputs/haiku_composer/.

← Back to index

Description¶

Randomly compose 5-7-5 syllable haiku poems from themed word banks — a gentle intro to data structures and randomness.

Libraries & Modules¶

  • random — choice, shuffle
  • Built-in: list, f-strings

Python Concepts You'll Practice¶

Lists, random selection, string formatting, loops

🎛️ Parameter Variations¶

  • Edit word banks: NATURE, EMOTIONS, SEASONS
  • HAIKU_COUNT — 1 vs 10 poems
  • SEED — set for reproducible poems (random.seed(42))
In [1]:
# Install dependencies for this project (safe to re-run)
import sys
!{sys.executable} -m pip install pillow numpy matplotlib wordcloud qrcode[pil] python-barcode fpdf2 jinja2 folium plotly pandas scipy ipywidgets -q
Skipping pip install (already installed)
In [2]:
# Interactive Jupyter setup — run this cell first
%matplotlib inline

from pathlib import Path
from IPython.display import display, Image as IPImage, HTML, Markdown, Audio, IFrame

OUTPUT_DIR = Path("outputs") / "haiku_composer"
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
print(f"✅ Outputs folder: {OUTPUT_DIR.resolve()}")
✅ Outputs folder: C:\Users\Hansel Yan\Projects\CodeItAll\outputs\haiku_composer

Step 1 — Word banks & syllable templates¶

In [3]:
import random

# 🎛️ TWEAK THESE — add your own words (syllable count matters!)
NATURE = ["cherry", "river", "moonlight", "frog", "mist", "bamboo", "rain", "stone"]
EMOTIONS = ["quiet", "gentle", "fleeting", "deep", "still", "soft", "ancient"]
SEASONS = ["autumn", "winter", "spring", "summer", "dawn", "dusk", "twilight"]
HAIKU_COUNT = 5
SEED = None  # try 42 for same results every run

if SEED is not None:
    random.seed(SEED)

def pick(pool, n=1):
    return random.choice(pool) if n == 1 else [random.choice(pool) for _ in range(n)]

templates = [
    (5, 7, 5),
]

haikus = []
for _ in range(HAIKU_COUNT):
    lines = []
    for syllables in templates[0]:
        if syllables == 5:
            line = f"{pick(NATURE)} {pick(EMOTIONS)}"
        else:
            line = f"{pick(SEASONS)} {pick(NATURE)} {pick(EMOTIONS)} flows"
        lines.append(line)
    haikus.append("\n".join(lines))

for i, h in enumerate(haikus, 1):
    print(f"--- Haiku {i} ---\n{h}\n")
--- Haiku 1 ---
frog still
autumn river fleeting flows
frog deep

--- Haiku 2 ---
mist deep
winter bamboo soft flows
cherry gentle

--- Haiku 3 ---
frog still
winter stone fleeting flows
frog still

--- Haiku 4 ---
rain still
spring stone fleeting flows
mist still

--- Haiku 5 ---
bamboo gentle
spring stone soft flows
frog deep

Step 2 — Export collection¶

In [4]:
out = OUTPUT_DIR / "haiku_collection.txt"
out.write_text("\n\n".join(haikus), encoding="utf-8")
print(f"Saved {HAIKU_COUNT} haiku to {out}")
Saved 5 haiku to outputs\haiku_composer\haiku_collection.txt

✅ Project complete¶

Check your output folder: outputs/haiku_composer/

Next: Project 8: Spiral Art Generator · Index