Haiku Generation

An exploration of automated poetry

Category: Haiku

Example Haiku Generator 4- Parts of Speech

A feature the RiTa offers that we have not taken advantage of yet is that the words in its dictionary are not only tagged with pronunciation data (which is how we tell how many syllables are in a word), but they are also tagged with the part of speech for the word- or rather, the parts, since a word can be used in multiple ways. This allows us to make slightly more nuanced distinctions when we ask for random words.

We'll make a slight change to our code, and allow our random word function to request words of a given part of speech. This is a very minor code change- it just means adding another parameter to the syllableCount function that specifies the Part of Speech (pos). Here is the revised function:

String syllableCount(String pos, int n) {
return lex.randomWord(pos, n);

You can replace the existing function in your code with this one, or you can add it. Using two functions with the same name and different parameters is called "overloading", and is commonly done to make programs conceptually simpler to use. The program is able to determine which one to use by the parameters you send it. They COULD do something completely different, but it is good practice for them to be as similar as possible.

While the code may be only slightly changed, the grammar is not. In order to take advantage of this new capability, we'll need to add a great deal to the grammar definition. We can no longer use the simple rule <1> to get a random one syllable word- we have to tell it what KIND of one syllable word. This has added a lot of definitions to our grammar. The grammar below looks complicated, but if you examine it, you'll see that it follows the same basic structure as the previous examples, but has a lot more of the definitions specified. In that way, it is kind of a blend between the first haiku generator, where each word was literally spelled out, and the second, where we only defined the word by its syllable count.


"<start>": "<5-line> % <7-line> % <5-line>",

"<5-line>": "<one-noun> <four-verb> |<wh-pronoun> <three-verb> <one-adverb> |<one-pronoun> <three-verb> <one-adverb> |<one-pronoun> <three-verb2> <one-adverb> |<one-determine> <one-adject> <three-plu-noun> | <one-determine> <one-adject> <three-noun> |<one-noun> <two-verb> <two-noun> |<one-noun> <two-verb> <two-plu-noun> |<wh-pronoun> <two-verb> <one-noun>, <one-interject> |<one-determine> <one-list> <two-adject> <one-noun>",

"<7-line>": "<one-interject> <one-interject> <5-line> |<two-adject> <5-line> |<5-line> <one-noun> - <one-super> |<5-line> <two-super>",

"<one-super>": "`syllableCount(\"jjs\",1);`",

"<two-super>": "`syllableCount(\"jjs\",2);`",

"<one-list>": "`syllableCount(\"ls\",1);`",

"<one-interject>": "`syllableCount(\"uh\",1);`",

"<one-adject>": "`syllableCount(\"jj\",1);`",

"<two-adject>": "`syllableCount(\"jj\",2);`",

"<one-determine>": "`syllableCount(\"dt\",1);`",

"<one-prop-noun>": "`syllableCount(\"nnp\",1);`",

"<one-noun>": "`syllableCount(\"nn\",1);`",

"<two-noun>": "`syllableCount(\"nn\",2);`",

"<two-plu-noun>": "`syllableCount(\"nns\",2);`",

"<three-noun>": "`syllableCount(\"nn\",3);`",

"<three-plu-noun>": "`syllableCount(\"nns\",3);`",

"<one-adverb>": "`syllableCount(\"rb\",1);`",

"<one-pronoun>": "`syllableCount(\"prp\",1);`",

"<wh-pronoun>": "`syllableCount(\"wp\",1);`",

"<two-verb>": "`syllableCount(\"vbz\",2);`",

"<three-verb>": "`syllableCount(\"vbz\",3);`",

"<three-verb2>": "`syllableCount(\"vbp\",3);`",

"<four-verb>": "`syllableCount(\"vbz\",4);`"


There is a lot of room for improvement here- the number of different haikus generated by this version is far fewer than the others, because the grammar is much shorter. As an exercise, why don't you add some more rule definitions to it? You can either flesh out the higher rules to make more combinations, or you could add more terminal productions to choose different words. Or both.

Here is the list of the part-of-speech tags you can use:

Coordinating conjunction

Cardinal number


Existential there

Foreign word

Preposition or subordinating conjunction


Adjective, comparative

Adjective, superlative

List item marker


Noun, singular or mass

Noun, plural

Proper noun, singular

Proper noun, plural


Possessive ending

Personal pronoun

Possessive pronoun


Adverb, comparative

Adverb, superlative





Verb, base form

Verb, past tense

Verb, gerund or present participle

Verb, past participle

Verb, non-3rd person singular present

Verb, 3rd person singular present



Possessive wh-pronoun


Why aren’t these haikus any good?

We have three haiku generators running now, and it would be generous at best to say that the output they produce is… sometimes evocative. Every so often, the haiku generator will create a combination of words that may inspire a creative combination of thoughts and images.

I find this happens more often with the the “random” version, as I find the static grammar to be too predictable to come up with anything that is truly surprising. The random haikus sometimes create a line or two that is evocative enough to be worth keeping around, but for the most part it just generates nonsense.

I think this shows that merely following the syllable conventions of the haiku form is not enough to create anything of any merit. The current versions will jumble together words without regard to what they mean, or even how they should function. If you generate enough of the random haikus, you’ll see strange elements intrude on occasion- words that are exclamations, or even just single letters. Certainly, the random words don’t work to create any kind of greater whole.

What would be good would be to find a way to inject some syntactical rules into the generation process. We’ve been treating each word as an isolated event, unrelated to the work as a whole except for its syllable count, and completely unrelated to the other words in the haiku.

Let’s upgrade our grammar to try to capture some simple interactions between words to create simple phrases. We’ll keep the random word mechanism, but try to generate random words of specific kinds and types.

Example Haiku Generator 3 – Refrigerator

There is a... school of thought... that haikus can be improved by adding the word "refrigerator". The most famous example of this is this haiku by an unknown author:

Haikus are easy,
But sometimes they don't make sense.

Notice that the haiku adheres strictly to the definition we are using- it has a 5-7-5 syllable pattern, but doesn't have any of the semantic criteria such as a seasonal reference or any particular devotion to visual imagery. It is possible that the startlingly dissonant last line serves as the pause or the "cutting word".

Sadly, it seems that the addition of the word "refrigerator" does not really add to the quality of the haikus we are generating. The true effectiveness comes from the unexpected appearance of the random word, and highlights the truthful irony of the statement that "sometimes they don't make sense." In our case, all of the words are random, so the impact is completely lost. The haikus generated by our random method are sometimes evocative, but they don't have the impact that one with meaning does.

The code to create this variation is identical to the code for the "random word" version- there is no need for any changes there. The grammar is nearly identical as well. In fact- there was only a single change to the grammar. As an exercise, can you determine what the change was? Can you replicate it with your own new final line?

Example Haiku Generator 2- Random Words

If you click on the haiku generator in the previous example a few times, it won't take long before you start noticing how small the vocabulary really is, especially with the single word five syllable lines like "transformational". After about 15 or 20 haikus, you'll see another "transformational".

How many possible haikus can it generate? Quite a lot, though they will only differ from each other in small amounts. Let's run the math on a rough estimate of the potential upper limit of unique haikus.

Let's ignore duplicate words for a moment, and say that "wave wave wave" is as unique as "pale grey bowl."
There are 31 one syllable words, 22 two syllable words, 11 three syllable words, 6 four syllable words, and 6 five syllable words.
There are (31*6)+(31*11*31)+(31*31*11)+(31*22*22)+(31*22*31*31)+(31*31*22*31)+(31*31*31*22)+(31*31*31*31*31)+(22*11)+(22*22*31)+(22*31*22)+(22*31*31*31)+(11*22)+(11*31*31)+(6*31)+(6) = 31,328,346 ways to arrange those words to make a five syllable line, and a further (31*31*31328346)+(22*31328346)+(31328346*31*31)+(31328346*22) = 61,591,528,236 to make the seven syllable line. Therefore, there are about 31,328,346 + 61,591,528,236 + 31,328,346 = 61,654,184,928 potentially unique haikus.

Sixty billion is a lot, but even so, it seems like it repeats itself. The human brain is very very good at noticing patterns, and it doesn't take much for us to pick up on repetitions like the kind that our haiku generator is making. The odds are great that each individual haiku is unique, but as you generate more and more haikus, the more alike they will seem. Increasing the number of words in our vocabulary will make the variability from one haiku to the next much greater.

Let's use randomly chosen words instead of a small dictionary or vocabulary. Well, it will still be picking words from a known list, but at least a) it's a much bigger list, and b) we don't have to type it in. We'll use the dictionary built into the RiTa software library, which has about 40 thousand words in it.

