spellchecker

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2024 License: MIT Imports: 13 Imported by: 0

README

Spellchecker

Yet another spellchecker written in go.

Features:

  • very small database: approximately 1mb for 30,000 unique words
  • average time to fix one word ~35μs
  • about 70-74% accuracy in Peter Norvig's test sets (see benchmarks)

Installation

$ go get -v github.com/f1monkey/spellchecker

Usage

func main() {
	// Create new instance
	sc, err := spellchecker.New(
		"abcdefghijklmnopqrstuvwxyz1234567890", // allowed symbols, other symbols will be ignored
		spellchecker.WithMaxErrors(2)
	)
	if err != nil {
		panic(err)
	}

	// Read data from any io.Reader
	in, err := os.Open("data/sample.txt")
	if err != nil {
		panic(err)
	}
	sc.AddFrom(in)

	// Add some more words
	sc.Add("lock", "stock", "and", "two", "smoking", "barrels")

	// Check if a word is correct
	result := sc.IsCorrect("coffee")
	fmt.Println(result) // true

	// Fix one word
	fixed, err := sc.Fix("awepon")
	if err != nil && !errors.Is(err, spellchecker.ErrUnknownWord) {
		panic(err)
	}
	fmt.Println(fixed) // weapon

	// Find max=10 suggestions for a word
	matches, err := sc.Suggest("rang", 10)
	if err != nil && !errors.Is(err, spellchecker.ErrUnknownWord) {
		panic(err)
	}
	fmt.Println(matches) // [range, orange]

	// Save data to any io.Writer
	out, err := os.Create("data/out.bin")
	if err != nil {
		panic(err)
	}
	sc.Save(out)

	// Load saved data from io.Reader
	in, err = os.Open("data/out.bin")
	if err != nil {
		panic(err)
	}
	sc, err = spellchecker.Load(in)
	if err != nil {
		panic(err)
	}
}

Benchmarks

Tests are based on data from Peter Norvig's article about spelling correction

Test set 1:
Running tool: /usr/local/go/bin/go test -benchmem -run=^$ -bench ^Benchmark_Norvig1$ github.com/f1monkey/spellchecker

goos: linux
goarch: amd64
pkg: github.com/f1monkey/spellchecker
cpu: AMD Ryzen 7 7840HS w/ Radeon 780M Graphics     
Benchmark_Norvig1-16    	     242	   4861057 ns/op	        74.07 success_percent	       200.0 success_words	       270.0 total_words	 1643485 B/op	   88241 allocs/op
PASS
ok  	github.com/f1monkey/spellchecker	3.343s
Test set 2:
Running tool: /usr/local/go/bin/go test -benchmem -run=^$ -bench ^Benchmark_Norvig2$ github.com/f1monkey/spellchecker

goos: linux
goarch: amd64
pkg: github.com/f1monkey/spellchecker
cpu: AMD Ryzen 7 7840HS w/ Radeon 780M Graphics     
Benchmark_Norvig2-16    	     150	   7226006 ns/op	        70.00 success_percent	       280.0 success_words	       400.0 total_words	 2389231 B/op	  129486 allocs/op
PASS
ok  	github.com/f1monkey/spellchecker	3.244s

Documentation

Index

Constants

View Source
const DefaultAlphabet = "abcdefghijklmnopqrstuvwxyz"
View Source
const DefaultMaxErrors = 2

Variables

View Source
var ErrUnknownWord = fmt.Errorf("unknown word")

Functions

This section is empty.

Types

type OptionFunc

type OptionFunc func(m *Spellchecker) error

OptionFunc option setter

func WithMaxErrors added in v0.1.0

func WithMaxErrors(maxErrors int) OptionFunc

WithMaxErrors set maxErrors, which is a max diff in bits betweeen the "search word" and a "dictionary word". i.e. one simple symbol replacement (problam => problem ) is a two-bit difference

func WithSplitter

func WithSplitter(f bufio.SplitFunc) OptionFunc

WithSplitter set splitter func for AddFrom() reader

type Spellchecker

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

func Load

func Load(reader io.Reader) (*Spellchecker, error)

Load reads spellchecker data from the provided reader and decodes it

func New

func New(alphabet string, opts ...OptionFunc) (*Spellchecker, error)

func (*Spellchecker) Add

func (m *Spellchecker) Add(words ...string)

Add adds provided words to dictionary

func (*Spellchecker) AddFrom

func (m *Spellchecker) AddFrom(input io.Reader) error

AddFrom reads input, splits it with spellchecker splitter func and adds words to dictionary

func (*Spellchecker) Fix

func (s *Spellchecker) Fix(word string) (string, error)

func (*Spellchecker) IsCorrect

func (s *Spellchecker) IsCorrect(word string) bool

IsCorrect check if provided word is in the dictionary

func (*Spellchecker) Save

func (m *Spellchecker) Save(w io.Writer) error

Save encodes spellchecker data and writes it to the provided writer

func (*Spellchecker) Suggest

func (s *Spellchecker) Suggest(word string, n int) ([]string, error)

Suggest find top n suggestions for the word

func (*Spellchecker) WithOpts

func (s *Spellchecker) WithOpts(opts ...OptionFunc) error

WithOpt set spellchecker options

Jump to

Keyboard shortcuts

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