Home For Fiction – Blog

for thinking people


January 24, 2022

A JavaScript Haiku Generator with User Input

Programming

haiku, javascript, poetry, programming

My iambic pentameter generator is among the most popular posts on Home for Fiction – and the most popular in the programming category. For a long time I’ve been meaning to make a JavaScript haiku generator as well, but I kept postponing it. Well, no more! In other words…

I thought to offer
a haiku generator
in chilly winter

And since I got into the trouble of doing all that, I thought, what the heck; let’s add user input to the mix. So, let’s see what a JavaScript haiku generator looks like.

JavaScript Haiku Generator
What would a JavaScript haiku generator be without images from Japan!

A JavaScript Haiku Generator: Creative Process

I have this theory in coding, that any program has a central aspect that unlocks the rest. Of course this isn’t as simple in large, complex projects. They involve interconnected layers of hierarchies and functionalities that can all be considered key. Nonetheless, I’d argue they also have an umbrella, core element that can be crucial to many others. It’s just that it’s likely so abstract that it’s hard to notice.

In any case, for a program as simple as this JavaScript haiku generator, the key element was figuring out the syntax of each line, based on the number of syllables. As most of you likely know, a haiku in English consists of three lines, each with 5, 7, and 5 syllables respectively. Feel free to read the relevant Wikipedia article for more on the origins of haiku poetry, as well as attempts to translate them.

The power of haiku poems lies with two elements: simplicity and juxtaposition. Notice how the two are interdependent: Because of its necessarily simple structure, a haiku poem draws its affective power from the often incongruous effect produced by the contrast between the first two lines and the third.

Therefore, pondering on this, you might appreciate why a random JavaScript haiku generator can produce some very intriguing results!

How It Works

The program works so that you can give it up to three nouns. It then attempts to create a haiku using these nouns, plus some context words related to them. You can also leave some (or all) of the input boxes empty, and the app picks nouns randomly. After generating the haiku, you can save the entire composition (the haiku plus the generated background) as a .png image.

As many of my similar programs – such as the iambic pentameter generator I mentioned earlier or the rhyming anapest generator – this JavaScript haiku generator relies on the RiTa library and the Datamuse API to generate the text.

So, what kind of results can we get?

I tried with the keywords “speech”, “computer”, and “line”, and I got this result:

long speech beautifying
never human computer
the line relocates

What if we tried to “help” the program a bit, by supplying keywords open to the contrast I referred to earlier? Let’s see what the program generated using “mountain”, “bird”, and “street”

opposite mountain
cooperating captive bird
the street congregates

The creative options are endless!

Where to Try this JavaScript Haiku Generator

You can try Haiku Generator following this link. For a full list of my available programs, see the relevant page on the main site.