Creates a string generator based on a syntax.
Turns Definitions into a Grammar.
Prints the Grammar
Generates a string while a predicate is valid
Generates an output found in the resulting grammar.
You can use generateCommands instead, If you intend to get the output right away.
import Dict
import Json.Decode
import Random exposing (Generator)
import Result.Extra
import Tracery.Syntax exposing (Definition(..), Expression(..))
import Set
andThenToString : ({ variable : String } -> String) -> Int -> (Grammar -> Generator Grammar) -> Grammar -> String
andThenToString fun seed gen grammar =
Random.step (gen grammar) (Random.initialSeed seed)
|> Tuple.first
|> toString fun
input : Grammar
input =
[ ( "origin", Choose [ [ Value "A ", Variable "animal" ] ] )
, ( "animal"
, Choose
[ [ Value "cat, looking at a ", Variable "animal" ]
, [ Value "bird." ]
]
)
]
|> Dict.fromList
|> fromDefinitions
input
|> andThenToString (\{variable} -> "dog.") 42 (generateOutput (\_ -> True) defaultStrategy)
--> "A cat, looking at a cat, looking at a cat, looking at a cat, looking at a bird."
input
|> andThenToString (\{variable} -> "dog.") 42 (generateOutput (\_ -> True) (noRecursionStrategy (Set.fromList ["animal"])))
--> "A bird."Computes the command in grammar.next.
Afterwards the next command gets loaded
import Dict
import Json.Decode
import Random exposing (Generator)
import Result.Extra
import Tracery.Syntax exposing (Definition(..), Expression(..))
andThenToString : ({ variable : String } -> String) -> Int -> (Grammar -> Generator Grammar) -> Grammar -> String
andThenToString fun seed gen grammar =
Random.step (gen grammar) (Random.initialSeed seed)
|> Tuple.first
|> toString fun
input : Grammar
input =
[ ( "origin", Choose [ [ Value "A ", Variable "animal" ] ] )
, ( "animal"
, Choose
[ [ Value "cat, looking at a ", Variable "animal" ]
, [ Value "bird." ]
]
)
]
|> Dict.fromList
|> fromDefinitionsusing this function, you can step through the computation
input
|> andThenToString (\{variable} -> "dog.") 42 (generateNext defaultStrategy)
--> "A dog."The second step does nothing (some steps only perform internal rearrangements)
input
|> andThenToString (\{variable} -> "dog.") 42
(\g -> g
|> (generateNext defaultStrategy)
|> Random.andThen (generateNext defaultStrategy)
)
--> "A dog."Doing a second step results in
input
|> andThenToString (\{variable} -> "dog.") 42
(\g -> g
|> (generateNext defaultStrategy)
|> Random.andThen (generateNext defaultStrategy)
|> Random.andThen (generateNext defaultStrategy)
)
--> "A cat, looking at a dog."The strategy specifies the algorithm to choose an option
This strategy will choose any option
This strategy will never choose a recursive option
This strategy will only chose recursive options
Moves to the next command without executing anything.
toNext : Grammar -> Grammar
toNext grammar =
grammar |> withCommands grammar.stackSets Commands of a Grammar.
sets the output as input.
set the remaining commands as output
next- what needs to be generated next?constants- whatdefinitions- the grammar rules (see Syntax)