Peter's Haiku Generator: Background Information

The Haiku generating Javascript program works by producing sentences using a predefined, limited vocabulary of up to 400 words, and a set of grammatical parsing rules.

The rules are given below. If you're not familiar with the format, then:

  • A thing between angle brackets is a word or a sequence of words, which I call a phrase.
  • Each rule breaks down a phrase into sub-phrases or single words. Follow the rules through to the end, and a whole sentence is defined as a sequence of single words.
  • A thing between square brackets is optional. So, for example, a "sentence" is either a "single sentence", or a "single sentence" followed by a "conjunction" and another "single sentence."
  • If there are several rules for making a phrase, on separate lines, then you need to choose one of them. For example, a "simple adjective" is either an "adjective" optionally followed by another "adjective", OR it's an "adjective" optionally preceded by an "adverb".
  • Nouns, verbs, adjectives etc are defined separately in the vocabulary database.

The Rules

<sentence> = <single sentence>[<conjunction><single sentence>]

<single sentence> = [<gerund phrase>]<simple sentence>

<simple sentence> = <noun phrase>[<adverb phrase>]<intransitive verb>[<preposition><noun phrase>]
                    <noun phrase><intransitive verb>[<preposition><noun phrase>][<adverb phrase>]
                    [<adverb phrase>]<noun phrase><intransitive verb>[<preposition><noun phrase>]
                    <noun phrase><transitive verb phrase><noun phrase>

<noun phrase> = "The "[<adjective phrase>]<improper noun>
                [<adjective phrase>]<proper noun>
                "A "[<adjective phrase>]<improper singular noun>

<gerund phrase> = [<adverb phrase>]<gerund>

<adverb phrase> = [<adverb>]<adverb>

<transitive verb phrase> = [<adverb phrase>]<transitive verb>

<adjective phrase> = <simple adjective>[" , "<simple adjective>]

<simple adjective> = <adjective>[<adjective>]

Note that terms used in defining the rules are based on normal grammatical terms, but don't necessarily have exactly the conventional meanings.

These rules are the same as those used in my Poetry Engine, which is an early (c1997) Javascript program for generating random poems of varying length. To make the haiku, I get the program to count syllables as it's making up the sentence. There are a few extra rules, so that the generator doesn't start making a simple sentence into a compound one if it's already close to the required syllable count for example. When it's finished, it sees if it has the right number. If it doesn't, it just throws the sentence away and tries again. If it doesn't get it right within ten goes, it gives up and just prints what it has anyway. (That's the reason for the nonsense about constraints on artistic expression - it's a bit of a kludge, but I thought the explanation rather endearing.)

I think the haiku are more successful than the tanka, but it was just a trivial addition to put in the option. The idea for adding the syllabics to the generator came from Jane Dorner, to whom I'm very grateful.

The Vocabularies

The program allows the user to select from a number of vocabularies. Those available at the moment include:

  • Standard - a general vocabulary. Some of the words are taken from translations of Japanese haiku.
  • Erotic - a sexually charged vocabulary. There are no obscene words included. The effect is often more humorous than sexy.
  • Silly - a childish vocabulary.
  • Noir - a vocabulary for producing dark, sometimes violent, urban haiku.
  • Not Silly - another fairly dark vocabulary. So called, because it is largely based on synonyms of words in the Silly vocabulary.
  • Light - a vocabulary for producing gentle, uplifting haiku. Well, that's the idea, anyway.

If you are interested in suggesting a new vocabulary, email me and we can discuss it.

The Javascript

If you wish to examine or make use of the code, please feel free. If you re-use or adapt my code, I would appreciate an acknowledgement. Only click on the following links if you want to load the files directly to examine or download them. You can't run the program by using these links.

Thank you for your interest. You may now return to the haiku generator, or go back to my home page.