Computer Haiku

How would you write a program

To make them for you


I'd start with some kind of dictionary file that contains a syllable count of each word in it. Then pick words from that add up to the required syllables/line

As to making it poetry, and not just random words, I have no idea.

Ian Jacobs
Poetry vs. random words, who the difference can tell? ;-)
Text-to-speech programs come with pronouncing dictionaries.

You could, in addition to using Ian's idea of syllable counts, also categorize the words by part of speech and generate phrases.

+1  A: 

I would look up syntactical programming and linguistic and try to find libraries for grammatical structure. From there it should be a simple step to add the word count and syllable count constraints.

+6  A: 

Not all haikus have the same number of syllables, but it's a place to start.

In terms of actually picking the words, I think that parts of speech would not be the place where I would start. Instead, I would look at Markov chains, and train your vocabulary on existing haikus.

Craig Stuntz
markov chain on pairs or more of words from haikus and other poetry i think would be pretty damn good!
Actually haikus have the same number of syllables, that's their definition:
Hmm, read this too: "Use of three (or fewer) lines of 17 or fewer syllables"
bah! Contemporary-English haiku? what crap.
+11  A: 

First, you'll want to look into Markov chains, and second, there's a book about computer-generated poetry called Virtual Muse.

Bill the Lizard

From the semantic sude of the story use sampling and fourier transformation. Pick significant parts of some detailed description reduced in single words and leave to a reader to fill in gaps with her own imagination

Boris Pavlović
Please expand on how you can deconstruct a story as a sum of sine or cosine waves of various frequencies.
well, what is the idea of sampling? sampling the signal with a frequency which is at least double of the highest frequency in the range of interest. if you have descreet information extracted like this you can reconstruct the analog input without any loss (in the interested range)
Boris Pavlović
analogous to this pick up significant verbs, nouns and adjectives which are corner stones in some description and leave everything else. compose these words in meaningfull arrays and you'll get haiku
Boris Pavlović
+2  A: 

implement a genetic algorithm to generate haikus drawn from a dictionary annotated with syllable counts, then pay people to read and rate them as the fitness function [mechanical turk would help]. Over time your program should evolve some good ones.


a GA you need
evolves at CPU speed
if fitness you heed

Steven A. Lowe
I'd love to see an implementation of this one. Genetic algorithms intrigue me greatly...
Erik Forbes
+9  A: 
  • count the syllables
  • randomly generate words
  • arrange sensibly
Jason S
It would have been *oh so much better* without your comment. Thats kinda like reminding the audience that "The last sentence, you know, **that** was the punchline. Did you get it?"
ok, fine, i'll delete the comment if you delete yours
Jason S
(FWIW mine was the first smart-ass answer so I was wary of being downvoted)
Jason S
+25  A: 

Measure syllables

Understand semantic flow

Your goal can be met

+10  A: 

Do not attempt it

Poetry does not mix well

With metal and bits

More seriously, good haiku (and even bad haiku) is a lot more about condensing meaning and imagery than counting syllables. It is generally also based on themes gathered from nature. Random word generation and syllable counting will get you measured gibberish, but not poetry...

Jeff Kotula
I don't know. The search for meaning in randomness seems deeply human. What is a poem that had no meaning put into it, but only the meaning taken from it by the reader? Is it a model of secular humanism?
"...syllable counting will get you measured gibberish, but not poetry." ... i thought telling the difference was what "reading poetry" was all about.
+1  A: 

"To convey one's mood in seventeen Syllables is very diffic . . ." (The great John Cooper Clarke Check out Beasley Street, one of my favourites)

How much more diffic for a computer? Logic knows no moods :)

Binary Worrier
It's much easier then - as there would be less variables if there are no moods. You just need to convey indifference every time.
+1  A: 

Some people here suggested using a dictionary and generating word sequences using a Markov Chain. That seems like a theoretically viable solution, especially if you use a high-order Markov Chain (not bi- or trigrams).

But I think in practice it would work better if you could collect a database of existing haikus and selectively change single words in them (e.g., change a given word to another, semantically related word). The existing haikus give you some kind of structure and cohesion, and you just need to (ex-)change little parts in them in order to create a new haiku (a variation on the old one).

Of course they won't be completely new haikus with this method, but at least they will be somewhat enjoyable for the readers.

+2  A: 

To make it readable, separate the dictionary into Nouns, Verbs, Adjectives, with syllable count.

Come up with some templates of the form:

[Noun] [Verb]"s"

[Verb] a(n) [Adjective] [Noun]

[adjective] [noun]

and trim your dictionaries to the beautiful words.


Parse existing haikus in a relational order, like word xx used after yy n times. So when creating, possibility of xx coming after yy will be (n / sum of count of all words used after yy). This way it will be selectively randomized and can still be a valid haiku.

Orkun Balkancı
That is a decent description of the idea behind Markov chains. If word y comes after word x 75% of the time in the source, then y will be chosen 75% of the time in the output.
really? i just made it up.
Orkun Balkancı
It works better with sets of three words, however: what word follows these two words.
+1  A: 

Write your program to generate Haiku's in Japanese. It will be far easier to measure your syllable count, pluse you are staying faithful to the original language of the poetry. If you have flexibility with the project, why not make the original Japanese - then show the English word by word literal translation by its side. It will look mysterious to say the least.

Anyways, just a different take on the problem.


I'd do my homework!

I assure you I am out of school.
Writing a Haiku-writing application as homework? I'd *like* to go to that school!
Joachim Sauer
Nearly year it took, to compose that comment, I'd vote it up!
+2  A: 

On Haiku Village, we have the technology to easily do this in a variety of ways. One idea is to simply read the global twitter feed, and detect unintentional haikus. Since the back-end also has a dictionary, it would be possible to produce questionable haikus, but I think the quality would be lacking.

I think if we had a star rating system, then I suppose machine learning could be used to decide what is 'good'.

+1  A: 

Your program must grok
Metaphores and imagery
And be creative.

Adam Jaskiewicz
+2  A: 

Haikus are easy, that I'll note
Solutions well documented, and functions rote
They're overdone and cheesy
Coding far too easy
Code me a limerick, then I'll vote

//I actually like haikus

Mike Robinson
+1 for the best poetry in the world.

I love this question. It is very imaginative. Answer Below.

Many people have suggested Markov chains, but I really don't think it would be possible. You need to know intelligently whether the syllable is a PHONEMES then you have to know where the syllable ends.

If you ever did this I would be amazed.

At some point you may wish to look up what the words you use actually mean in English.
+1  A: 
for (i is 0
and i is less than thirteen)
echo syllable

I'd start by ignoring all the responders who think the plural form of haiku is 'haikus'.


The algorithm for having a computer output high quality haiku works something like this:

Setup Phase

  find the email address of a world-renowned writer of haiku
  confirm that this person is willing to generate haiku on demand
until sucker^H^H^H^H^H^Hwriter is found

Execution Phase

  wait for a haiku request
  when a haiku request is received, email the previously-stored master and ask for a haiku
  wait for the haiku to return by reply
  output haiku

There are, of course, various enhancements which can be made upon this fundamental architecture. For example the setup phase can be extended to set up a pool of haiku experts. The execution phase can be used to generate haiku during idle times and cache them against future demand. The specifics of such tweaking are left as an exercise for the student.


Markov Sequences
A syllabic Database
Three lines of python