ROBBESTAD ARCHIVErecovered 2026-09-01

ARCHIVE FILE

Procedurally generated text with JavaScript

written
archived
source
robbestad.com (Blogger)

The lustful Prince screwed the ravishing lemur

Wouldn't it be fun if you could construct random and coherent sentences procedurally? Imagine you're working on a game and would like to introduce a variety in the text being presented to the player, but don't want to write a bunch of boilerplate text? You could type in a few variables yourself and do a search and replace, or you could make life simpler and use Rantjs to generate the text.

[Timeline Photoshop]

Rantjs is available via Github and npm. Using it in your own projects is easy if you use the npm module. Install with npm install rantjs and include it with the following code:

var rantjs = require("rantjs");
var rant = new rantjs.SimpleRant;
var sentence = rant.rantConstructor('<firstname male> likes to <verb-transitive> <noun.plural> with <pron poss male> pet <noun-animal> on <timenoun dayofweek plural>.');

console.log(sentence); // 'Sean likes to chop parrots with his pet cat on Saturdays.'

SimpleRant loads the bundled English dictionary. rantConstructor takes a template string. Each <token> is a lookup: firstname male picks a male first name, verb-transitive a verb that takes an object, noun.plural a plural noun, pron poss male a possessive pronoun, noun-animal an animal, timenoun dayofweek plural a weekday. The generator fills the slots independently, so the same template yields a different sentence every call. That is the whole trick — you write the shape of the sentence once.

Rantjs comes preinstalled with a english dictionary. Adding items to the dictionary is done by setting properties in the variable dic. For instance, if you want an alien tag you could set it as follows (this can be pasted in via the Console in FireFox/Chrome, or loaded as a custom.dic.js):

dic.alien = {};
var alien_race = ["Badoon/Badoons", "Brood/The Broods", "Celestials/The Celestials", "Kree/The Kree"];
dic.alien.all = alien_race;
dic.alien.race = alien_race;
dic.alien.subs = ["singular", "plural"];
dic.tokens.push("alien");

Each string is a singular/plural pair split on /. subs tells the parser those two forms exist. tokens.push("alien") registers the tag so templates can use it. After that you can write:

<alien race plural>

which might print The Kree, or even:

<alien plural> are like <adj pp> <noun dog plural>.

which mixes the new dictionary with the built-in adjectives and dog nouns.

A history of Rantjs

Rantjs is an attempt at implementing a project called Rant. I read a writeup on it in a Reddit thread and thought the idea was terrific, but couldn't use it is written in C# and runs on Windows, and that's pretty far removed from my platform of choice and workflow.

So, I started working on a JavaScript implementation I called Rantjs, and have just now reached a point where it's starting to become usable. Rant is a complex app with tons of features, so it'll take a long while before Rantjs reaches feature parity, but the aim is to get there eventually.

For now, my only real world implementation of Rantjs is the codeword at the bottom of this site (generated on every release), but I'm excited about the possibilities it opens in games development. I imagine creating specific dictionaries that are geared towards settings such as medieval RPGs, space RPGs etc.

Source: https://github.com/robbestad/Rantjs

← index