generator

package
v0.0.0-...-c477d30 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 6, 2020 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package generator contains functionality to generate randomized, structured text based on a mixture of randomized selection and configured selections.

Index

Constants

View Source
const RoundsMax = uint8(30)

RoundsMax defines the maximum number of replacement rounds allowed during Rendering.

Variables

This section is empty.

Functions

func Render

func Render(instruction string, i *Inventory, state *State, source rng.RandomSource) string

Render generates output from the supplied instruction string using the Inventory, State and RandomSource. The instructions are rendered by replacing one element at a time, selecting the first complete Token or first complete Variable found (in that order). Tokens or Variables which include other Token or Variable references are considered invalid/incomplete and will be skipped.

Types

type Generator

type Generator struct {
	// contains filtered or unexported fields
}

Generator is a reusable text generator which potentially produces different output each time it is used.

func CreateGenerator

func CreateGenerator(instructions string, inventory *Inventory) *Generator

CreateGenerator creates a reusable text generator based on the instructions provided and the given inventory. Each time the generator is used, it can potentially produce different output.

func (*Generator) Run

func (g *Generator) Run() string

Run executes the generator with a new empty State.

func (*Generator) RunWithState

func (g *Generator) RunWithState(state *State) string

RunWithState executes the generator with the supplied State. This function is used to execute Generators with pre-defined state for instruction sets that require variable substitution.

func (*Generator) UseRandomSource

func (g *Generator) UseRandomSource(rng rng.RandomSource)

UseRandomSource assigns a RandomSource to use when picking tokens.

type Inventory

type Inventory struct {
	// contains filtered or unexported fields
}

Inventory acts as a collection of categorized Tokens which can be queried for both randomized and parameterized selection.

func CreateInventory

func CreateInventory() *Inventory

CreateInventory creates a new, empty Inventory.

func (*Inventory) Add

func (i *Inventory) Add(t Token) *Token

Add adds an existing Token to this Inventory. The added token is returned, to help support chaining and make it interchangeable with AddToken.

func (*Inventory) AddToken

func (i *Inventory) AddToken(id string, content string, rarity float64, tags map[string]string) *Token

AddToken creates and adds a new Token to this Inventory. The created token is returned to support chaining and to make it interchangeable with Add.

func (*Inventory) Load

func (i *Inventory) Load(path string) error

Load adds Tokens to the Inventory from a YAML file containing an array of Token definitions.

func (*Inventory) Pick

func (i *Inventory) Pick(selector *Selector, offset float64) *Token

Pick selects a random Token from the inventory which matches the given Selector. If no matching Tokens are found, then nil is returned.

type Properties

type Properties map[string]string

Properties defines the structure used to store token properties.

type Selector

type Selector struct {
	Category string
	Require  map[string]string
	Exclude  map[string]string
	Exists   map[string]bool
}

Selector acts as a structured query for Tokens, describing the Category and required/excluded characteristics necessary for selection.

func ParseSelector

func ParseSelector(category string, options string) *Selector

ParseSelector examines string parts to create a new Selector.

func (*Selector) IsSimple

func (s *Selector) IsSimple() bool

IsSimple checks to see if the Selector only selects based upon its category.

func (*Selector) MatchesToken

func (s *Selector) MatchesToken(t *Token) bool

MatchesToken checks if the selector would select the supplied Token.

type State

type State struct {
	Vars map[string]string
}

State includes configurable state that can be used to configure individual executions of a Generator.

func CreateState

func CreateState() *State

CreateState builds a new, empty State.

func (*State) SetVars

func (s *State) SetVars(v map[string]string)

SetVars sets the values of one or more variables based on the supplied map.

type Token

type Token struct {
	Category   string
	Content    string
	Rarity     float64
	Properties map[string]string
	SetVars    map[string]string
}

Token represents a single item which can be placed into the generated output of a Generator.

func BuildToken

func BuildToken(category string, content string, rarity float64, tags Properties) Token

BuildToken builds a new Token based upon the supplied information. No SetVars are defined. If you want to set State variables on render, use OnRenderSet to define them.

func (*Token) IsValid

func (t *Token) IsValid() bool

IsValid checks to see if this Token is valid and usable for generation. Invalid Tokens may become valid if Normalize is called on them, but this is not guaranteed. If you are reading or creating Tokens with non-validated input, you should call Normalize before checking validity.

func (*Token) Normalize

func (t *Token) Normalize()

Normalize updates the Token to ensure that it matches required behaviors. Categories must not start or end with whitespace. Rarities must not be zero or negative. If the Rarity is invalid, it is set to a default of 1.0

func (*Token) OnRenderSet

func (t *Token) OnRenderSet(variable string, value string)

OnRenderSet defines a State variable to set when this Token is rendered to Generator output.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL