The whacky art of Lullian poetry

The article describes a Lullian poetry generator in PHP, the idea for which was inspired from Clifford Rickover’s wonderful and eclectic, The Loom of God. The idea is attributed to Ramon Lull, a Spanish theologian, who lived around the 13th century. Lull believed that every branch of knowledge was governed by simple basic principles, and that we could explore all knowledge by evaluating all possible combinations of those principles.

The design for the Lullian poetry sprouts from this premise. The Lullian generator works by randomly selecting words and phrases which are then placed in a pre-defined ‘Semantic Schema’. The philosophical idea being that randomly chosen words can be used to stimulate the mind in producing startling fictional plots and verbal imagery that you might not otherwise consider.

Take the following example short poems generated by the program. Although the resulting poems are nothing like the ones you would ordinarily encounter, the real facet of the poems is their whimsical nature of word combinations, sometimes ridiculously so.

A delightful stranger
A delightful stranger misgiving while riding the fantastic flower.
calmly , the stranger withholding .
The flower thrusting while weeping a eager pleasure.

A fine beast
A fine beast shriving while eating the gentle ocean.
always, the beast melting .
The ocean casting while shooting a fantastic weather.

A agreeable cat
A agreeable cat learning while riding the elated pest.
enormously, the cat springing.
The pest outshining while hanging a calm arm.

The ‘Semantic Schema’ used to generate the above poems is shown below.

A (adjective1) (noun1)
A (adjective1) (noun1) (verb) (phrase) the (adjective) (noun2).
(adverb), the (noun1) (verb).
The (noun2) (verb) (phrase) a (adjective) (noun3).

Now all you need to do is to randomly select a word from a word class (verb, adjective, noun etc.), and replace it with its corresponding placeholder in the schema. The PHP code given below does this automatically for you. Of course you can change the schema to get different types of poem structure.

  * Function to replace every occurrence of a English
  * word class with a random selected word from an array.
function substitute($match)
    global $adjective, $noun, $verb, $adverb, $phrase;
    global $stack;
    $match[0] = preg_replace('/[\(\)]/', "", $match[0]);
    $mytype = $match[0];

        $base = substr($match[0],0, strlen($match[0])-1);

        if(array_key_exists($match[0], $stack)) {
            return $stack[$match[0]];
        } else {
            $randomOffset = rand(0, count(${$base}) - 1);
            $stack[$match[0]] = ${$base}[$randomOffset];
            return $stack[$match[0]];

    $randomOffset = rand(0, count(${$mytype}) - 1);
    return ${$mytype}[$randomOffset];

/* Start */
$stack = array();
$data = array("noun", "verb", "adjective", "adverb", "phrase");

/* Read all the words from the files into the approriate array */
foreach($data as $type) {
    ${$type} = file("{$type}.txt", FILE_IGNORE_NEW_LINES);

/* Poem semantic schema */
$template = <<

The code uses five text files for each word class, containing random words and phrases. The quirkiness of the poems depends on these word collection; so if you meticulously select some interesting words for these files, you can create some offbeat poems.

Another thing you can do with the above idea it to use it to generate book or movie titles, by defining an appropriate schema. Some schema templates are shown below, with the examples.

(noun) and (noun)
Example: Pride and Prejudice, Crime and Punishment, War and Peace

The (noun) Who
Example: The Spy Who Loved Me, The Man Who Melted

Of course, to create some nice titles you require some good, selected repository of words. The same method can also be used to generate website domain names. So for example if your business vertical is apparels, you can create a word list of various terms from the apparel industry and let the generator churn out some names.

To play further, you can download the code from below.

Download code files
Downloads : [downloadcounter(auto-poem)] / File size : [downloadsize(auto-poem)]

2 thoughts to “The whacky art of Lullian poetry”

  1. Hi Sameer! Thanks for your blog, I’ve found some useful posts, and I intend to use the one on controlling WordPress’ plugins via XML-RPC.

    I just wanted to note that Ramon Llull wasn’t Spanish but Catalan, living in the Crown of Aragon and creating great pieces for the early Catalan literature. As Wikipedia states, he didn’t even speak Spanish.

    Anyway, good job and keep posting!

  2. Thanks for sharing this, nice simple condensed code. I had seen random poem generators out on the internet before but never sat down long enough to work one out myself.

    In an age old BASIC programming teaching manual I have it shows a similar example in BASIC to generate short stories.

    Its nice how such a simple program can be manipulated to output a ton of different schema.

Leave a Reply

Your email address will not be published.