-
Notifications
You must be signed in to change notification settings - Fork 0
/
shamoji.go
55 lines (44 loc) · 865 Bytes
/
shamoji.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
package shamoji
import (
"fmt"
"sync"
"github.com/tchap/go-patricia/patricia"
"golang.org/x/text/unicode/norm"
)
func NewWordChecker(form norm.Form, words []string) (wc *WordChecker, err error) {
if len(words) == 0 {
err = fmt.Errorf("may not be zero length")
}
wc = &WordChecker{
Form: form,
Trie: generateTrie(form, words),
}
return
}
func generateTrie(form norm.Form, words []string) (trie *patricia.Trie) {
var wg sync.WaitGroup
wg.Add(len(words))
finChan := make(chan bool)
go func() {
wg.Wait()
finChan <- true
}()
trie = patricia.NewTrie()
wordChan := make(chan []byte, len(words))
for _, w := range words {
go func(word string) {
defer wg.Done()
wordChan <- form.Bytes([]byte(word))
}(w)
}
LOOP:
for {
select {
case b := <-wordChan:
trie.Set(b, true)
case <-finChan:
break LOOP
}
}
return
}