Orasund/elm-tracery/1.0.0

NOTE: the latest version is 5.0.0

Tracery

Tracery is a text-generation language mostly used for twitter bots.

See Tracery.io for more information.

fromJson : String -> Result Error (Generator String)

Turns a tracery json-string into a generator

A tracery json is a object that has a origin field.

import Json.Decode
import Random
import Result.Extra


seed : Random.Seed
seed =
    Random.initialSeed 40


generate : String -> String
generate json =
    json
        |> Tracery.fromJson
        |> Result.Extra.unpack
            Json.Decode.errorToString
            (\generator ->
                Random.step generator seed
                    |> Tuple.first
            )

You can reference other fields using #..#

"""
{ "origin": "I have two pets: a #pet# and a #pet#"
, "pet": ["cat","dog","fish","parrot"]
}
"""
|> generate
--> "I have two pets: a fish and a cat"

Definitions may also be recursive.

"""
{ "origin": "I have #pets#"
, "pets": ["a #pet#","a #pet# and #pets#"]
, "pet": ["cat","dog","fish","parrot"]
}
"""
|> generate
--> "I have a cat and a dog"

You can define constants by providing a string instead of a list.

"""
{ "origin": "My #favoritePet# is the best #favoritePet# in the world"
, "favoritePet" : "#pet#"
, "pet": ["cat","dog","fish","parrot"]
}
"""
|> generate
--> "My fish is the best fish in the world"

You may define sub-definitions to organize your definitions.

"""
{ "origin": ["My #cat#","My #dog#"]
, "cat":
  { "origin":"cat is named #name#"
  , "name": ["Cleopatra","Cesar"]
  }
, "dog":
  { "origin":"dog is named #name#"
  , "name": ["Athena","Zeus"]
  }
}
"""
|> generate
--> "My dog is named Athena"
fromSyntax : Syntax -> Generator String

Creates a string generator based on a syntax.