-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplugin.go
244 lines (218 loc) · 6.67 KB
/
plugin.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
package gosudachi
import (
"fmt"
"github.com/msnoigrs/gosudachi/dictionary"
)
type Settings interface {
GetBaseConfig() *BaseConfig
}
type BaseConfig struct {
SystemDict string
CharacterDefinitionFile string
UserDict []string
Utf16String bool
}
type PluginMaker interface {
GetInputTextPluginArray(f MakeInputTextPluginFunc) ([]InputTextPlugin, error)
GetOovProviderPluginArray(f MakeOovProviderPluginFunc) ([]OovProviderPlugin, error)
GetPathRewritePluginArray(f MakePathRewritePluginFunc) ([]PathRewritePlugin, error)
GetEditConnectionCostPluginArray(f MakeEditConnectionCostPluginFunc) ([]EditConnectionCostPlugin, error)
}
type Plugin interface {
GetConfigStruct() interface{}
}
type MakeInputTextPluginFunc func(n string) InputTextPlugin
type MakeEditConnectionCostPluginFunc func(n string) EditConnectionCostPlugin
type MakeOovProviderPluginFunc func(n string) OovProviderPlugin
type MakePathRewritePluginFunc func(n string) PathRewritePlugin
func DefMakeInputTextPlugin(k string) InputTextPlugin {
switch k {
case "DefaultInputTextPlugin", "com.worksap.nlp.sudachi.DefaultInputTextPlugin":
return NewDefaultInputTextPlugin(nil)
case "ProlongedSoundMarkInputTextPlugin", "com.worksap.nlp.sudachi.ProlongedSoundMarkInputTextPlugin":
return NewProlongedSoundMarkInputTextPlugin(nil)
}
return nil
}
func DefMakeEditConnectionCostPlugin(k string) EditConnectionCostPlugin {
switch k {
case "InhibitConnectionPlugin", "com.worksap.nlp.sudachi.InhibitConnectionPlugin":
return NewInhibitConnectionPlugin([]*[]int{})
}
return nil
}
func DefMakeOovProviderPlugin(k string) OovProviderPlugin {
switch k {
case "MeCabOovProviderPlugin", "com.worksap.nlp.sudachi.MeCabOovProviderPlugin":
return NewMeCabOovProviderPlugin(nil)
case "SimpleOovProviderPlugin", "com.worksap.nlp.sudachi.SimpleOovProviderPlugin":
return NewSimpleOovProviderPlugin(nil)
}
return nil
}
func DefMakePathRewritePlugin(k string) PathRewritePlugin {
switch k {
case "JoinNumericPlugin", "com.worksap.nlp.sudachi.JoinNumericPlugin":
return NewJoinNumericPlugin(nil)
case "JoinKatakanaOovPlugin", "com.worksap.nlp.sudachi.JoinKatakanaOovPlugin":
return NewJoinKatakanaOovPlugin(nil)
}
return nil
}
type EditConnectionCostPlugin interface {
Plugin
SetUp(grammar *dictionary.Grammar) error
Edit(grammar *dictionary.Grammar) error
}
func InhibitConnection(grammar *dictionary.Grammar, leftId int16, rightId int16) {
grammar.SetConnectCost(leftId, rightId, dictionary.InhibitedConnection)
}
type PathRewritePlugin interface {
Plugin
SetUp(grammar *dictionary.Grammar) error
Rewrite(text *InputText, path *[]*LatticeNode, lattice *Lattice) error
}
func ConcatenateNodes(path *[]*LatticeNode, begin int, end int, lattice *Lattice, normalizedForm string) (*LatticeNode, error) {
if begin >= end {
return nil, fmt.Errorf("begin >= end")
}
tpath := *path
b := tpath[begin].GetBegin()
e := tpath[end-1].GetEnd()
bwi := tpath[begin].GetWordInfo()
posId := bwi.PosId
var (
surfaceLen int
normalizedFormLen int
dictionaryFormLen int
readingFormLen int
length int16
)
wilist := make([]*dictionary.WordInfo, 0, end - begin)
for i := begin; i < end; i++ {
info := tpath[i].GetWordInfo()
wilist = append(wilist, info)
surfaceLen += len(info.Surface)
length += info.HeadwordLength
if normalizedForm == "" {
normalizedFormLen += len(info.NormalizedForm)
}
dictionaryFormLen += len(info.DictionaryForm)
readingFormLen += len(info.ReadingForm)
}
csurface := make([]byte, 0, surfaceLen)
var cnormalizedForm []byte
if normalizedForm == "" {
cnormalizedForm = make([]byte, 0, normalizedFormLen)
}
cdictionaryForm := make([]byte, 0, dictionaryFormLen)
creadingForm := make([]byte, 0, readingFormLen)
for _, wi := range wilist {
csurface = append(csurface, []byte(wi.Surface)...)
if normalizedForm == "" {
cnormalizedForm = append(cnormalizedForm, []byte(wi.NormalizedForm)...)
}
cdictionaryForm = append(cdictionaryForm, []byte(wi.DictionaryForm)...)
creadingForm = append(creadingForm, []byte(wi.ReadingForm)...)
}
if normalizedForm == "" {
normalizedForm = string(cnormalizedForm)
}
wi := &dictionary.WordInfo{
Surface: string(csurface),
HeadwordLength: length,
PosId: posId,
NormalizedForm: normalizedForm,
DictionaryForm: string(cdictionaryForm),
ReadingForm: string(creadingForm),
}
node := &LatticeNode{}
node.SetRange(b, e)
node.SetWordInfo(wi)
*path = replaceNode(tpath, begin, end, node)
return node, nil
}
func ConcatenateOov(path *[]*LatticeNode, begin int, end int, posId int16, lattice *Lattice) (*LatticeNode, error) {
if begin >= end {
return nil, fmt.Errorf("begin >= end")
}
tpath := *path
b := tpath[begin].GetBegin()
e := tpath[end-1].GetEnd()
n := lattice.GetMinimumNode(b, e)
if n != nil {
*path = replaceNode(tpath, begin, end, n)
return n, nil
}
var (
surfaceLen int
length int16
)
wilist := make([]*dictionary.WordInfo, 0, end - begin)
for i := begin; i < end; i++ {
info := tpath[i].GetWordInfo()
wilist = append(wilist, info)
surfaceLen += len(info.Surface)
length += info.HeadwordLength
}
csurface := make([]byte, 0, surfaceLen)
for _, wi := range wilist {
csurface = append(csurface, []byte(wi.Surface)...)
}
s := string(csurface)
wi := &dictionary.WordInfo{
Surface: s,
HeadwordLength: length,
PosId: posId,
NormalizedForm: s,
DictionaryForm: s,
ReadingForm: "",
}
node := &LatticeNode{}
node.SetRange(b, e)
node.SetWordInfo(wi)
node.IsOov = true
*path = replaceNode(tpath, begin, end, node)
return node, nil
}
func GetCharCategoryTypes(text *InputText, node *LatticeNode) uint32 {
return text.GetCharCategoryTypesRange(node.Begin, node.End)
}
func replaceNode(path []*LatticeNode, begin int, end int, node *LatticeNode) []*LatticeNode {
d := end - begin
if d > 1 {
if end < len(path) {
copy(path[begin+1:], path[end:])
}
path = path[:len(path)-d+1]
}
path[begin] = node
return path
}
type InputTextPlugin interface {
Plugin
SetUp() error
Rewrite(builder *InputTextBuilder) error
}
type OovProviderPlugin interface {
Plugin
SetUp(grammar *dictionary.Grammar) error
ProvideOOV(inputText *InputText, offset int, hasOtherWords bool) ([]*LatticeNode, error)
}
func GetOOV(p OovProviderPlugin, inputText *InputText, offset int, hasOtherWords bool) ([]*LatticeNode, error) {
nodes, err := p.ProvideOOV(inputText, offset, hasOtherWords)
if err != nil {
return []*LatticeNode{}, err
}
for _, node := range nodes {
wi := node.GetWordInfo()
node.Begin = offset
node.End = offset + int(wi.HeadwordLength)
}
return nodes, nil
}
func CreateNodeOfOOV() *LatticeNode {
return &LatticeNode{
IsOov: true,
}
}