How would you write a program to generate Haiku?

Algorithm

Algorithm Problem Overview


Computer Haiku
How would you write a program
To make them for you

Algorithm Solutions


Solution 1 - Algorithm

Measure syllables
Understand semantic flow
Your goal can be met

Solution 2 - Algorithm

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...

Solution 3 - Algorithm

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

Solution 4 - Algorithm

  • count the syllables
  • randomly generate words
  • arrange sensibly

Solution 5 - Algorithm

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

Solution 6 - Algorithm

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.

Solution 7 - Algorithm

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'.

Solution 8 - Algorithm

for (i is 0
and i is less than thirteen)
print s i plus plus

Solution 9 - Algorithm

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.

EDIT:

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

Solution 10 - Algorithm

"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 :)

Solution 11 - Algorithm

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.

Solution 12 - Algorithm

Your program must grok
Metaphores and imagery
And be creative.

Solution 13 - Algorithm

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.

Solution 14 - Algorithm

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.

Solution 15 - Algorithm

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.

Solution 16 - Algorithm

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.

Solution 17 - Algorithm

Markov Sequences
A syllabic Database
Three lines of python

Solution 18 - Algorithm

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.

Solution 19 - Algorithm

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

Solution 20 - Algorithm

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

Solution 21 - Algorithm

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

Setup Phase

loop:
  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

loop:
  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.

Solution 22 - Algorithm

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjoeforkerView Question on Stackoverflow
Solution 1 - AlgorithmpatrosView Answer on Stackoverflow
Solution 2 - AlgorithmJeff KotulaView Answer on Stackoverflow
Solution 3 - AlgorithmBill the LizardView Answer on Stackoverflow
Solution 4 - AlgorithmJason SView Answer on Stackoverflow
Solution 5 - AlgorithmMike RobinsonView Answer on Stackoverflow
Solution 6 - AlgorithmCraig StuntzView Answer on Stackoverflow
Solution 7 - AlgorithmMatthew HigginsView Answer on Stackoverflow
Solution 8 - AlgorithmFentonView Answer on Stackoverflow
Solution 9 - AlgorithmSteven A. LoweView Answer on Stackoverflow
Solution 10 - AlgorithmBinary WorrierView Answer on Stackoverflow
Solution 11 - AlgorithmnailitdownView Answer on Stackoverflow
Solution 12 - AlgorithmAdam JaskiewiczView Answer on Stackoverflow
Solution 13 - AlgorithmmvrakView Answer on Stackoverflow
Solution 14 - AlgorithmFrankView Answer on Stackoverflow
Solution 15 - AlgorithmOrkun BalkancıView Answer on Stackoverflow
Solution 16 - AlgorithmElijahView Answer on Stackoverflow
Solution 17 - AlgorithmSingleNegationEliminationView Answer on Stackoverflow
Solution 18 - AlgorithmIan JacobsView Answer on Stackoverflow
Solution 19 - AlgorithmBrianView Answer on Stackoverflow
Solution 20 - AlgorithmBoris PavlovićView Answer on Stackoverflow
Solution 21 - AlgorithmJUST MY correct OPINIONView Answer on Stackoverflow
Solution 22 - AlgorithmLaykeView Answer on Stackoverflow