We just need to make a few small changes to our grammar and to our code.

"<start>": "<5-line> % <7-line> % <5-line>,

"<5-line>": "<1> <4>|<1> <3> <1>|<1> <1> <3>|<1> <2> <2>|<1> <2> <1> <1>|<1> <1> <2> <1>|<1> <1> <1> <2>|<1> <1> <1> <1> <1>|<2> <3>|<2> <2> <1>|<2> <1> <2>|<2> <1> <1> <1>|<3> <2>|<3> <1> <1>|<4> <1>|<5>",

"<7-line>": "<1> <1> <5-line>|<2> <5-line>|<5-line> <1> <1>|<5-line> <2>",

"<1>": "`syllableCount(1);`",

"<2>": "`syllableCount(2);`",

"<3>": "`syllableCount(3);`",

"<4>": "`syllableCount(4);`",

"<5>": "`syllableCount(5);`"

We changed the grammar so it doesn't have a list of words in it at all any more. Instead, when we get to the point of generating our terminal results, we use what is known as a "callback". A callback is a way for our grammar to incorporate code in its rules. We are using a function call named "syllableCount(number)". As you might suspect, the number represents the number of syllables in a word we want. In order to use this new function, we have to define it in our code.

/* callback */
String syllableCount(int n) {
RiLexicon lex;
return lex.randomWord(n);

All of our code is the same as the previous iteration, but we add the above function definition below our previous code. It uses the RiTa Lexicon to generate a random word of the appropriate syllable length to use as the terminal element in our grammar generation.

I won't calculate the number of possibilities for this haiku generator, because I don't know how many 1 syllable, 2 syllable, Etc words are in the lexicon.But I will say that the haikus will be a LOT more varied than before!

Example Haiku Generator 1 part 2

As promised, here is the code that will allow you to generate haikus on your own. This is nearly identical to, and is based off of the code that Daniel Howe, the creator of RiTa posted on his site. My work is very derivative of his and could not exist without it.

import rita.*;
RiGrammar grammar;
RiText[] rts = new RiText[3];

void setup()
size(600, 200);

RiText.defaults.alignment = CENTER;

rts[0] = new RiText(this, "To make a haiku", width / 2, 75);
rts[1] = new RiText(this, "You must click this window first", width / 2, 110);
rts[2] = new RiText(this, "and one will appear", width / 2, 145);

grammar = new RiGrammar(this);


void draw()
background(230, 240, 255);
for (int k = 0; k < rts.length; k++)

void mouseClicked()
String result = grammar.expand();
String[] lines = result.split("%");
for (int i = 0; i < rts.length; i++)

Let's go through this slowly. I assume that you have no experience in Java like languages but that you are familiar with programming style syntax.

import rita.*;

This grants us access to the RiTa toolkit, which is what (like the flux capacitor) is what makes this possible.

RiGrammar grammar;
RiText[] rts = new RiText[3];

Here we declare variables and objects for use later. Both RiGrammar and RiText are parts of the RiTa library.

void setup()
size(600, 200);

RiText.defaults.alignment = CENTER;

rts[0] = new RiText(this, "To make a haiku", width / 2, 75);
rts[1] = new RiText(this, "You must click this window first", width / 2, 110);
rts[2] = new RiText(this, "and one will appear", width / 2, 145);

grammar = new RiGrammar(this);


The "setup" function establishes the foundation for the program, and is a key element in most Processing programs. It runs in the beginning of the program to "set things up". In this case, it establishes the size of the window that the code will run in, and how to arrange the text. Next, it makes three lines of text appear on the screen, giving instructions to the user. We'll be using the RiText "rts" array again later on to display the haikus.
Finally, we load the grammar that we saw in the previous blog post into the program. When you download the code to run your self, you'll need to edit this to your individual system.

void draw()
background(230, 240, 255);
for (int k = 0; k < rts.length; k++)

The "draw" function is also an important part of Processing programs, but ours is very simple. It loops through each element of the "rts" array, and draws it to the screen. The first time, it draws the introductory text defined in the setup function. Later, it will draw the text that the grammar chooses. It loops continuously until the program is finished, so as things change, it will always draw the most recent elements.

void mouseClicked()
String result = grammar.expand();
String[] lines = result.split("%");
for (int i = 0; i < rts.length; i++)

Finally, the "mouseClicked" function governs what happens every time the mouse button is clicked. Here, it steps through the grammar rules (expands them) and the result is a long string of text. We then do some minor formatting to split the text into a more familiar haiku format, and we store the lines in each of the "rts" array elements. This makes it available to the "draw" function, who then draws the new haiku to the window/screen.

We'll reuse a lot of this code in later projects so future discussions will be shorter. Try copy-pasting the code into a new Processing project, and using it with the grammar you edited from the previous blog. If you want to personalize it, try:

  • changing the size of the window
  • editing the instructions that setup writes
  • changing the background color of the window

Example Haiku generator 1 part 1

Here is the first iteration of the Haiku generator. We'll discuss it in two parts; the grammar that governs the haiku creation, and the code that enables the grammar. Since we just discussed context free grammars, we'll look at it first.

"<start>": "<5-line> % <7-line> % <5-line>,

"<5-line>": "<1> <4>|<1> <3> <1>|<1> <1> <3>|<1> <2> <2>|<1> <2> <1> <1>|<1> <1> <2> <1>|<1> <1> <1> <2>|<1> <1> <1> <1> <1>|<2> <3>|<2> <2> <1>|<2> <1> <2>|<2> <1> <1> <1>|<3> <2>|<3> <1> <1>|<4> <1>|<5>",

"<7-line>": "<1> <1> <5-line>|<2> <5-line>|<5-line> <1> <1>|<5-line> <2>",

"<1>": "teach |pale |grey||flow |bowl |home |thine |flag |star |rocks |light |dusk |day |dawn |light |robs |ant |twig |mine |shell |pen |book |rust |wave |sea |through |sound |mind |smoke |snow |fish ",

"<2>": "drifting |purple |mountains |skyline |city |faces |toward |towards |dragon |bases |table |human |ocean |thinking |zooming |brushing |under |driveway |pushing |failing |sparkles |willow ",

"<3>": "sunrises |classical |people farms |memories |programming |spaceship earth|far away |apprentice |adventure |peasant hut |heavy rain ",

"<4>": "going homeward |lasagna pan |the edge of town |empty bookshelf |dinner table |unix laptop ",

"<5>": "transformational |instantaneous |strangers on a train |towards the outside |out in the mountains |in the dark of night"

The only real differences between this and the example we used in the previous post is that this grammar is more complete, in that all of the combinations and potential syllable orders are explicitly laid out. Additionally, you'll note that some liberties were taken with the idea of a four or five syllable "word".

Click on the generator several times, and you'll see that each haiku is unique. The odds of getting the same haiku with the same words in the same order is really quite small. However, if you click several more times, you'll see that the vocabulary is really quite small. Compared to the vastness of the English language, the approximately 100 words our haikus will be made up of is really quite small.

Of course, the biggest problem is that our haikus are mostly terrible! The grammar picks words at random, so the only phrases in any kind of order are the ones we put in for the five syllable words. And again, since they are picked at random, sometimes we'll see the same words twice in a row in awkward fashion. And a lot of the words don't really work too well with each other. I imagine that most human authors won't use "peasant hut" and "unix laptop" together in a haiku very often.

For a small project, why don't you try downloading the grammar and editing it? Try removing some of the more awkward phrases, and inserting others that you would prefer. In the next section, we'll go over the code so you can run it yourself.

Context Free Grammar

One of the chief tools we will use to make our haikus is a “grammar.” We’ll define what a haiku is a high level in the context of what it is made up of. Then, we will define those smaller pieces, and repeat the process until we get down to a piece small enough to deal with on its own- typically this will be a word in this case.

In the previous post about haiku form, we saw that there was a great deal of nuance to each poem, with references to seasons and a stylistic break between two images. Our haikus won’t be that complex- for our purposes, we’ll only care about getting the right number of syllables in the right order.

  • What is a haiku?
    • A Haiku is a three line poem.
  • What is the first line like?
    • The first line has five syllables.
  • What is the second line like?
    • The second line has seven syllables.
  • What is the third line like?
    • The third line also has five syllables.

We could formally write this in a style like this:

Haiku : five-syllable-line seven-syllable-line five-syllable-line

We can use the definition for the first and third line again because they are the same thing, so why not? In this format, the process is that you can replace items on the left side of the colon (Haiku) with the items on the right side of the colon (five-syllable-line seven-syllable-line five-syllable-line). It is not very interesting now, but we’ll add some more rules for replacement now.

  • What is a five syllable line?
    • A five syllable line has words that add up to a total of five syllables.
  • What is a seven syllable line?
    • A seven syllable line has words that add up to a total of seven syllables.

So just as a haiku is made of of lines, a line is made up of words. (Or possibly just one long word.) Since “syllable” takes up a lot of space and it keeps showing up, in the definition below I am just going to abbreviate it as “S”. Let’s write out a formal description of five-syllable-line below:

five-syllable-line : 5-S-word | 4-S-word 1-S-word | 3-S-word 2-S-word | 3-S-word 1-S-word 1-S-word …

There are only a finite number of ways to arrange the words in a haiku first line. The easiest case is that you have a single word that is five syllables long (Inconceivable is a 5-S-word). You could have a word that is four syllables long (exterior) followed by a word one syllable long (door). A three syllable word followed by a two syllable word, or perhaps by two one syllable words. And of course, you can rearrange the order, so that the single syllable word comes before the four syllable word. We use the vertical bar “|” to separate the various possible combinations. The full grammar will list all of the possibilities, and the different orders they can be in. “4-S-word 1-S-word” is not the same thing as “1-S-word 4-S-word”, after all.
The rule is that once again, you replace the item on the left with the item on the right, but you can only pick one item separated by the bar. You can’t replace “five-syllable-line” with “4-S-word 1-S-word 3-S-word 2-S-word”- that would end up being ten syllables. There ends up being 16 unique combinations.

Since we did all that work defining a five-syllable-line, it would be a shame to only use it once. Fortunately we can use it again when we define a seven-syllable-line. It is just like a five-syllable-line, but with two more syllables.

seven-syllable-line : five-syllable-line 2-S-word | five-syllable-line 1-S-word 1-S-word | 2-S-word five-syllable-line | 1-S-word five-syllable-line 1-S-word | 1-S-word 1-S-word five-syllable-line

By using the five-syllable line definition, we were able to cut down the number of rules to only five.  This shows the real power of creating a grammar like this- once a rule has been created, you can use it in other rules. You can create some pretty complex definitions by creating deep layers of rules.
But if you actually want to get things done, sooner or later you have to have rules that are the base of the pyramid or the end of the line. They are called “terminal productions” because they don’t substitute in any other rules, but give you the item were describing. Our terminal rules will give us words.

5-S-word : “inconceivable” | “animosity” | “reciprocity”

4-S-word : “beautifully” | “necessary” | “anarchist”

3-S-word : “compactor” |  “American” | “protocol”

2-S-word : “button” | “running” | “deadly” | “quickly”

1-S-word : “push” | “pull” | “the” | “please” | “and”

 Like before, you can substitute an item on the left for one of the items in the list on the right, but once you have done that, that rule chain has finished. As you go through the rules, you will end up creating a haiku that meets the specifications we outlined.

Step 1

Haiku : five-syllable-line seven-syllable-line five-syllable-line

Step 2

Haiku : 1-S-word 1-S-word 3-S-word seven-syllable-line five-syllable-line

Step 3

Haiku : 1-S-word 1-S-word 3-S-word seven-syllable-line five-syllable-line

Step 4

Haiku : 1-S-word 1-S-word 3-S-word 2-S-word 4-S-word 1-S-word five-syllable-line

Step 5

Haiku : 1-S-word 1-S-word 3-S-word 2-S-word 4-S-word 1-S-word 2-S-word 3-S-word

Step 6

Haiku : push 1-S-word 3-S-word 2-S-word 4-S-word 1-S-word 2-S-word 3-S-word

Step 7

Haiku : "push" "the" 3-S-word 2-S-word 4-S-word 1-S-word 2-S-word 3-S-word

Step 8

Haiku : "push" "the" "compactor" 2-S-word 4-S-word 1-S-word 2-S-word 3-S-word

Step 9

Haiku : "push" "the" "compactor" "button" 4-S-word 1-S-word 2-S-word 3-S-word

Step 10

Haiku : "push" "the" "compactor" "button" "beautifully" 1-S-word 2-S-word 3-S-word

Step 11

Haiku : "push" "the" "compactor" "button" "beautifully" "please" 2-S-word 3-S-word

Step 12

Haiku : "push" "the" "compactor" "button" "beautifully" "please" "deadly" 3-S-word

Step 13

Haiku : "push" "the" "compactor" "button" "beautifully" "please" "deadly" "protocol"

And we finish! All of the rules have been replaced by terminal productions, so there is nothing further to do. Let’s format it into something a little more traditional looking.

Push the compactor
button beautifully, please-
deadly protocol

I doubt it will win any awards at the haiku competitions that are held each year, but Star Wars fans might like it.

Rita and Processing

All of these exercises use a programming environment called “Processing”, which is a programming environment that is designed to promote software literacy. It is syntactically identical to Java, but provides an interface that many non programmers find easier to approach. Processing provides a number of libraries of code frameworks that make it easy to begin projects that would otherwise be unattainable.

One of these libraries is named RiTa, which provides a number of tools for natural language, with a focus on “generative literature.” It is designed to integrate with Processing with minimal work, and can be used directly in Java with only a little more effort.

All of the examples on this blog use both Processing and RiTa, so if you want to follow along and make your own versions, you will need them too.

To get Processing:
1) Go to the Processing website and download the most recent version of Processing
http://www.processing.org/download/. The site will ask you to make a donation, but that is not required and you can download it for free.
2) Follow the instructions at http://www.processing.org/learning/gettingstarted/ in order to make sure that you can run the tool. That initial tutorial will also give you an overview in running Processing.

