/
wubi.go
60 lines (50 loc) · 1.23 KB
/
wubi.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
package wubi
import (
"bytes"
"slices"
"strings"
)
type Entry struct {
Word string
Code string
Rank int
}
type Format interface {
GetName() string
GetID() string
GetCanMarshal() bool
Marshal([]*Entry, bool) []byte
GetHasRank() bool
Unmarshal(*bytes.Reader) []*Entry
GetKind() int
}
type Template struct {
Name string // 名称
ID string // 唯一 ID
CanMarshal bool // 是否可以序列化
HasRank bool // 是否有 Rank
}
func (t *Template) GetName() string { return t.Name }
func (t *Template) GetID() string { return t.ID }
func (t *Template) GetCanMarshal() bool { return t.CanMarshal }
func (t *Template) Marshal([]*Entry, bool) []byte { return nil }
func (t *Template) GetHasRank() bool { return t.HasRank }
func (t *Template) GetKind() int { return WUBI }
const WUBI = 2
var FormatList = make([]Format, 0, 20)
func GenRank(di []*Entry) []*Entry {
codeMap := make(map[string]int)
for i := range di {
codeMap[di[i].Code]++
di[i].Rank = codeMap[di[i].Code]
}
return di
}
func New(format string) Format {
for _, v := range FormatList {
if slices.Contains(strings.Split(v.GetID(), ","), format) {
return v
}
}
return nil
}