-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchewing.go
128 lines (102 loc) · 2.67 KB
/
chewing.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package libgochewing
import (
"bufio"
"errors"
"fmt"
"os"
"strings"
)
type Chewing struct {
phraseArray *PhraseArray
phraseTree *PhraseTree
phraseBKForest *PhraseBKForest
logger ChewingLogger
keyboardType int
}
type ChewingLogger interface {
Printf(format string, v ...interface{})
}
type ChewingParameters struct {
PhraseFile string
Logger ChewingLogger
}
type ChewingDefaultLogger struct{}
func New(params *ChewingParameters) (chewing *Chewing, err error) {
chewing = new(Chewing)
chewing.setupLogger(params)
err = chewing.setupPhraseArray(params)
if err != nil {
return nil, err
}
chewing.setupPhraseTree(params)
chewing.setupPhraseBKForest(params)
return chewing, nil
}
func (this *ChewingDefaultLogger) Printf(format string, v ...interface{}) {}
func (this *Chewing) setupLogger(params *ChewingParameters) {
if params.Logger == nil {
params.Logger = new(ChewingDefaultLogger)
}
this.logger = params.Logger
}
func (this *Chewing) setupPhraseArray(params *ChewingParameters) (err error) {
file, err := os.Open(params.PhraseFile)
if err != nil {
return err
}
defer file.Close()
this.phraseArray = newPhraseArray()
for scanner := bufio.NewScanner(file); scanner.Scan(); {
text := scanner.Text()
comment := strings.Index(text, "#")
if comment != -1 {
text = text[:comment]
}
text = strings.TrimSpace(text)
if text == "" {
continue
}
token := strings.Split(text, " ")
if len(token) < 3 {
return errors.New(fmt.Sprintf("`%s' is invalid in PhraseFile", text))
}
var frequency uint32
count, _ := fmt.Sscanf(token[1], "%d", &frequency)
if count != 1 {
return errors.New(fmt.Sprintf("`%s' is not a valid frequency", token[1]))
}
bopomofoSeq := token[2:]
phoneSeq := make([]uint16, len(bopomofoSeq))
for i := 0; i < len(bopomofoSeq); i++ {
phoneSeq[i], err = convertBopomofoToPhone(bopomofoSeq[i])
if err != nil {
return err
}
}
phrase, err := newPhrase(token[0], frequency)
if err != nil {
return err
}
this.phraseArray.insert(phrase, phoneSeq)
}
return nil
}
func (this *Chewing) setupPhraseTree(params *ChewingParameters) {
this.phraseTree = newPhraseTree()
for _, item := range this.phraseArray.array {
this.phraseTree.insert(item)
}
}
func (this *Chewing) setupPhraseBKForest(params *ChewingParameters) {
this.phraseBKForest = newPhraseBKForest()
for _, item := range this.phraseArray.array {
this.phraseBKForest.insert(item)
}
}
func (this *Chewing) SetKeyboardType(keyboard int) error {
if keyboard < KEYBOARD_MIN || keyboard > KEYBOARD_MAX {
return errors.New(fmt.Sprintf("illegal keyboard type %d", keyboard))
}
this.keyboardType = keyboard
return nil
}