Once you have Processing functioning, you can install the RiTa libraries:
1) Go to the RiTa website and download the file that is available.
2) Follow the instructions on the site to make the libraires available-
“To install RiTa in Processing, unzip RiTa.zip and place the ‘rita’ folder inside the “libraries” folder of your sketchbook. If your not sure where your sketchbook is, open the Preferences window in Processing and look for the “Sketchbook location” item in the menus.”
3) Run Processing, and then “Select “Add Library…” from the “Import Library…” submenu within the Sketch menu.”

Hopefully optional step 4) If the above does not work with your version of Processsing, you may need to follow the more detailed instructions at http://wiki.processing.org/w/How_to_Install_a_Contributed_Library

About Haiku

Haikus are a Japanese form of poetry that is known for evoking emotional images of nature, in a pithy, succinct format. When written in English, Haikus are limited to 17 syllables across three lines, in a 5-7-5 manner.

Haikus are often written by American schoolchildren as an introduction to poetry because the syllable counting provides an easy way to introduce creative constraints, and the form is very forgiving otherwise. Some people think that even the adherence to syllable counting is unnecessary, and that the true essence of the poem should be in the images it conveys.

In the classic format, the haiku should include a reference to a specific season, or to a natural scene. A common technique is to “juxtapose” two images, with a pause or “cutting word” between them.

