Python Haiku Generator

In the same vain as my poem generator, I have coded a haiku generator in Python. Initially I wanted to write a haiku generator before resorting to a poem generator, but I could not find a source for the number of syllables for a dictionary of words.

Well, that problem is solved — Carnagie Mellon offers a pronunciation dictionary, for their speech-to-text engine, Sphinx. The dictionary is open-source, so I am using it as a source for words and syllables for the haiku generator.

Haikus are traditionally three lines, with a 5-7-5 split, or five syllables for the first line, seven syllables for the second, and again five syllables for the final line. If your middle school days of English class are beyond your recollection, it looks something like this:

An old silent pond…

A frog jumps into the pond,

splash! Silence again.

The CMU dictionary is simply a text file with each word split into the following format:

AGENCY  EY1 JH AH0 N S IY0

The line starts with the word, then two whitespaces and the pronunciation with numbers indicating the syllable breaks.

I wrote a function to break these lines down into a word and a syllable count, which is then stored in a Python dictionary. The dictionary is set up with the syllable count as the key, and the list of words with associated syllable count as the values.

The final part the code then builds lines depending on the amount of syllables desired, which is then constructed in a final function in a haiku format. Here is a sample of outputs:

MUSTAFA WILSON’S

WONDERFULNESS JAWED BARBY

WORLDERS BAS-RELIEF


BATTERERS FOOTLIGHTS

STRIKE KONARSKI HARDYMON

CHOO STRANDS SOLIDLY


SPATH RASH SPIKE MUCUS

UNDERLINING SCREWDRIVERS

STOUT SEALED VOS HANZLIK


Check out the code here.

Comments