Moon Rat Gardener
1 Moon Rat Gardening
2 Writing templates
3 Generate text
load-generator-file
load-generator-string
generate
4 Moon Grade Ranter
5 Weird Machinery
6 Syntax & grammar
7 links & further
8.12

Moon Rat Gardener🔗ℹ

nik gaffney <nik at fo.am>

 (require moonrat) package: moonrat

1 Moon Rat Gardening🔗ℹ

A simple template based text generator. aka. Moon Rat Gardener

MRG is a template based text generator closely following the perchance grammar and borrowing features (or non-features) from similar generators such as rant, tracery, lorem ipsum, dada engine etc. MRG aims to be simple to use and easy to read. Extensibility is possible but shouldn’t be at the expense of readability or simplicity. Extraneous syntax is kept to a minimum.

Generators are usually built around list substitution (such as this example from the perchance documentation).

output

  Your [pack] contains [item], [item] and [item].

 

pack

  purse

  backpack

  bag

  pack

  knapsack

  rucksack

 

item

  a few coins

  an old {silver|bronze} ring

  a handkerchief

  a shard of bone

  some lint

  a tin of tea leaves

This template generates sentences like “Your purse contains some lint, a few coins and a handkerchief.” or “Your knapsack contains a few coins, a shard of bones and an old silver ring.”

2 Writing templates🔗ℹ

Templates are written in plain text with placeholders for text that can be replaced. They are usually based around lists of words or phrases. Special characters are limited to the square and curly brackets. i.e. “[ ]” and “{ }”. (Further examples can be found in the data folder).

The inclusion of a word in square brackets, like [word], will select an item from the list named word. A list begins with a single word label and is followed by lines indented with 2 spaces.

Curly brackets can be used to indicate a choice, {this|that|the other} or a range of numbers {1-101} and can also be used to generate the correct article or plural for a word using {a} or {s} (e.g. fish{s} generates “fishes” and ox{s} generates “oxen”)

There are some modifier functions with the form [word.modifier] that can be used to as filters (e.g. [mouse.plural] give “mice” or [mouse.upcase] gives “MOUSE”)

A template needs to include an output label with at least one item. An element is chosen randomly from the output list and used to generate a single output text.

output

  one word

The output items can select from a list like so…

output

  [word]

 

word

  lonely

3 Generate text🔗ℹ

syntax

(load-generator-file file-path)

 
  file-path : string?
Load an MRG template from a file.

syntax

(load-generator-string template)

 
  template : string?
Load an MRG template from a string.

syntax

(generate)

Generate some text from the currently active MRG template.

Generate text from a racket programme

Read a template from a file and generate text...

#lang racket
(require moonrat)
(load-generator-file "music-genre.mg")
(generate)

Generate text from the command line

Run moonrat with a template file to generate text on the command line…

raco moonrat data/music-genre.mg

further details…

raco moonrat -h

4 Moon Grade Ranter🔗ℹ

Darius Kazemi has compiled a collection of copora for “the creation of weird internet stuff” specifically for text generation. MRG includes a basic conversion utility to help use these lists with moonrat. The converted lists are not validated in any way and will almost certainly need some manual coercion to be useful.

copora can be converted from a given url

raco cp2mg -u https://raw.githubusercontent.com/dariusk/corpora/master/data/colors/fictional.json

or file…

raco cp2mg -f data/corpora/interjections.json -o data/interjections.mg

5 Weird Machinery🔗ℹ

The templates are almost certainly a weird machine capable of unexpected machinations using list substitution (well formed recursive lists) and choice (even without assignment). Implementation is left as an exercise for the reader (see also string rewriting and antigram)

6 Syntax & grammar🔗ℹ

(to be continued…)

7 links & further🔗ℹ

Included, precluded, transcluded and occluded…