Skip to content

An (currently incomplete) implementation of the Tracery text-expansion library by GalaxyKate

License

Notifications You must be signed in to change notification settings

martletandco/tracery-go

Repository files navigation

tracery-go

An (currently incomplete) implementation of the Tracery text-expansion library by GalaxyKate

import "github.com/martletandco/tracery-go"
g := tracery.NewGrammar()
g.PushRule("toggle", "flop")
g.PushRule("animal", "fox", "dog", "snail", "whale", "cow", "emu")
g.PushRule("pace", "quick", "slow", "sluggard", "rapid", "brisk")
g.PushRule("colour", "brown", "red", "purple", "orange")
g.PushRule("disposition", "lazy", "alert", "bored", "distracted", "eager")
out := g.Flatten("[toggle:flip]The #pace# #colour# #animal# jumped over the #disposition# #animal# to #toggle#[toggle:POP] #toggle#")
fmt.Println(out)

Outputs

The rapid red fox jumped over the eager emu

See in the Go Playground

Note that due to caching and other reasons random numbers to not really work in the playground

List of important features missing

  • Load/save to/from JSON
  • Parse errors
  • Expand for grammar debugging*
  • CBDQ compatibility†
  • (Probably many others that have escaped me just now)

List of less important but still missing features

  • Convenience functions for serialising/deserialising rules
  • A command line wrapper for ad-hock usage
  • Adding rules in bulk
  • Adding modifiers in bulk

List of ideas to explore

  • Symbol/modifier missing handler

  • Improve indefinite article application1

  • Mode option to support new versions or different compatibility

  • Add short hand for an in-place random selection based on [x:1,2,3]#x#

  • *Parses and 'expands' eagerly, rather than lazily as the original does, so I'm not sure the same interface works

  • †Some features are implemented, such as the Random Push (i.e. [x:1,2])

About

An (currently incomplete) implementation of the Tracery text-expansion library by GalaxyKate

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages