Version: v1.2.3 Latest Latest

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

Go to latest
Published: Jan 12, 2020 License: CC0-1.0, BSD-3-Clause Imports: 7 Imported by: 0



Package markov handles markov chain generation.

This Markov chain code is taken from the "Generating arbitrary text" codewalk:

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



This section is empty.


This section is empty.


This section is empty.


type Chain

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

func NewChain(prefixLen int) *Chain

NewChain returns a new Chain with prefixes of prefixLen words.

func (*Chain) Generate

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

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

func (*Chain) Load

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

Load the chain from a file

func (*Chain) Save

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

Save the chain to a file

func (*Chain) Write

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

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

type Prefix

type Prefix []string

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

func (Prefix) Shift

func (p Prefix) Shift(word string)

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

func (Prefix) String

func (p Prefix) String() string

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

Source Files

Jump to

Keyboard shortcuts

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