The pipotron

The pipotron

> Programming

The pipotron is an algorithm that allows you to randomly create a sentence from pre-defined words placed end to end. The main purpose of the pipotron is entertainment, because since the text is randomly generated, it usually makes sense and sounds weird and funny.

To create a pipotron, you must first write a database (or BDD), i.e. tables containing the list of words that could potentially be part of the sentence, then l 'algorithm itself which will take care of constructing a sentence from the database, by picking random words from it. Of course, the generated text must have correct grammar, conjugation, and spelling in order to achieve a satisfying sentence, although the meaning itself may be quirky and offbeat.

The pipotron is an algorithm with extremely variable complexity: it can just as well produce a simple sentence made up of a pre-constructed subject and a verb as it can make complex sentences with several propositions, verb agreements, the management of gender and number, circumstantial complements...

Example of a rudimentary pipotron

That's the theory, now some practice. We are going to write a very simple pipotron (in LeekScript), like the first category mentioned above.

The DB The sentences generated by our pipotron will be composed:

we will therefore have two tables: a subjects table and a verbs table:

global subjects = []; global verbs = [];

This is now one of the most fun parts of making a pipotron: choosing the content of the database. Indeed, so that our sentences do not all look alike, we must introduce as many words as possible into our database.

In subjects, for example, we will put the following strings:

and in verbs:

These are examples, you can of course put the words you want in your database (it's up to you to be inventive and original), but ATTENTION, to be sure that the sentence is correct, only put verbs and topics that correspond to each other! For example, all my verbs are conjugated in the third person singular because all my subjects are singular noun phrases. If I had put "the dogs" instead of "the dog", I could have come across a sentence like "the dogs sleeps" which is of course incorrect.

Moreover, it is really advisable to put in the arrays only strings of the precise type that the name of the array suggests. For example, in verbs, I only put verbs, and not verbs with adverb ("short quickly" for example) or with COD. In subjects I did not put any name or isolated adjective, only complete subjects. This precaution will help you in more complex pipotrons to clarify your code and limit grammatical errors.

We now have a complete database, made up of subjects and verbs:

global subjects = [ "the dog", "a plane", "the big pie", "Luke Skywalker", "a pink pony", "my grandmother's kangaroo", "a large hairy balloon filled with Romanian mayonnaise" ]; global verbs = [ "short", "slip", "calls out", "grows", "sleep", "shimmers", "sits" ];

The algorithm

Now that we have our database, we can move on to the pipotron algorithm, which we will place in a getPipotron function. Our algorithm must:

I advise you to create a randTab function that takes a random value from a non-associative array. It is therefore the function that will carry out the famous choice of random words characteristic of the pipotron. It should look like this:

function random(array){ return array[randInt(0, count(array))]; }

We can now create a phrase variable that will contain the phrase to say:

function getPipotron(){ var sentence = random(subjects) + " " + random(verbs); } function random(array){ return array[randInt(0, count(array))]; }

The problem is that our sentence is not one. Indeed, it does not have a capital letter at the beginning, nor a period at the end. We must therefore create an addPunctuation function in order to process our sentence:

function addPunctuation(@text){ text += "."; // uppercase text = toUpper(charAt(text, 0)) + substring(text, 1, length(text) - 1); }

All that remains is to apply this function to our phrase and return phrase and our pipotron is finished!

overall subjects = [ "the dog", "a plane", "the big pie", "Luke Skywalker", "a pony r