headlines

package module
v0.0.0-...-b2e72c5 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2015 License: ISC Imports: 8 Imported by: 0

README

headlines

GoDoc

Markov Chain Generator focussed on generating headlines or single sentences.

What?

Markov Chains.

This package generates a Markov Chain from a data stream of line-separated phrases.

Usage

A minimal implementation follows. In this case we're generating 100 phrases, each of which being a maximum of 20 words long. When generating each phrase the previous two words are used to select the next word in the phrase.

package main

import (
	"fmt"
	"log"
	"os"

	"github.com/e-dard/dailymarkov/generator"
)

func main() {
	if fi, err = os.Open("/tmp/foo.txt"); err != nil {
		log.Fatal(err)
	}
	defer fi.Close()

	chain := generator.NewChain(2)
	if err := chain.Build(fi); err != nil {
		log.Fatal(err)
	}

	for i := 0; i < 100; i++ {
		text, err := chain.Generate(20)
		if err != nil {
			log.Fatal(err)
		}
		fmt.Println(text)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Chain

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

Chain represents a Markov Chain.

When a Chain is built, it maintains the state space and state transistions in a map of indexes, where each index refers to an index of a corresponding slice of tokens.

The Chain also contains a slice of indexes for the first token found in every sentence in the corpus. These are used to begin generated phrases.

To map indexes to the actual tokens Indexes are used.

func NewChain

func NewChain(l int) *Chain

NewChain creates a new Chain.

A Chain uses a prefix length l to determine how many tokens (words) to consider when deciding on the next token in a generated phrase.

func (*Chain) Build

func (c *Chain) Build(r io.Reader) error

Build consumes from a reader and first builds an index of all tokens read. Then is re-reads from the reader using the built index to construct a mapping between prefixes and suffixes.

The underlying data stream should contain phrases separated by \n.

func (*Chain) Generate

func (c *Chain) Generate(l int) (string, error)

Generate uses the Markov chain to generate a phrase with a maximum length of l.

func (*Chain) MustGenerate

func (c *Chain) MustGenerate(length int) string

MustGenerate panics if Generate returns an error.

Jump to

Keyboard shortcuts

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