-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinputtext.go
245 lines (212 loc) · 6.4 KB
/
inputtext.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package gosudachi
import (
"unicode/utf8"
"github.com/msnoigrs/gosudachi/dictionary"
)
type InputText struct {
OriginalText string
ModifiedText string
Bytea []byte
offsets []int
byteIndexes []int
charCategories []uint32
charCategoryContinuities []int
canBowList []bool
}
func NewInputText(originalText string, modifiedText string, bytea []byte, offsets []int, byteIndexes []int, charCategories []uint32, charCategoryContinuities []int, canBowList []bool) *InputText {
return &InputText{
OriginalText: originalText,
ModifiedText: modifiedText,
Bytea: bytea,
offsets: offsets,
byteIndexes: byteIndexes,
charCategories: charCategories,
charCategoryContinuities: charCategoryContinuities,
canBowList: canBowList,
}
}
func (t *InputText) GetText() string {
return t.ModifiedText
}
func (t *InputText) GetByteText() []byte {
return t.Bytea
}
func (t *InputText) GetSubstring(begin int, end int) string {
return string([]rune(t.ModifiedText)[t.byteIndexes[begin]:t.byteIndexes[end]])
}
func (t *InputText) GetOffsetTextLength(index int) int {
return t.byteIndexes[index]
}
func (t *InputText) GetOriginalIndex(index int) int {
return t.offsets[index]
}
func (t *InputText) GetCharCategoryTypes(index int) uint32 {
return t.charCategories[t.byteIndexes[index]]
}
func (t *InputText) GetCharCategoryTypesRange(begin int, end int) uint32 {
if begin+t.charCategoryContinuities[begin] < end {
return uint32(0)
}
b := t.byteIndexes[begin]
e := t.byteIndexes[end]
continuousCategory := t.charCategories[b]
for i := b + 1; i < e; i++ {
continuousCategory &= t.charCategories[i]
}
return continuousCategory
}
func (t *InputText) GetCharCategoryContinuousLength(index int) int {
return t.charCategoryContinuities[index]
}
func (t *InputText) GetCodePointsOffsetLength(index int, codePointOffset int) int {
length := 0
target := t.byteIndexes[index] + codePointOffset
for i := index; i < len(t.Bytea); i++ {
if t.byteIndexes[i] >= target {
return length
}
length++
}
return length
}
func (t *InputText) CodePointCount(begin int, end int) int {
return t.byteIndexes[end] - t.byteIndexes[begin]
}
func (t *InputText) CanBow(index int) bool {
return t.IsCharAlignment(index) && t.canBowList[t.byteIndexes[index]]
}
func (t *InputText) IsCharAlignment(index int) bool {
return (t.Bytea[index] & 0xC0) != 0x80
}
type InputTextBuilder struct {
OriginalText string
modifiedRunes []rune
textOffsets []int
grammar *dictionary.Grammar
}
func NewInputTextBuilder(text string, grammar *dictionary.Grammar) *InputTextBuilder {
modifiedRunes := []rune(text)
offsetslen := len(modifiedRunes) + 1
textOffsets := make([]int, offsetslen, offsetslen)
for i := 0; i < len(modifiedRunes); i++ {
textOffsets[i] = i
}
textOffsets[len(modifiedRunes)] = len(modifiedRunes)
return &InputTextBuilder{
OriginalText: text,
modifiedRunes: modifiedRunes,
textOffsets: textOffsets,
grammar: grammar,
}
}
func (builder *InputTextBuilder) GetText() []rune {
ret := make([]rune, len(builder.modifiedRunes))
copy(ret, builder.modifiedRunes)
return ret
}
func (builder *InputTextBuilder) Replace(begin int, end int, runes []rune) {
rl := len(runes)
tlen := end - begin
offset := builder.textOffsets[begin]
if rl < tlen {
ol := len(builder.modifiedRunes)
copy(builder.modifiedRunes[begin+rl:], builder.modifiedRunes[end:])
copy(builder.modifiedRunes[begin:], runes)
builder.modifiedRunes = builder.modifiedRunes[:ol-tlen+rl]
tolen := len(builder.textOffsets)
copy(builder.textOffsets[begin+rl:], builder.textOffsets[end:])
builder.textOffsets = builder.textOffsets[:tolen-tlen+rl]
} else if rl == tlen {
copy(builder.modifiedRunes[begin:], runes)
} else {
builder.modifiedRunes = append(builder.modifiedRunes, make([]rune, rl-tlen)...)
copy(builder.modifiedRunes[begin+rl:], builder.modifiedRunes[end:])
copy(builder.modifiedRunes[begin:], runes)
builder.textOffsets = append(builder.textOffsets, make([]int, rl-tlen)...)
copy(builder.textOffsets[begin+rl:], builder.textOffsets[end:])
}
for i := 0; i < rl; i++ {
builder.textOffsets[begin+i] = offset
}
}
func (builder *InputTextBuilder) Build() *InputText {
// getCharCategoryTypes
runeCount := len(builder.modifiedRunes)
charCategoryTypes := make([]uint32, runeCount, runeCount)
for i := 0; i < runeCount; i++ {
charCategoryTypes[i] = builder.grammar.CharCategory.GetCategoryTypes(builder.modifiedRunes[i])
}
modifiedText := string(builder.modifiedRunes)
p := []byte(modifiedText)
keepp := p
bytelength := len(p)
size := bytelength + 1
indexes := make([]int, size, size)
offsets := make([]int, size, size)
sizes := make([]int, runeCount, runeCount)
pi := 0
for i := 0; len(p) > 0; i++ {
_, size := utf8.DecodeRune(p)
sizes[i] = size
for j := 0; j < size; j++ {
indexes[pi] = i
offsets[pi] = builder.textOffsets[i]
pi++
}
p = p[size:]
}
indexes[bytelength] = runeCount
offsets[bytelength] = builder.textOffsets[len(builder.textOffsets)-1]
// getCharCategoryContinuities
charCategoryContinuities := make([]int, bytelength, bytelength)
pi = 0
for i := 0; i < runeCount; {
next := i + getCharCategoryContinuousLength(charCategoryTypes, i)
var length int
for j := i; j < next; j++ {
length += sizes[j]
}
for k := length; k > 0; k-- {
charCategoryContinuities[pi] = k
pi++
}
i = next
}
// buildCanBowList
canBowList := make([]bool, runeCount, runeCount)
if runeCount > 0 {
canBowList[0] = true
for i := 1; i < runeCount; i++ {
types := charCategoryTypes[i]
if (types&dictionary.ALPHA == dictionary.ALPHA) ||
(types&dictionary.GREEK == dictionary.GREEK) ||
(types&dictionary.CYRILLIC == dictionary.CYRILLIC) {
cc := charCategoryTypes[i-1] & types
canBowList[i] = cc == 0
continue
}
canBowList[i] = true
}
}
return &InputText{
builder.OriginalText,
modifiedText,
keepp,
offsets,
indexes,
charCategoryTypes,
charCategoryContinuities,
canBowList,
}
}
func getCharCategoryContinuousLength(charCategories []uint32, offset int) int {
continuousCategory := charCategories[offset]
var length int
for length = 1; length < len(charCategories)-offset; length++ {
cc := continuousCategory & charCategories[offset+length]
if cc == 0 {
return length
}
}
return length
}