-
Notifications
You must be signed in to change notification settings - Fork 7
/
bigram_dict.go
64 lines (50 loc) · 949 Bytes
/
bigram_dict.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
60
61
62
63
64
package gotokenizer
import (
"bufio"
"github.com/xujiajun/utils/strconv2"
"io"
"os"
"strings"
)
// BigramDict records dictPath and bigram records
type BigramDict struct {
dictPath string
isLoaded bool
maxF int
records map[string]int
}
// NewBigramDict returns a newly initialized BigramDict object
func NewBigramDict(dictPath string) *BigramDict {
return &BigramDict{
dictPath: dictPath,
records: make(map[string]int),
}
}
// Load returns Bigram Dict records
func (bd *BigramDict) Load() error {
if bd.isLoaded {
return nil
}
fi, err := os.Open(bd.dictPath)
if err != nil {
return err
}
defer fi.Close()
br := bufio.NewReader(fi)
for {
a, _, c := br.ReadLine()
if c == io.EOF {
break
}
res := strings.Split(string(a), " ")
f := res[1]
fi, _ := strconv2.StrToInt(f)
strKey := res[0]
bd.records[strKey] = fi
if bd.maxF < fi {
bd.maxF = fi
}
}
bd.isLoaded = true
return nil
}