You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
N.B. This library is still in early development and may change.
Overview
package main
import (
"fmt""github.com/eskriett/spell"
)
funcmain() {
// Create a new instance of spells:=spell.New()
// Add words to the dictionary. Words require a frequency, but can have// other arbitrary metadata associated with thems.AddEntry(spell.Entry{
Frequency: 100,
Word: "two",
WordData: spell.WordData{
"type": "number",
},
})
s.AddEntry(spell.Entry{
Frequency: 1,
Word: "town",
WordData: spell.WordData{
"type": "noun",
},
})
// Lookup a misspelling, by default the "best" suggestion will be returnedsuggestions, _:=s.Lookup("twon")
fmt.Println(suggestions)
// -> [two]suggestion:=suggestions[0]
// Get the frequency from the suggestionfmt.Println(suggestion.Frequency)
// -> 100// Get metadata from the suggestionfmt.Println(suggestion.WordData["type"])
// -> number// Get multiple suggestions during lookupsuggestions, _=s.Lookup("twon", spell.SuggestionLevel(spell.LevelAll))
fmt.Println(suggestions)
// -> [two, town]// Save the dictionarys.Save("dict.spell")
// Load the dictionarys2, _:=spell.Load("dict.spell")
suggestions, _=s2.Lookup("twon", spell.SuggestionLevel(spell.LevelAll))
fmt.Println(suggestions)
// -> [two, town]// Spell supports word segmentations3:=spell.New()
s3.AddEntry(spell.Entry{Frequency: 1, Word: "the"})
s3.AddEntry(spell.Entry{Frequency: 1, Word: "quick"})
s3.AddEntry(spell.Entry{Frequency: 1, Word: "brown"})
s3.AddEntry(spell.Entry{Frequency: 1, Word: "fox"})
segmentResult, _:=s3.Segment("thequickbrownfox")
fmt.Println(segmentResult)
// -> the quick brown fox// Spell supports multiple dictionariess4:=spell.New()
s4.AddEntry(spell.Entry{Word: "épeler"}, spell.DictionaryName("french"))
suggestions, _=s4.Lookup("épeler", spell.DictionaryOpts(
spell.DictionaryName("french"),
))
fmt.Println(suggestions)
// -> [épeler]
}
Credits
Spell makes use of a symmetric delete algorithm and is loosely based on the
SymSpell implementation.
About
Spelling correction and string segmentation written in Go