forked from snapp-incubator/go-symspell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymspell.go
53 lines (47 loc) · 1.7 KB
/
symspell.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package symspell
import (
"log"
"github.com/snapp-incubator/go-symspell/internal"
"github.com/snapp-incubator/go-symspell/pkg/items"
"github.com/snapp-incubator/go-symspell/pkg/options"
"github.com/snapp-incubator/go-symspell/pkg/verbosity"
)
func NewSymSpell(opt ...options.Options) SymSpell {
symspell, err := internal.NewSymSpell(opt...)
if err != nil {
log.Fatal("[ERROR] ", err)
}
return symspell
}
// NewSymSpellWithLoadDictionary used when want Lookup only
func NewSymSpellWithLoadDictionary(dirPath string, termIndex, countIndex int, opt ...options.Options) SymSpell {
symspell := NewSymSpell(opt...)
ok, err := symspell.LoadDictionary(dirPath, termIndex, countIndex, " ")
if err != nil {
log.Fatal("[Error] ", err)
}
if !ok {
log.Fatal("[Error] loading dictionary has been failed")
}
return symspell
}
func NewSymSpellWithLoadBigramDictionary(vocabDirPath, bigramDirPath string, termIndex, countIndex int, opt ...options.Options) SymSpell {
symspell := NewSymSpell(opt...)
ok, err := symspell.LoadDictionary(vocabDirPath, termIndex, countIndex, " ")
if err != nil || !ok {
log.Fatal("[Error] ", err)
}
ok, err = symspell.LoadBigramDictionary(bigramDirPath, termIndex, countIndex+1, "")
if err != nil || !ok {
if bigramDirPath != "" {
log.Println("[Error] ", err)
}
}
return symspell
}
type SymSpell interface {
Lookup(phrase string, verbosity verbosity.Verbosity, maxEditDistance int) ([]items.SuggestItem, error)
LookupCompound(phrase string, maxEditDistance int) *items.SuggestItem
LoadBigramDictionary(corpusPath string, termIndex, countIndex int, separator string) (bool, error)
LoadDictionary(corpusPath string, termIndex int, countIndex int, separator string) (bool, error)
}