Skip to content

Aarneus/grotesque

Repository files navigation

grotesque

A Clojure(script) library for text generation.
Uses a context-free grammar similar to Tracery but with added state conditions and effects on rules.
Honestly if I didn't want to to use this library with Clojure this would most likely be a Tracery extension.

Tests Clojars Project

A text-generation library

Grotesque works by generating text according to a grammar defined by you.

An example grammar

{"color"    ["red" "blue" "green"]
 "vehicle"  [["car"   :set.vehicle.car] 
             ["boat"  :set.vehicle.boat] 
             ["plane" :set.vehicle.plane]]
 "terrain"  [["on the road"  :when.vehicle.car]
             ["in the water" :when.vehicle.boat]
             ["in the sky"   :when.vehicle.plane]]
 "position" ["A #color# #vehicle# is #terrain#."]}

The example output for "#position#"

A red car is on the road.
A green boat is in the water.
A blue plane is in the sky.
A red boat is in the water.

The basic functionality of the grammar is that a word in the text that is surrounded by hashtags # is replaced with the phrase in a corresponding rule.
Here "The #color# ball." is replaced with e.g. "The blue ball."

Note that rules also accept either strings or keywords as their keys.
They also accept brackets [ and ] as separators instead of hashtags.

This simple example also demonstrates how the state handling works. The :vehicle rules set the type of vehicle and the :terrain checks it to make sure that e.g. A red boat is in the sky. is never generated.

Note that the state handling functions are omitted here.
How they work and how to implement your own is detailed in the documentation for state.

Documentation

Read the docs here.

Quickstart

The simplest way to start playing with Grotesque is to generate a simple grammar with an equally simple example model:

(ns example.core
  (:require [grotesque.core :as g]))

(defn handler-fn [grammar rule-id [_ attribute value]]
  (assoc-in grammar [:data :model attribute] value))

(defn validator-fn [grammar rule-id [_ attribute value]]
  (= value (get-in grammar [:data :model attribute])))

(-> {"color"    ["red" "blue" "green"]
     "vehicle"  [["car"   :set.vehicle.car] 
                 ["boat"  :set.vehicle.boat] 
                 ["plane" :set.vehicle.plane]]
     "terrain"  [["on the road"  :when.vehicle.car]
                 ["in the water" :when.vehicle.boat]
                 ["in the sky"   :when.vehicle.plane]]
     "position" ["A #color# #vehicle# is #terrain#."]}
    (g/create-grammar)
    (g/set-handler :set handler-fn)
    (g/set-validator :when validator-fn)
    (g/generate "#position#")
    :generated)

About

Heavily inspired by the work of
Kate Compton,
Elan Ruskin and
Emily Short.

The name itself is a riff on Tracery.

Distributed under the MIT Expat License.

About

A Clojure(Script) library for text generation

Resources

License

Stars

Watchers

Forks

Packages

No packages published