markov

package
v0.0.0-...-b865509 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2023 License: MIT Imports: 6 Imported by: 0

README

markov (package)

GoDoc License MIT

This package generates markov string sequences.

Usage

Words
package main

import (
	"fmt"
	"strings"

	"github.com/jsageryd/markov/markov"
)

func main() {
	words := []string{
		"albatross",
		"alligator",
		"antelope",
	}

	c := markov.NewStringsChain(2, 0)

	for _, s := range words {
		c.Feed(strings.Split(s, ""))
	}

	for n := 0; n < 3; n++ {
		fmt.Println(strings.Join(c.Generate(), ""))
	}
}
albator
antelope
alligatross
Sentences
package main

import (
	"fmt"
	"strings"

	"github.com/jsageryd/markov/markov"
)

func main() {
	sentences := []string{
		"I am a cat.",
		"As yet I have no name.",
		"I've no idea where I was born.",
	}

	c := markov.NewStringsChain(1, 0)

	for _, s := range sentences {
		c.Feed(strings.Split(s, " "))
	}

	for n := 0; n < 3; n++ {
		fmt.Println(strings.Join(c.Generate(), " "))
	}
}
I am a cat.
As yet I am a cat.
I've no name.

Documentation

Overview

Package markov generates string sequences.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type StringsChain

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

StringsChain is a Markov chain of strings.

Example (Sentences)
sentences := []string{
	"squirrels are members of the family Sciuridae",
	"squirrels are indigenous to the Americas, Eurasia, and Africa",
	"the earliest known squirrels date from the Eocene period",
}

c := NewStringsChain(1, 0)

for _, s := range sentences {
	c.Feed(strings.Split(s, " "))
}

for n := 0; n < 3; n++ {
	fmt.Println(strings.Join(c.Generate(), " "))
}
Output:

squirrels are indigenous to the family Sciuridae
squirrels are members of the family Sciuridae
the earliest known squirrels date from the family Sciuridae
Example (Words)
words := []string{
	"albatross",
	"alligator",
	"antelope",
}

c := NewStringsChain(2, 0)

for _, s := range words {
	c.Feed(strings.Split(s, ""))
}

for n := 0; n < 3; n++ {
	fmt.Println(strings.Join(c.Generate(), ""))
}
Output:

albator
antelope
alligatross

func NewStringsChain

func NewStringsChain(order int, randSeed int64) *StringsChain

NewStringsChain returns a new StringsChain of the given order.

func (*StringsChain) ExportState

func (c *StringsChain) ExportState(w io.Writer) error

ExportState exports the state of the chain. The state of the PRNG is not exported.

func (*StringsChain) Feed

func (c *StringsChain) Feed(seq []string)

Feed feeds the chain with the given sequence of strings.

func (*StringsChain) Generate

func (c *StringsChain) Generate() []string

Generate generates a sequence of strings based on those fed.

func (*StringsChain) ImportState

func (c *StringsChain) ImportState(r io.Reader) error

ImportState imports the given state. The state of the PRNG is unchanged.

func (*StringsChain) String

func (c *StringsChain) String() string

String returns the chain visualised.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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