markov

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2019 License: MIT Imports: 7 Imported by: 0

README

markov

GoDoc Go Report Card Build Status

Markov model library for Go

Examples

Create a store for holding your Markov table data. Two built-in types are provided:

store := markov.NewInMemoryTableStore()
store, err := markov.NewBoltTableStore("path/to/store")

Stores may need to be closed after use:

defer store.Close()

Create a Markov model using the store:

model := markov.NewModel(store)

Use an Accumulator to build the model. The Accumulator needs the order of the model to build (here, we’re specifying a 3rd-order Markov model, meaning that each symbol is dependent on the three symbols that come before it):

acc := markov.NewAccumulator(model, 3)

Symbols can be added in sequence to the accumulator. Symbols are strings, but can represent characters, words, events, or any abstract element in an ordered sequence.

err := acc.Add("first symbol")
…
err = acc.Add("second symbol")
…
err = acc.Add("third symbol")

An empty string signifies the “end” of a sequence of symbols, if such a thing makes sense for your model:

err := acc.Add("")

Use a Generator to sample from the model. The Generator needs to know the order of the model for sampling (usually the same order as used for the Accumulator), and also a SampleSource which determines how samples are drawn (here, we use a seeded pseudorandom number generator):

gen := markov.NewGenerator(model, 3, rand.New(rand.NewSource(int64(12345))))

Sequences of symbols can be generated by sampling from the Generator:

symbol, err := gen.Get()

If the end of a sequence is reached, the empty symbol ("") is returned with no error.

Documentation

Index

Constants

View Source
const BOLT_BUCKET = "markov"

Variables

View Source
var ErrTableNotFound = errors.New("table not found")

Functions

This section is empty.

Types

type Accumulator

type Accumulator struct {
	Model           *Model
	CurrentSequence Sequence
}

func NewAccumulator

func NewAccumulator(m *Model, order uint) *Accumulator

func (*Accumulator) Add

func (acc *Accumulator) Add(symbol string) (err error)

type BoltTableStore

type BoltTableStore struct {
	Bolt *bolt.DB
}

func NewBoltTableStore

func NewBoltTableStore(filePath string) (*BoltTableStore, error)

func (*BoltTableStore) Close

func (ts *BoltTableStore) Close() error

func (*BoltTableStore) Get

func (ts *BoltTableStore) Get(k SequenceKey, dest *Table) error

func (*BoltTableStore) Put

func (ts *BoltTableStore) Put(k SequenceKey, table *Table) error

type Generator

type Generator struct {
	Model           *Model
	CurrentSequence Sequence
	SampleSource    SampleSource
}

func NewGenerator

func NewGenerator(m *Model, order uint, s SampleSource) *Generator

func (*Generator) Get

func (g *Generator) Get() (symbol string, err error)

type InMemoryTableStore

type InMemoryTableStore struct {
	TablesBySequence map[SequenceKey]*Table
}

func NewInMemoryTableStore

func NewInMemoryTableStore() *InMemoryTableStore

func (*InMemoryTableStore) Get

func (ts *InMemoryTableStore) Get(key SequenceKey, dest *Table) error

func (*InMemoryTableStore) Put

func (ts *InMemoryTableStore) Put(key SequenceKey, table *Table) error

type Model

type Model struct {
	Tables TableStore
}

func NewModel

func NewModel(store TableStore) *Model

func (*Model) Add

func (m *Model) Add(seq Sequence, nextSymbol string) error

func (Model) Sample

func (m Model) Sample(seq Sequence, sampleValue float64) (nextSymbol string, err error)

type SampleSource

type SampleSource interface {
	Float64() float64
}

type Sequence

type Sequence struct {
	Symbols []string
}

func EmptySequence

func EmptySequence(order uint) Sequence

func SequenceWith

func SequenceWith(symbols ...string) Sequence

func (Sequence) Key

func (s Sequence) Key() SequenceKey

func (Sequence) Order

func (s Sequence) Order() uint

func (Sequence) WithNext

func (s Sequence) WithNext(nextSym string) Sequence

type SequenceKey

type SequenceKey string

type Table

type Table struct {
	TotalSymbols uint64
	Entries      []TableEntry
	EntryIndices map[string]int
}

func EmptyTable

func EmptyTable() Table

func (*Table) Add

func (t *Table) Add(symbol string)

func (Table) Sample

func (t Table) Sample(symbolIndex uint64) (symbol string)

type TableEntry

type TableEntry struct {
	Frequency uint64
	Symbol    string
}

type TableStore

type TableStore interface {
	Get(key SequenceKey, dest *Table) error
	Put(key SequenceKey, table *Table) error
}

Jump to

Keyboard shortcuts

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