The pipotron

The pipotron

> Programming

The pipotron is an algorithm that randomly creates a sentence by stringing together predefined words. Its main purpose is entertainment: since the text is randomly generated, its meaning and sound are usually strange and funny.

Having a pipotron may seem pointless, but depending on the case, its incongruous content can frighten the enemy, make them laugh or even leave them stunned, the goal each time being to throw your opponent off balance and take advantage of the distraction to set up a winning tactic.

To create a pipotron, you first need to write a database (or DB), that is, arrays containing the list of words that could end up in the sentence, then the algorithm itself, which builds a sentence from the database by picking words from it at random. Of course, the generated text must have correct grammar, conjugation and spelling to make a satisfying sentence, even if the meaning itself can be bizarre and offbeat.

The pipotron is an algorithm of extremely variable complexity: it can produce nothing more than a simple sentence made of a prebuilt subject and a verb, or build complex sentences with several clauses, verb agreement, handling of gender and number, adverbials...

Example of a basic pipotron

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

The database The sentences generated by our pipotron will be made of:

so we will have two arrays: a subjects array and a verbs array:

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

Now comes one of the most fun parts of making a pipotron: choosing what goes into the database. To keep our sentences from all looking alike, we need to put as many words as possible into our database.

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

and in verbs:

These are just examples: you can of course put whatever words you like in your database (it's up to you to be inventive and original), but CAREFUL: to make sure the sentence is correct, only use verbs and subjects that go together! For example, all my verbs are conjugated in the third person singular because all my subjects are singular noun phrases. If I had used "the dogs" instead of "the dog", I could have ended up with a sentence like "the dogs sleeps", which is of course incorrect.

Also, it is strongly recommended to only put strings of the exact kind that the array's name suggests. For example, in verbs, I only put verbs, not verbs with an adverb ("runs quickly", for example) or with a direct object. In subjects, I didn't put any lone noun or adjective, only complete subjects. In more complex pipotrons, this precaution will help you keep your code clear and limit grammar mistakes.

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

global subjects = [ "the dog", "a plane", "the big pie", "Luke Skywalker", "a pink pony", "my grandmother's kangaroo", "a big hairy balloon filled with Romanian mayonnaise" ]; global verbs = [ "runs", "slips", "screams", "grows", "sleeps", "shimmers", "sits down" ];

The algorithm

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

I recommend creating a random function that picks a random value from a non-associative array. This is the function that will make the famous random word choices that define a pipotron. It should look like this:

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

We can now create a sentence variable that will hold the sentence 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 isn't really one yet: it has no capital letter at the start and no period at the end. So we need to create an addPunctuation function to process our sentence:

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

All that's left is to apply this function to our sentence and return sentence, and our pipotron is done!

global subjects = [ "the dog", "a plane", "the big pie", "Luke Skywalker", "a pink pony", "my grandmother's kangaroo", "a big hairy balloon filled with Romanian mayonnaise" ]; global verbs = [ "runs", "slips", "screams", "grows", "sleeps", "shimmers", "sits down" ]; function getPipotron(){ var sentence = random(subjects) + " " + random(verbs); sentence = addPunctuation(sentence); return sentence; } function random(array){ return array[randInt(0, count(array))]; } function addPunctuation(text){ text += "."; //capital letter text = toUpper(charAt(text, 0)) + substring(text, 1, length(text) - 1); return text; }

We could have declared our subjects with a capital letter right away and put a period at the end of the verbs in the database, but the current solution is more dynamic and more flexible. Imagine we want to make a more complex sentence with a direct object after the verb. If our verbs already ended with a period, there would be a period in the middle of the sentence, before the object, and the text wouldn't end with a period. You get the idea: adding the punctuation after building the sentence is a good habit to get into for when you create more elaborate pipotrons.

As you will see when testing this pipotron, the possible sentences are very limited, and it's not unusual to get the exact same sentence twice. That's because this pipotron is really very simple: there are only 7 subjects * 7 verbs = 49 possible sentences. That's the advantage of making your pipotron more complex: you get longer, more varied and funnier sentences.

A few pipotrons from the community

More information in this pipotron thread.

Note: before the nitpickers jump in, no, I haven't covered the difference between random and pseudo-random in this introduction. Yes, in theory, a true pipotron should only make truly random choices, whereas the rand and randInt functions actually return pseudo-random numbers. Here the difference is negligible: these functions do the job just fine for our pipotrons.