A variant of the haiku format is called the “Zappai”. It is considered the doggerel of Haiku, not meeting the more refined rules and omitting thematic constraints such as the pause or the seasonal reference.

This project calls the creations it produces “haikus”, but they are far from it. At their best, they would be Zappai, and at their worst… they are not even nonsense.

Here are some links to learn more about haikus, specifically the American form.

The Haiku Society of America

In The Moonlight a Worm…

Haiku North America


This site is an exploration of automated poetry creation, focusing on haiku- or rather, haiku-like structures. It is intended for people who are interested in creating their own projects to use as a model to begin their own explorations.

It is structured as a gentle introduction for readers who are familiar with the idea of programming, but may not have any direct experience with it themselves. The examples are simple, and each one builds from the previous. The articles include a small exercise for the reader that will lead them to greater understanding of the process.

Even if you do not intend to create any of your own haikus by modifying the examples, here, you may enjoy some of the haikus that were created. The vast majority of them are not very good, but some of them contain the seeds of an evocative phrase that may be inspiring to a writer.

Each example includes a working version at the top of the page that demonstrates the type of haiku that is generated by that code and data.  Clicking on the box that contains the haiku will generate another one. It will erase the one that is present, so enjoy it while you can! These should be ephemeral, after all, and the odds are very slim that you will ever see a repeat.