regomarkov

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2019 License: MIT Imports: 7 Imported by: 1

README

REgomarkov

GoDoc

Go implementation of markov chains for textual data.

You can find out more about markov chains here and here

Notice

This is a rehost of https://github.com/mb-14/gomarkov which was not receiving PR merges or updates. This version contains some PRs from the original project, modifications, and is updated to be go mod compatible.

Installation

With go 1.13+

go get github.com/asciifaceman/regomarkov

Usage

package main

import (
	"github.com/mb-14/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)
	// or to have the chain provide a full sentence (if possible)
	txt, _ := chain.GenerateAll()
	fmt.Println(txt)

	//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

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

This section is empty.

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) GenerateAll

func (chain *Chain) GenerateAll() ([]string, error)

GenerateAll generates whole chain of text from scratch.

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 a 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 sequece

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