gomarkov

package module
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2023 License: MIT Imports: 11 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

gomarkov

GoDoc Go Report Card

Go implementation of markov chains for textual data.

You can find out more about markov chains here and here

Usage

package main

import (
	"github.com/pavel/gomarkov"
	"fmt"
	"strings"
	"io/ioutil"
	"encoding/json"
)

func main() {
	//Create a chain of order 2
	chain := gomarkov.NewChain(2)

	//Feed in training data
	chain.Add(strings.Split("I want a cheese burger", " "))
	chain.Add(strings.Split("I want a chilled sprite", " "))
	chain.Add(strings.Split("I want to go to the movies", " "))

	//Get transition probability of a sequence
	prob, _ := chain.TransitionProbability("a", []string{"I", "want"})
	fmt.Println(prob)
	//Output: 0.6666666666666666

	//You can even generate new text based on an initial seed
	chain.Add(strings.Split("Mother should I build the wall?", " "))
	chain.Add(strings.Split("Mother should I run for President?", " "))
	chain.Add(strings.Split("Mother should I trust the government?", " "))
	next, _ := chain.Generate([]string{"should", "I"})
	fmt.Println(next)

	//The chain is JSON serializable
	jsonObj, _ := json.Marshal(chain)
	err := ioutil.WriteFile("model.json", jsonObj, 0644)
	if err != nil {
		fmt.Println(err)
	}
}

Examples

Documentation

Index

Examples

Constants

View Source
const (
	StartToken = "$"
	EndToken   = "^"
)

Tokens are wrapped around a sequence of words to maintain the start and end transition counts

Variables

This section is empty.

Functions

func ConcurrentSearchClosest added in v0.2.0

func ConcurrentSearchClosest(slice []string, key string, chunkCount int) int
Example
println(ConcurrentSearchClosest(slice, "8 марта", runtime.NumCPU()))

Types

type Chain

type Chain struct {
	Order int
	// contains filtered or unexported fields
}

Chain is a markov chain instance

func NewChain

func NewChain(order int) *Chain

NewChain creates an instance of Chain

func (*Chain) Add

func (chain *Chain) Add(input []string)

Add adds the transition counts to the chain for a given sequence of words

func (*Chain) Generate

func (chain *Chain) Generate(current NGram) (string, error)

Generate generates new text based on an initial seed of words

func (*Chain) MarshalJSON

func (chain *Chain) MarshalJSON() ([]byte, error)

MarshalJSON ...

func (*Chain) TransitionProbability

func (chain *Chain) TransitionProbability(next string, current NGram) (float64, error)

TransitionProbability returns the transition probability between two states

func (*Chain) UnmarshalJSON

func (chain *Chain) UnmarshalJSON(b []byte) error

UnmarshalJSON ...

type NGram

type NGram []string

NGram is an array of words

type Pair

type Pair struct {
	CurrentState NGram  // n = order of the chain
	NextState    string // n = 1
}

Pair is a pair of consecutive states in a sequence

func MakePairs

func MakePairs(tokens []string, order int) []Pair

MakePairs generates n-gram pairs of consecutive states in a sequence

Directories

Path Synopsis
examples
fakernews command
gibberish command
pokenamer command

Jump to

Keyboard shortcuts

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