Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

markov source realm

Package markov ports the canonical Go example "Generating arbitrary text: a Markov chain algorithm" ([https://go.dev/...

Overview

Package markov ports the canonical Go example "Generating arbitrary text: a Markov chain algorithm" (https://go.dev/doc/codewalk/markov/) to a gno.land realm.

The original reads words from an io.Reader, builds a map from each two-word prefix to the list of words that followed it, then walks that chain picking random suffixes to babble new text. Here the corpus lives on-chain: Feed appends words to a persistent avl.Tree-backed chain, and Generate walks it. The only change needed for on-chain determinism is the source of randomness: instead of math/rand we seed a small LCG with runtime.ChainHeight(), so Generate is a pure, reproducible query that still varies block to block.

Feed is a crossing function (takes `cur realm` first) because it mutates on-chain state; Generate and Render are read-only.

Functions 4

func Feed

crossing Action
1func Feed(cur realm, text string) string
source

Feed appends text to the on-chain corpus, extending the Markov chain. Any caller may enrich the corpus — this is the open-membership demo model.

func Generate

Action
1func Generate(nWords int) string
source

Generate babbles up to nWords of Markov text from the current corpus, seeded deterministically by the chain height. Read-only.

func Render

1func Render(path string) string
source

Render shows a freshly generated sample plus corpus stats. The path may carry a word count, e.g. Render("/60").

func Stats

Action
1func Stats() (int, int)
source

Stats returns (totalWords, prefixCount) for the current corpus.

Types 2

type Chain

struct
1type Chain struct {
2	table  avl.Tree
3	prefix Prefix
4	words  int
5}
source

Chain is the on-chain Markov chain. table maps prefix key -> *suffixList; prefix is the rolling build window so successive Feed calls extend one continuous corpus rather than restarting; words is the running word count.

type Prefix

slice
1type Prefix []string
source

Prefix is a sliding window of the last prefixLen words seen. It mirrors the Prefix type in the original program.

Imports 5

Source Files 2