forked from f1monkey/spellchecker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alphabet.go
59 lines (48 loc) · 1.26 KB
/
alphabet.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
54
55
56
57
58
59
package spellchecker
import "fmt"
type Alphabet struct {
// Letters to use in alphabet. Duplicates are not allowed
Letters string
// Length bit count to encode alphabet
// If it is less than rune count in letters then
// several letters will be encoded as one bit.
// It decreases database size for a bit
// but drastically reduces search performance in large dictionaries
Length int
}
var DefaultAlphabet = Alphabet{
Letters: "abcdefghijklmnopqrstuvwxyz",
Length: 26,
}
type alphabet map[rune]uint32
// newAlphabet create a new alphabet instance
func newAlphabet(str string, length int) (alphabet, error) {
runes := []rune(str)
if len(runes) == 0 {
return nil, fmt.Errorf("unable to use empty string as alphabet")
}
if length > 63 {
return nil, fmt.Errorf("alphabets longer than 63 are not supported (yet?)")
}
result := make(alphabet, length)
for i, s := range runes {
index := i % length
if _, ok := result[s]; ok {
return nil, fmt.Errorf("duplicate symbol %q at position %d", s, i)
}
result[s] = uint32(index)
}
return result, nil
}
func (a alphabet) encode(word []rune) bitmap {
var b bitmap
for _, letter := range word {
if index, ok := a[letter]; ok {
b.or(index)
}
}
return b
}
func (a alphabet) len() int {
return len(a)
}