-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmecaboovproviderplugin.go
275 lines (258 loc) · 6.59 KB
/
mecaboovproviderplugin.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package gosudachi
import (
"fmt"
"io"
"os"
"strconv"
"strings"
"github.com/msnoigrs/gosudachi/data"
"github.com/msnoigrs/gosudachi/dictionary"
"github.com/msnoigrs/gosudachi/internal/lnreader"
)
type categoryInfo struct {
catType uint32
isInvoke bool
isGroup bool
length int
}
type oov struct {
leftId int16
rightId int16
cost int16
posId int16
}
type MeCabOovProviderPluginConfig struct {
CharDef *string
UnkDef *string
}
type MeCabOovProviderPlugin struct {
config *MeCabOovProviderPluginConfig
categories map[uint32]*categoryInfo
oovList map[uint32]*[]*oov
}
func NewMeCabOovProviderPlugin(config *MeCabOovProviderPluginConfig) *MeCabOovProviderPlugin {
if config == nil {
config = &MeCabOovProviderPluginConfig{}
}
return &MeCabOovProviderPlugin{
config: config,
categories: map[uint32]*categoryInfo{},
oovList: map[uint32]*[]*oov{},
}
}
func (p *MeCabOovProviderPlugin) GetConfigStruct() interface{} {
if p.config == nil {
p.config = &MeCabOovProviderPluginConfig{}
}
return p.config
}
func (p *MeCabOovProviderPlugin) SetUp(grammar *dictionary.Grammar) error {
if p.config.CharDef == nil {
zstr := ""
p.config.CharDef = &zstr
}
if p.config.UnkDef == nil {
zstr := ""
p.config.UnkDef = &zstr
}
if p.categories == nil {
p.categories = map[uint32]*categoryInfo{}
}
if p.oovList == nil {
p.oovList = map[uint32]*[]*oov{}
}
err := p.readCharacterProperty(*p.config.CharDef)
if err != nil {
return fmt.Errorf("MeCabOovProviderPlugin: %s", err)
}
err = p.readOov(*p.config.UnkDef, grammar)
if err != nil {
return fmt.Errorf("MeCabOovProviderPlugin: %s", err)
}
p.config = nil
return nil
}
func (p *MeCabOovProviderPlugin) ProvideOOV(inputText *InputText, offset int, hasOtherWords bool) ([]*LatticeNode, error) {
nodes := []*LatticeNode{}
length := inputText.GetCharCategoryContinuousLength(offset)
if length > 0 {
catTypes := inputText.GetCharCategoryTypes(offset)
for t := dictionary.DEFAULT; t <= dictionary.NOOOVBOW; t *= 2 {
if (catTypes & t) != t {
continue
}
cinfo, ok := p.categories[t]
if !ok {
continue
}
llength := length
oovs, ok := p.oovList[t]
if !ok {
continue
}
if cinfo.isGroup && (cinfo.isInvoke || !hasOtherWords) {
s := inputText.GetSubstring(offset, offset+length)
for _, oov := range *oovs {
nodes = append(nodes, p.getOovNode(s, oov, length))
}
llength -= 1
}
if cinfo.isInvoke || !hasOtherWords {
for i := 1; i <= cinfo.length; i++ {
sublength := inputText.GetCodePointsOffsetLength(offset, i)
if sublength > llength {
break
}
s := inputText.GetSubstring(offset, offset+sublength)
for _, oov := range *oovs {
nodes = append(nodes, p.getOovNode(s, oov, sublength))
}
}
}
}
}
return nodes, nil
}
func (p *MeCabOovProviderPlugin) getOovNode(text string, oov *oov, length int) *LatticeNode {
node := CreateNodeOfOOV()
node.SetParameter(oov.leftId, oov.rightId, oov.cost)
wi := &dictionary.WordInfo{
Surface: text,
HeadwordLength: int16(length),
PosId: oov.posId,
NormalizedForm: text,
DictionaryForm: text,
ReadingForm: "",
}
node.SetWordInfo(wi)
return node
}
func (p *MeCabOovProviderPlugin) readCharacterProperty(charDef string) error {
var charDefReader io.Reader
if charDef != "" {
charDefFd, err := os.OpenFile(charDef, os.O_RDONLY, 0644)
if err != nil {
return fmt.Errorf("%s: %s", err, charDef)
}
defer charDefFd.Close()
charDefReader = charDefFd
} else {
charDefF, err := data.Assets.Open("char.def")
if err != nil {
return fmt.Errorf("%s: (data.Assets)char.def", err)
}
defer charDefF.Close()
charDefReader = charDefF
}
r := lnreader.NewLineNumberReader(charDefReader)
for {
line, err := r.ReadLine()
if err == io.EOF {
break
}
if err != nil {
return err
}
if lnreader.IsSkipLine(line) {
continue
}
if len(line) > 2 && line[0] == '0' && line[1] == 'x' {
continue
}
cols := strings.Fields(string(line))
if len(cols) < 4 {
return fmt.Errorf("char.def: invalid format at line %d", r.NumLine)
}
catType, err := dictionary.GetCategoryType(cols[0])
if err != nil {
return fmt.Errorf("char.def: %s is invalid type at line %d", cols[0], r.NumLine)
}
_, ok := p.categories[catType]
if ok {
return fmt.Errorf("char.def: %s is already defined at line %d", cols[0], r.NumLine)
}
l, err := strconv.Atoi(cols[3])
if err != nil {
return fmt.Errorf("char.def: %s is invalid number at line %d", cols[3], r.NumLine)
}
catinfo := &categoryInfo{
catType: catType,
isInvoke: cols[1] != "0",
isGroup: cols[2] != "0",
length: l,
}
p.categories[catType] = catinfo
}
return nil
}
func (p *MeCabOovProviderPlugin) readOov(unkDef string, grammar *dictionary.Grammar) error {
var unkDefReader io.Reader
if unkDef != "" {
unkDefFd, err := os.OpenFile(unkDef, os.O_RDONLY, 0644)
if err != nil {
return err
}
defer unkDefFd.Close()
unkDefReader = unkDefFd
} else {
unkDefF, err := data.Assets.Open("unk.def")
if err != nil {
return err
}
defer unkDefF.Close()
unkDefReader = unkDefF
}
r := lnreader.NewLineNumberReader(unkDefReader)
for {
line, err := r.ReadLine()
if err == io.EOF {
break
}
if err != nil {
return err
}
cols := strings.Split(string(line), ",")
if len(cols) < 10 {
return fmt.Errorf("unk.def: invalid format at line %d", r.NumLine)
}
catType, err := dictionary.GetCategoryType(cols[0])
if err != nil {
return fmt.Errorf("unk.def: %s is invalid type at line %d", cols[0], r.NumLine)
}
_, ok := p.categories[catType]
if !ok {
return fmt.Errorf("unk.def: %s is undefined at line %d", cols[0], r.NumLine)
}
leftId, err := strconv.ParseInt(cols[1], 10, 16)
if err != nil {
return fmt.Errorf("unk.def: %s is invalid number at line %d", cols[1], r.NumLine)
}
rightId, err := strconv.ParseInt(cols[2], 10, 16)
if err != nil {
return fmt.Errorf("unk.def: %s is invalid number at line %d", cols[2], r.NumLine)
}
cost, err := strconv.ParseInt(cols[3], 10, 16)
if err != nil {
return fmt.Errorf("unk.def: %s is invalid number at line %d", cols[3], r.NumLine)
}
pos := []string{cols[4], cols[5], cols[6], cols[7], cols[8], cols[9]}
posId := grammar.GetPartOfSpeechId(pos)
if posId == -1 {
return fmt.Errorf("unk.def: unknown Part Of Speech at line %d", r.NumLine)
}
poov := &oov{
leftId: int16(leftId),
rightId: int16(rightId),
cost: int16(cost),
posId: posId,
}
l, ok := p.oovList[catType]
if !ok {
ll := []*oov{}
l = &ll
p.oovList[catType] = l
}
*l = append(*l, poov)
}
return nil
}