x: within.website/x/markov Index | Files

package markov

import "within.website/x/markov"

Package markov handles markov chain generation.

This Markov chain code is taken from the "Generating arbitrary text" codewalk: http://golang.org/doc/codewalk/markov/

Minor modifications have been made to make it easier to integrate with a webserver and to save/load state

Index

Package Files

legal.go markov.go

type Chain Uses

type Chain struct {
    Chain map[string][]string
    // contains filtered or unexported fields
}

Chain contains a map ("chain") of prefixes to a list of suffixes. A prefix is a string of prefixLen words joined with spaces. A suffix is a single word. A prefix can have multiple suffixes.

func NewChain Uses

func NewChain(prefixLen int) *Chain

NewChain returns a new Chain with prefixes of prefixLen words.

func (*Chain) Generate Uses

func (c *Chain) Generate(n int) string

Generate returns a string of at most n words generated from Chain.

func (*Chain) Load Uses

func (c *Chain) Load(fileName string) error

Load the chain from a file

func (*Chain) Save Uses

func (c *Chain) Save(fileName string) error

Save the chain to a file

func (*Chain) Write Uses

func (c *Chain) Write(in string) (int, error)

Write parses the bytes into prefixes and suffixes that are stored in Chain.

type Prefix Uses

type Prefix []string

Prefix is a Markov chain prefix of one or more words.

func (Prefix) Shift Uses

func (p Prefix) Shift(word string)

Shift removes the first word from the Prefix and appends the given word.

func (Prefix) String Uses

func (p Prefix) String() string

String returns the Prefix as a string (for use as a map key).

Package markov imports 7 packages (graph) and is imported by 2 packages. Updated 2019-06-07. Refresh now. Tools for package owners.