Tracery is a text-generation language mostly used for twitter bots.
See Tracery.io for more information.
Runs a grammar until it ends.
Some recursive definitions might take a long time.
Use runTo if you want to avoid long waiting times.
Runs a grammar until it reaches a key in the list.
import Json.Decode
import Random
import Result.Extra
generateTo : List String -> ({variable:String} -> String)-> Int -> String -> String
generateTo list fun seed json =
json
|> Tracery.fromJson
|> Result.Extra.unpack
Json.Decode.errorToString
(\grammar ->
Random.step (Tracery.runTo list grammar) (Random.initialSeed seed)
|> Tuple.first
|> toString fun
)
"""
{ "origin": ["A #color# #animal#"]
, "color": ["black","white","gray"]
, "animal":
[ "cat, looking at a #color# #animal#"
, "bird."
]
}
"""
|> generateTo ["animal"] (\{variable} -> "dog.") 42
--> "A black dog."Will just write the current output.
use run or runTo, to actually compute something.
Turns a tracery json-string into a generator
A tracery json is a object that has a
originfield.The
#and\characters need to be escaped.If you provide a list, tracer will tick an element at random.
You can reference other fields using
#..#Definitions may also be recursive.
You can define constants by providing a string instead of a list.
You may define sub-definitions to organize your definitions.