-
Notifications
You must be signed in to change notification settings - Fork 1
/
definputtextplugin.go
187 lines (171 loc) · 4.49 KB
/
definputtextplugin.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
187
package gosudachi
import (
"fmt"
"io"
"os"
"strings"
"unicode"
"unicode/utf8"
"github.com/msnoigrs/gosudachi/data"
"github.com/msnoigrs/gosudachi/internal/lnreader"
"golang.org/x/text/unicode/norm"
)
type DefaultInputTextPluginConfig struct {
RewriteDef string
}
type DefaultInputTextPlugin struct {
config *DefaultInputTextPluginConfig
rewriteDef string
ignoreNormalizeMap map[rune]bool
keyLengths map[rune]int
replaceCharMap map[string][]rune
}
func NewDefaultInputTextPlugin(config *DefaultInputTextPluginConfig) *DefaultInputTextPlugin {
if config == nil {
config = &DefaultInputTextPluginConfig{}
}
return &DefaultInputTextPlugin{
config: config,
ignoreNormalizeMap: map[rune]bool{},
keyLengths: map[rune]int{},
replaceCharMap: map[string][]rune{},
}
}
func (p *DefaultInputTextPlugin) GetConfigStruct() interface{} {
if p.config == nil {
p.config = &DefaultInputTextPluginConfig{}
}
return p.config
}
func (p *DefaultInputTextPlugin) SetUp() error {
if p.rewriteDef == "" {
p.rewriteDef = p.config.RewriteDef
}
p.config = nil
if p.ignoreNormalizeMap == nil {
p.ignoreNormalizeMap = map[rune]bool{}
}
if p.keyLengths == nil {
p.keyLengths = map[rune]int{}
}
if p.replaceCharMap == nil {
p.replaceCharMap = map[string][]rune{}
}
err := p.readRewriteLists(p.rewriteDef)
if err != nil {
return fmt.Errorf("DefaultInputTextPlugin: %s", err)
}
return nil
}
func (p *DefaultInputTextPlugin) getKeyLength(key rune, def int) int {
l, ok := p.keyLengths[key]
if !ok {
return def
}
return l
}
func (p *DefaultInputTextPlugin) Rewrite(builder *InputTextBuilder) error {
runes := builder.GetText()
runelen := len(runes)
utf8buf := make([]byte, 8, 8)
offset := 0
nextOffset := 0
TEXTLOOP:
for i := 0; i < runelen; i++ {
offset += nextOffset
nextOffset = 0
// 1. replace char without normalize
for l := minInt(p.getKeyLength(runes[i], 0), runelen-i); l > 0; l-- {
replace, ok := p.replaceCharMap[string(runes[i:i+l])]
if ok {
builder.Replace(i+offset, i+l+offset, replace)
nextOffset += len(replace) - l
i += l - 1
continue TEXTLOOP
}
}
// 2. normalize
original := runes[i]
// 2-1. capital alphabet (not only latin but greek, cyrillic, etc) -> small
lower := unicode.ToLower(original)
var replace []rune
_, ok := p.ignoreNormalizeMap[lower]
if ok {
if original == lower {
continue
}
replace = []rune{lower}
} else {
// 2-2. normalize (except in ignoreNormalize)
// e.g. full-width alphabet -> half-width / ligature / etc.
size := utf8.EncodeRune(utf8buf, lower)
replace = []rune(string(norm.NFKC.Bytes(utf8buf[:size])))
}
nextOffset = len(replace) - 1
if len(replace) != 1 || original != replace[0] {
builder.Replace(i+offset, i+1+offset, replace)
}
}
return nil
}
func minInt(a, b int) int {
if a < b {
return a
}
return b
}
func (p *DefaultInputTextPlugin) readRewriteLists(rewriteDef string) error {
var rewriteDefReader io.Reader
if rewriteDef != "" {
rewriteDefFd, err := os.OpenFile(rewriteDef, os.O_RDONLY, 0644)
if err != nil {
return fmt.Errorf("DefaultInputTextPlugin: %s: %s", err, rewriteDef)
}
defer rewriteDefFd.Close()
rewriteDefReader = rewriteDefFd
} else {
rewiteDefF, err := data.Assets.Open("rewrite.def")
if err != nil {
return fmt.Errorf("DefaultInputTextPlugin: %s: (data.Assets)rewrite.def", err)
}
defer rewiteDefF.Close()
rewriteDefReader = rewiteDefF
}
r := lnreader.NewLineNumberReader(rewriteDefReader)
for {
line, err := r.ReadLine()
if err == io.EOF {
break
}
if err != nil {
return fmt.Errorf("DefaultInputTextPlugin: %s", err)
}
if lnreader.IsSkipLine(line) {
continue
}
cols := strings.Fields(string(line))
if len(cols) == 1 {
// ignored normalize list
key := []rune(cols[0])
if len(key) != 1 {
return fmt.Errorf("DefaultInputTextPlugin: %s is already defined at line %d", cols[0], r.NumLine)
}
p.ignoreNormalizeMap[key[0]] = true
} else if len(cols) == 2 {
// replace char list
_, ok := p.replaceCharMap[cols[0]]
if ok {
return fmt.Errorf("DefaultInputTextPlugin: %s is already defined at line %d", cols[0], r.NumLine)
}
key := []rune(cols[0])
if p.getKeyLength(key[0], -1) < len(key) {
// store the longest key length
p.keyLengths[key[0]] = len(key)
}
p.replaceCharMap[cols[0]] = []rune(cols[1])
} else {
return fmt.Errorf("DefaultInputTextPlugin: invalid format at line %d", r.NumLine)
}
}
return nil
}