This script writes haikus using a Markov Chain language model and a clustering algorithm.
I read an interesting article about someone who wrote a piece of software that would scrape newspapers. The software would look for instances of unintentional haikus. What an interesting idea! I have previously played around with generating sentences from Markov Chains, and thought that generating poems using Markov Chains would be interesting.
From a high level, my software works as follows:
- Take a corpus and create a Markov Chain (I have found that the complete works of Shakespeare work well).
- Take the same corpus, and create a graph.
- Given the sentence "See Spot run" in the corpus, "see", "spot", and "run" would be nodes in the graph with weighted edges between them.
- Generate a line of a certain number of syllables using the Markov Chain.
- Using the graph, find a word that is the most connected to the words in the previous line(s).
- Use the word found in #4 to seed the Markov Chain for generating the next line.
- Repeat until a haiku is created.
Markov Chains are a basic language model that are easy to implement and fun to use. My incorporation of a clustering algorithm serves an important purpose. Generating a poem line by line with a Markov Chain grants no guarantees that the lines will be related in subject matter. The clustering algorithm lets you seed the next line with a word that is closely related to those in the previous line(s). This makes it more likely that there will be a common theme / tone underlying the entire poem.
I concatenated three of the haikus generated by this program together, and submitted the result to a few literature magazines. The experiment was to see if a blind third party thought that any of the poems were good enough to print. The submission was printed in "THE CAL LITERATURE & ARTS MAGAZINE ISSUE XXIX". The accepted poem is below:
Heart obey my blood
Love can trifle with itself
Thee beaten for being
You feel the bruises
Of the noblest note to give
Your worships reason
Distribute exact
Copies of this or perish
Thy nativity