-
Notifications
You must be signed in to change notification settings - Fork 0
/
cws.go
53 lines (47 loc) · 1.44 KB
/
cws.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
package gcws
import "fmt"
// CWS short for Chinese Word Segmentation,
// interface contains all behaviors for cws adapter.
type CWS interface {
// Parent is the interface of adapter parent structure.
Parent() interface{}
// SetConfig set cws configuration.
SetConfig(Config)
// Tokenize returns the string array of split.
Tokenize(string) []string
// ConfigTokenize returns the string array of split with cws configuration.
ConfigTokenize(Config, string) []string
}
// Instance is a function create a new CWS Instance
type Instance func(paths ...string) CWS
var (
lastName string
adapters = make(map[string]Instance)
)
// Register makes a cws adapter available by the adapter name.
// If Register is called twice with the same name or if driver is nil, it panics.
func Register(name string, adapter Instance) {
if adapter == nil {
panic("cws: Register adapter is nil")
}
if _, ok := adapters[name]; ok {
panic("cws: Register called twice for adapter " + name)
}
lastName = name
adapters[name] = adapter
}
// NewCWS Create a new cws driver by adapter name and dictionary paths.
// name is the name of adapter, returns last adapter if empty.
// paths are file paths of the dictionary.
func NewCWS(name string, paths ...string) (adapter CWS, err error) {
if name == "" {
name = lastName
}
f, ok := adapters[name]
if !ok {
err = fmt.Errorf("cws: unknown adapter name %q (forgot to import?)", name)
return
}
adapter = f(paths...)
return
}