-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopencc.go
186 lines (175 loc) · 4.01 KB
/
opencc.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package opencc
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"path/filepath"
"strings"
)
var (
// Dir .
Dir string
configPath = "config"
dictionaryPath = "dictionary"
defaultDir = `./`
defaultConversion = "s2twp"
)
// Group holds a sequence of dicts
type Group struct {
Files []string
Dicts []*dict
}
func (g *Group) String() string {
return fmt.Sprintf("%+v", g.Files)
}
// OpenCC contains the converter
type openCC struct {
Conversion string
Description string
DictChains []*Group
}
var conversions = map[string]*openCC{
"hk2s": {}, "s2hk": {}, "s2t": {}, "s2tw": {}, "s2twp": {},
"t2hk": {}, "t2s": {}, "t2tw": {}, "tw2s": {}, "tw2sp": {},
}
// Init construct an instance of OpenCC.
func Init() {
if Dir == "" {
Dir = defaultDir
}
for k, v := range conversions {
if err := v.dict(k); err != nil {
panic("init dict")
}
}
}
// Convert .
func Convert(ctx context.Context, in string) (string, error) {
return convert(in, defaultConversion)
}
// convert string from Simplified Chinese to Traditional Chinese or vice versa
func convert(in, conversion string) (string, error) {
cc := conversions["s2t"]
for _, group := range cc.DictChains {
r := []rune(in)
var tokens []string
for i := 0; i < len(r); {
s := r[i:]
var token string
max := 0
for _, dict := range group.Dicts {
ret, err := dict.prefixMatch(string(s))
if err != nil {
return "", err
}
if len(ret) > 0 {
o := ""
for k, v := range ret {
if len(k) > max {
max = len(k)
token = v[0]
o = k
}
}
i += len([]rune(o))
break
}
}
if max == 0 { //no match
token = string(r[i])
i++
}
tokens = append(tokens, token)
}
in = strings.Join(tokens, "")
}
return in, nil
}
func (cc *openCC) dict(conversion string) error {
cc.Conversion = conversion
configFile := filepath.Join(Dir, configPath, conversion+".json")
body, err := ioutil.ReadFile(configFile)
if err != nil {
return err
}
var m interface{}
if err = json.Unmarshal(body, &m); err != nil {
return err
}
config := m.(map[string]interface{})
name, ok := config["name"]
if !ok {
return fmt.Errorf("name not found in %s", configFile)
}
cc.Description = name.(string)
dictChain, ok := config["conversion_chain"].([]interface{})
if !ok {
return fmt.Errorf("conversion_chain not found in %s", configFile)
}
for _, v := range dictChain {
d, ok := v.(map[string]interface{})
if !ok {
return fmt.Errorf("should be map inside conversion_chain")
}
dictMap, ok := d["dict"]
if !ok {
return fmt.Errorf("should have dict inside conversion_chain")
}
if dict, ok := dictMap.(map[string]interface{}); ok {
group, err := cc.group(dict)
if err != nil {
return err
}
cc.DictChains = append(cc.DictChains, group)
}
}
return nil
}
func (cc *openCC) group(d map[string]interface{}) (*Group, error) {
t, ok := d["type"]
if !ok {
return nil, fmt.Errorf("type not found in %+v", d)
}
if typ, ok := t.(string); ok {
ret := &Group{}
switch typ {
case "group":
ds, ok := d["dicts"]
if !ok {
return nil, fmt.Errorf("no dicts field found")
}
dicts, ok := ds.([]interface{})
if !ok {
return nil, fmt.Errorf("dicts field invalid")
}
for _, dict := range dicts {
d, ok := dict.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("dicts items invalid")
}
group, err := cc.group(d)
if err != nil {
return nil, err
}
ret.Files = append(ret.Files, group.Files...)
ret.Dicts = append(ret.Dicts, group.Dicts...)
}
case "txt":
file, ok := d["file"]
if !ok {
return nil, fmt.Errorf("no file field found")
}
daDict, err := buildFromFile(filepath.Join(Dir, dictionaryPath, file.(string)))
if err != nil {
return nil, err
}
ret.Files = append(ret.Files, file.(string))
ret.Dicts = append(ret.Dicts, daDict)
default:
return nil, fmt.Errorf("type should be txt or group")
}
return ret, nil
}
return nil, fmt.Errorf("type should be string")
}