-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdecode.go
57 lines (46 loc) · 1.29 KB
/
decode.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
package main
import (
"encoding/json"
"io/ioutil"
bits "github.com/siongui/go-succinct-data-structure-trie"
)
type TrieData struct {
EncodedData string
NodeCount uint
RankDirectoryData string
}
func loadTrie(filePath string) (td TrieData, err error) {
b, err := ioutil.ReadFile(filePath)
if err != nil {
return
}
err = json.Unmarshal(b, &td)
return
}
func main() {
// Set alphabet of words
bits.SetAllowedCharacters("abcdeghijklmnoprstuvyāīūṁṃŋṇṅñṭḍḷ…'’° -")
// Note that you must include space " " in your alphabet if you do not
// use the default alphabet.
// default alphabet is [a-z ], i.e.,
// bits.SetAllowedCharacters("abcdefghijklmnopqrstuvwxyz ")
td, err := loadTrie("trie.json")
if err != nil {
panic(err)
}
println(td.EncodedData)
println(td.NodeCount)
println(td.RankDirectoryData)
// decode: build frozen succinct trie
ft := bits.FrozenTrie{}
ft.Init(td.EncodedData, td.RankDirectoryData, td.NodeCount)
// decode: look up words
println(ft.Lookup("sacca"))
println(ft.Lookup("sacc"))
println(ft.Lookup("dhamma"))
// decode: words suggestion (find words that start with "prefix")
// find words starts with "a", max number of returned words is 10
for _, word := range ft.GetSuggestedWords("a", 10) {
println(word)
}
}