Help us shape the future of Go! Take the 2026 Go Developer Survey

spellcheck

package module
v0.0.0-...-169d6d2 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2015 License: MIT Imports: 5 Imported by: 0

README

go-spellcheck

This is an implementation of Peter Norvig's spell correction algorithm in Golang. You can read more about the algorithm here. http://norvig.com/spell-correct.html

This implementation is multithread-safe and looks little like Norvig's implementation, but the underlying algorithm is the same.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultAlphabet = strings.Split("abcdefghijklmnopqrstuvwxyz", "")
View Source
var DefaultDelimiters = " _=.?!,\n\"'"

Functions

This section is empty.

Types

type CheckCmd

type CheckCmd struct {
	Word     string
	Alphabet []string
	Result   chan *CheckResult
}

func (*CheckCmd) GetBestMatch

func (cmd *CheckCmd) GetBestMatch(d *Dict, variants []string) (best_string string, best_score int)

GetBestMatch() will take a set of variants and return the one with the highest score per the dict.Known() function

func (*CheckCmd) Run

func (cmd *CheckCmd) Run(d *Dict)

Run() is the logic that will be executed by the Dict worker thread. This should not be called directly. It will determine if the provided word is in the dictionary, and, if it isn't, it will generate a set of variants using Variants(), and will send the best result down cmd.Result in the form of a CheckResult

func (*CheckCmd) Variants

func (cmd *CheckCmd) Variants(word string) []string

Variants() will take a single word and generate a set of possible permutations

type CheckResult

type CheckResult struct {
	IsCorrect  bool
	Correction string
	Score      int
}

type Dict

type Dict struct {
	Cmds chan DictCmd
	Dict map[string]int
}

Dict is the central struct for this library. It maintains the worker goroutines, and contains the internal data. All member functions of dict are thread safe.

func NewDict

func NewDict() *Dict

NewDict() will create and initialize a new Dictionary, and start the worker goroutine

func (*Dict) CheckSentence

func (d *Dict) CheckSentence(sentence string) SentenceCorrectionSet

func (*Dict) CheckWord

func (d *Dict) CheckWord(word string) *CheckResult

CheckWord() is a primitive used to determine if a single word is spelled correctly

func (*Dict) Destroy

func (d *Dict) Destroy()

Destroy() will terminate all worker goroutines. Once you release all references to the object, the internal data will clean up, too.

func (*Dict) Known

func (d *Dict) Known(word string) int

Known() will return an integer value for whether the word exists in the dictionary. 0 means that the word was not found.

func (*Dict) PushQueue

func (d *Dict) PushQueue(cmd DictCmd)

PushQueue() will push a DictCmd task into the command queue. This is not a guarantee that it will be executed now or next, but that it will be executed eventually

func (*Dict) TrainFile

func (d *Dict) TrainFile(filename string) (error, chan bool)

TrainFile() accepts a path to a file, opens that file, and will create a TrainCmd and queue it.

func (*Dict) TrainReader

func (d *Dict) TrainReader(r io.Reader) chan bool

TrainReader() is identical to TrainDict() except a io.Reader is provided instead of a file path.

type DictCmd

type DictCmd interface {
	Run(d *Dict)
}

DictCmd is an interface that all internal commands intending on modifying internal Dict data must fulfill. This is to ensure multithread safety

type SentenceCorrection

type SentenceCorrection struct {
	BeginAt int
	EndAt   int
	Result  *CheckResult
}

func (*SentenceCorrection) Apply

func (sc *SentenceCorrection) Apply(s string) string

type SentenceCorrectionSet

type SentenceCorrectionSet []SentenceCorrection

func (SentenceCorrectionSet) ApplyAll

func (scs SentenceCorrectionSet) ApplyAll(s string) string

type TrainCmd

type TrainCmd struct {
	Reader     io.Reader
	Delimiters string
	Done       chan bool
}

TrainCmd is a command ran within a dictionary's goroutine that populates the internal dictionary with words.

func (*TrainCmd) Run

func (cmd *TrainCmd) Run(d *Dict)

Run() is the internal TrainCmd function to fulfil the DictCmd interface. This is executed within the Dict worker goroutine, and should not be called directly.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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