bayesian

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2020 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Class

type Class string

Class defines a class that the classifier will filter: C = {C_1, ..., C_n}. You should define your classes as a set of constants, for example as follows:

const (
    Good Class = "Good"
    Bad Class = "Bad
)

Class values should be unique.

type Classifier

type Classifier struct {
	Classes  []Class
	DocCount map[Class]int64
	Words    WordsTrie
}

Classifier implements the Naive Bayesian Classifier.

func NewClassifier

func NewClassifier(classes ...Class) (*Classifier, error)

NewClassifier returns a new classifier. The classes provided should be at least 2 in number and unique.

func NewClassifierFromFile

func NewClassifierFromFile(name string) (c *Classifier, err error)

NewClassifierFromFile loads an existing classifier from file. The classifier was previously saved with a call to c.WriteToFile(string).

func NewClassifierFromReader

func NewClassifierFromReader(r io.Reader) (c *Classifier, err error)

NewClassifierFromReader deserializes of a Gob encoded classifier.

func (*Classifier) Learn

func (c *Classifier) Learn(document []string, class Class)

Learn supervised learns the given document.

func (*Classifier) ProbScores

func (c *Classifier) ProbScores(doc []string) map[Class]float64

ProbScores calculates probabilities.

func (*Classifier) Write

func (c *Classifier) Write(w io.Writer) (err error)

WriteTo serializes this classifier to GOB and write to Writer.

func (*Classifier) WriteToFile

func (c *Classifier) WriteToFile(name string) (err error)

WriteToFile serializes this classifier to a file.

type WordData

type WordData struct {
	Total int64
	Count map[Class]int64
}

WordData contains the total word count for each classes and total word count.

type WordsTrie

type WordsTrie struct {
	WordData *WordData
	Children map[rune]*WordsTrie
}

WordsTrie is the trie data structure which contains WordData for words.

func NewWordsTrie

func NewWordsTrie() WordsTrie

NewWordsTrie returns new WordsTrie.

func (*WordsTrie) Get

func (trie *WordsTrie) Get(word string) *WordData

Get returns the WordData of the given word if it exists.

func (*WordsTrie) GetOrNew

func (trie *WordsTrie) GetOrNew(word string) *WordData

GetOrNew returns the WordData of the given word. If WordData doesn't exist, make it and return.

Jump to

Keyboard shortcuts

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