Documentation ¶
Overview ¶
Package spell provides functions for spell check and correction. It wraps https://github.com/sajari/fuzzy as the core spelling engine.
A single globally-usable spelling dictionary is managed.
Index ¶
- Constants
- Variables
- func Asset(name string) ([]byte, error)
- func AssetDir(name string) ([]string, error)
- func AssetInfo(name string) (os.FileInfo, error)
- func AssetNames() []string
- func CheckIgnore(word string) bool
- func CheckLexLine(src []rune, tags lex.Line) lex.Line
- func CheckWord(word string) ([]string, bool)
- func Complete(s string) []string
- func Edits1(word string) []string
- func IgnoreWord(word string)
- func Initialized() bool
- func LearnWord(word string)
- func Levenshtein(a, b *string) int
- func ModTime(path string) (time.Time, error)
- func MustAsset(name string) []byte
- func Open(path string) error
- func OpenAsset(fname string) error
- func OpenCheck() error
- func OpenDefault() error
- func ResetLearnTime()
- func RestoreAsset(dir, name string) error
- func RestoreAssets(dir, name string) error
- func SampleEnglish() []string
- func Save(filename string) error
- func SaveIfLearn() error
- func Train(file os.File, new bool) (err error)
- func UnLearnWord(word string)
- type Autos
- type Counts
- type Method
- type Model
- func (md *Model) Autocomplete(input string) ([]string, error)
- func (md *Model) CheckKnown(input string, correct string) bool
- func (md *Model) Delete(term string)
- func (md *Model) EditsMulti(term string, depth int) []string
- func (md *Model) Init() *Model
- func (md *Model) Potentials(input string, exhaustive bool) map[string]*Potential
- func (md *Model) Save(filename string) error
- func (md *Model) SaveLight(filename string) error
- func (md *Model) SetCount(term string, count int, suggest bool)
- func (md *Model) SetDepth(val int)
- func (md *Model) SetDivergenceThreshold(val int)
- func (md *Model) SetThreshold(val int)
- func (md *Model) SetUseAutocomplete(val bool)
- func (md *Model) SpellCheck(input string) string
- func (md *Model) SpellCheckSuggestions(input string, n int) []string
- func (md *Model) Suggestions(input string, exhaustive bool) []string
- func (md *Model) Train(terms []string)
- func (md *Model) TrainQuery(term string)
- func (md *Model) TrainWord(term string)
- func (md *Model) WriteTo(w io.Writer) error
- type Pair
- type Potential
Constants ¶
const ( SpellDepthDefault = 2 SpellThresholdDefault = 5 SuffDivergenceThresholdDefault = 100 )
const ( MethodIsWord Method = 0 MethodSuggestMapsToInput = 1 MethodInputDeleteMapsToDict = 2 MethodInputDeleteMapsToSuggest = 3 )
const SaveAfterLearnIntervalSecs = 20
SaveAfterLearnIntervalSecs is number of seconds since file has been opened / saved above which model is saved after learning.
Variables ¶
var (
Ignore = map[string]struct{}{}
)
Functions ¶
func Asset ¶
Asset loads and returns the asset for the given name. It returns an error if the asset could not be found or could not be loaded.
func AssetDir ¶
AssetDir returns the file names below a certain directory embedded in the file by go-bindata. For example if you run go-bindata on data/... and data contains the following hierarchy:
data/ foo.txt img/ a.png b.png
then AssetDir("data") would return []string{"foo.txt", "img"} AssetDir("data/img") would return []string{"a.png", "b.png"} AssetDir("foo.txt") and AssetDir("notexist") would return an error AssetDir("") will return []string{"data"}.
func AssetInfo ¶
AssetInfo loads and returns the asset info for the given name. It returns an error if the asset could not be found or could not be loaded.
func AssetNames ¶
func AssetNames() []string
AssetNames returns the names of the assets. nolint: deadcode
func CheckIgnore ¶ added in v0.9.14
CheckIgnore returns true if the word is found in the Ignore list
func CheckLexLine ¶ added in v0.9.14
CheckLexLine returns the Lex regions for any words that are misspelled within given line of text with existing Lex tags -- automatically excludes any Code token regions (see token.IsCode). Token is set to token.TextSpellErr on returned Lex's
func Edits1 ¶ added in v1.0.0
Edits1 creates a set of terms that are 1 char delete from the input term
func Initialized ¶
func Initialized() bool
Initialized returns true if the model has been loaded or created anew
func LearnWord ¶
func LearnWord(word string)
LearnWord adds a single word to the corpus: this is deterministic and we set the threshold to 1 to make it learn it immediately.
func Levenshtein ¶ added in v1.0.0
Calculate the Levenshtein distance between two strings
func MustAsset ¶
MustAsset is like Asset but panics when Asset would return an error. It simplifies safe initialization of global variables. nolint: deadcode
func OpenCheck ¶ added in v0.9.14
func OpenCheck() error
OpenCheck checks if the current file has been modified since last open time and re-opens it if so -- call this prior to checking.
func OpenDefault ¶ added in v0.9.14
func OpenDefault() error
OpenDefault loads the default spelling file. TODO: need different languages obviously!
func ResetLearnTime ¶ added in v0.9.14
func ResetLearnTime()
func RestoreAsset ¶
RestoreAsset restores an asset under the given directory
func RestoreAssets ¶
RestoreAssets restores an asset under the given directory recursively
func SampleEnglish ¶ added in v1.0.0
func SampleEnglish() []string
func Save ¶
Save saves the spelling model which includes the data and parameters note: this will overwrite any existing file -- be sure to have opened the current file before making any changes.
func SaveIfLearn ¶ added in v0.9.14
func SaveIfLearn() error
SaveIfLearn saves the spelling model to file path that was used in last Open command, if learning has occurred since last save / open. If no changes also checks if file has been modified and opens it if so.
func UnLearnWord ¶ added in v1.0.9
func UnLearnWord(word string)
UnLearnWord removes word from dictionary -- in case accidentally added
Types ¶
type Model ¶ added in v1.0.0
type Model struct { Data map[string]*Counts `json:"data"` Maxcount int `json:"maxcount"` Suggest map[string][]string `json:"suggest"` Depth int `json:"depth"` Threshold int `json:"threshold"` UseAutocomplete bool `json:"autocomplete"` SuffDivergence int `json:"-"` SuffDivergenceThreshold int `json:"suff_threshold"` SuffixArr *suffixarray.Index `json:"-"` SuffixArrConcat string `json:"-"` sync.RWMutex }
Model is the full data model
func FromReader ¶ added in v1.0.0
FromReader loads a model from a Reader
func (*Model) Autocomplete ¶ added in v1.0.0
For a given string, autocomplete using the suffix array model
func (*Model) CheckKnown ¶ added in v1.0.0
Test an input, if we get it wrong, look at why it is wrong. This function returns a bool indicating if the guess was correct as well as the term it is suggesting. Typically this function would be used for testing, not for production
func (*Model) EditsMulti ¶ added in v1.0.0
Edits at any depth for a given term. The depth of the model is used
func (*Model) Potentials ¶ added in v1.0.0
Return the raw potential terms so they can be ranked externally to this package
func (*Model) SaveLight ¶ added in v1.0.0
Save a spelling model to disk, but discard all entries less than the threshold number of occurrences Much smaller and all that is used when generated as a once off, but not useful for incremental usage
func (*Model) SetCount ¶ added in v1.0.0
Manually set the count of a word. Optionally trigger the creation of suggestion keys for the term. This function lets you build a model from an existing dictionary with word popularity counts without needing to run "TrainWord" repeatedly
func (*Model) SetDepth ¶ added in v1.0.0
Change the default depth value of the model. This sets how many character differences are indexed. The default is 2.
func (*Model) SetDivergenceThreshold ¶ added in v1.0.0
Optionally set the suffix array divergence threshold. This is the number of query training steps between rebuilds of the suffix array. A low number will be more accurate but will use resources and create more garbage.
func (*Model) SetThreshold ¶ added in v1.0.0
Change the default threshold of the model. This is how many times a term must be seen before suggestions are created for it
func (*Model) SetUseAutocomplete ¶ added in v1.0.0
Optionally disabled suffixarray based autocomplete support
func (*Model) SpellCheck ¶ added in v1.0.0
Return the most likely correction for the input term
func (*Model) SpellCheckSuggestions ¶ added in v1.0.0
Return the most likely corrections in order from best to worst
func (*Model) Suggestions ¶ added in v1.0.0
For a given input string, suggests potential replacements
func (*Model) TrainQuery ¶ added in v1.0.0
TrainQuery using a search query term. This builds a second popularity index of terms used to search, as opposed to generally occurring in corpus text