-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdictionary.go
67 lines (58 loc) · 1.4 KB
/
dictionary.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
package moji
import (
"strings"
)
// Dictionary defines an interface for mapping between a string and index
type Dictionary interface {
Encode([]MaybeIndex) string
Decode(string) []MaybeIndex
}
type defaultDictionary []string
func (d defaultDictionary) encode(m MaybeIndex) string {
if m.i < 0 || m.i > len(d) {
return m.s
}
return d[m.i]
}
func (d defaultDictionary) decode(s string) (MaybeIndex, string) {
for i, p := range d {
if strings.HasPrefix(s, p) {
return MaybeIndex{i: i, s: p}, strings.Replace(s, p, "", 1)
}
}
rs := []rune(s)
head := string(rs[:1])
tail := string(rs[1:])
return MaybeIndex{i: -1, s: head}, tail
}
func (d defaultDictionary) Encode(ms []MaybeIndex) string {
s := make([]byte, 0)
for _, m := range ms {
s = append(s, d.encode(m)...)
}
return string(s)
}
func (d defaultDictionary) Decode(s string) []MaybeIndex {
ms := make([]MaybeIndex, 0)
var m MaybeIndex
for len(s) != 0 {
m, s = d.decode(s)
ms = append(ms, m)
}
return ms
}
// NewDictionary creates a dictionary from the given string slice
func NewDictionary(d []string) Dictionary {
return defaultDictionary(d)
}
// NewRangeDictionary creates a dictionary from the given range
func NewRangeDictionary(s, e rune) Dictionary {
if s > e {
panic("NewRangeDictionary: range is invaid")
}
d := make([]string, 0)
for r := s; r != e; r++ {
d = append(d, string([]rune{r}))
}
return NewDictionary(d)
}