This repository has been archived by the owner on Jul 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
nlp.go
348 lines (324 loc) · 9.16 KB
/
nlp.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// Package nlp provides general purpose Natural Language Processing.
package nlp
import (
"bytes"
"errors"
"fmt"
"reflect"
"strconv"
"time"
"unicode"
"github.com/cdipaolo/goml/base"
"github.com/cdipaolo/goml/text"
"github.com/shixzie/nlp/parser"
)
// NL is a Natural Language Processor
type NL struct {
models []*model
naive *text.NaiveBayes
// Output contains the training output for the
// NaiveBayes algorithm
Output *bytes.Buffer
}
// New returns a *NL
func New() *NL { return &NL{Output: bytes.NewBufferString("")} }
// P proccesses the expr and returns one of
// the types passed as the i parameter to the RegistryModel
// func filled with the data inside expr
func (nl *NL) P(expr string) interface{} { return nl.models[nl.naive.Predict(expr)].fit(expr) }
// Learn maps the models samples to the models themselves and
// returns an error if something occurred while learning
func (nl *NL) Learn() error {
if len(nl.models) > 0 {
stream := make(chan base.TextDatapoint)
errors := make(chan error)
nl.naive = text.NewNaiveBayes(stream, uint8(len(nl.models)), base.OnlyWordsAndNumbers)
nl.naive.Output = nl.Output
go nl.naive.OnlineLearn(errors)
for i := range nl.models {
err := nl.models[i].learn()
if err != nil {
return fmt.Errorf("model#%d %v", i, err)
}
for _, s := range nl.models[i].samples {
stream <- base.TextDatapoint{
X: string(s),
Y: uint8(i),
}
}
}
close(stream)
for {
err := <-errors
if err != nil {
return fmt.Errorf("error occurred while learning: %s", err)
}
// training is done!
break
}
return nil
}
return fmt.Errorf("register at least one model before learning")
}
type model struct {
tpy reflect.Type
fields []field
expected [][]item
samples [][]byte
timeFormat string
timeLocation *time.Location
}
type item struct {
limit bool
value []byte
field field
}
type field struct {
index int
name string
kind interface{}
}
// ModelOption is an option for a specific model
type ModelOption func(*model) error
// WithTimeFormat sets the format used in time.Parse(format, val),
// note that format can't contain any spaces, the default is 01-02-2006_3:04pm
func WithTimeFormat(format string) ModelOption {
return func(m *model) error {
for _, v := range format {
if unicode.IsSpace(v) {
return errors.New("time format can't contain any spaces")
}
}
m.timeFormat = format
return nil
}
}
// WithTimeLocation sets the location used in time.ParseInLocation(format, value, loc),
// the default is time.Local
func WithTimeLocation(loc *time.Location) ModelOption {
return func(m *model) error {
if loc == nil {
return errors.New("time location can't be nil")
}
m.timeLocation = loc
return nil
}
}
// RegisterModel registers a model i and creates possible patterns
// from samples, the default layout when parsing time is 01-02-2006_3:04pm
// and the default location is time.Local.
// Samples must have special formatting:
//
// "play {Name} by {Artist}"
func (nl *NL) RegisterModel(i interface{}, samples []string, ops ...ModelOption) error {
if i == nil {
return fmt.Errorf("can't create model from nil value")
}
if len(samples) == 0 {
return fmt.Errorf("samples can't be nil or empty")
}
tpy, val := reflect.TypeOf(i), reflect.ValueOf(i)
if tpy.Kind() == reflect.Struct {
mod := &model{
tpy: tpy,
expected: make([][]item, len(samples)),
timeFormat: "01-02-2006_3:04pm",
timeLocation: time.Local,
}
mod.setSamples(samples)
for _, op := range ops {
err := op(mod)
if err != nil {
return err
}
}
NextField:
for i := 0; i < tpy.NumField(); i++ {
if tpy.Field(i).Anonymous || tpy.Field(i).PkgPath != "" {
continue NextField
}
if v, ok := val.Field(i).Interface().(time.Time); ok {
mod.fields = append(mod.fields, field{i, tpy.Field(i).Name, v})
continue NextField
} else if v, ok := val.Field(i).Interface().(time.Duration); ok {
mod.fields = append(mod.fields, field{i, tpy.Field(i).Name, v})
continue NextField
}
switch val.Field(i).Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Float32, reflect.Float64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.String:
mod.fields = append(mod.fields, field{i, tpy.Field(i).Name, val.Field(i).Kind()})
}
}
nl.models = append(nl.models, mod)
return nil
}
return fmt.Errorf("can't create model from non-struct type")
}
func (m *model) learn() error {
for sid, s := range m.samples {
tokens, err := parser.ParseSample(sid, s)
if err != nil {
return err
}
var exps []item
var hasAtLeastOneKey bool
l := len(tokens)
for i, tk := range tokens {
if tk.Kw {
hasAtLeastOneKey = true
mistypedField := true
for _, f := range m.fields {
if string(tk.Val) == f.name {
mistypedField = false
exps = append(exps, item{field: f, value: tk.Val})
}
}
if mistypedField {
return fmt.Errorf("sample#%d: mistyped field %q", sid, tk.Val)
}
} else {
if i+1 < l {
if tokens[i+1].Kw {
exps = append(exps, item{limit: true, value: tk.Val})
continue
}
}
}
}
if !hasAtLeastOneKey {
return fmt.Errorf("sample#%d: need at least one keyword", sid)
}
m.expected[sid] = exps
}
return nil
}
func (m *model) selectBestSample(expr []byte) []item {
// slice [sample_id]score
scores := make([]int, len(m.samples))
tokens, _ := parser.ParseSample(0, expr)
mapping := make([][]item, len(m.samples))
limitsOrder := make([][][]byte, len(m.samples)+1)
for sid, exps := range m.expected {
var currentVal [][]byte
var reading bool
var lastToken int
expecteds:
for _, e := range exps {
// fmt.Printf("expecting: %s - limit: %v\n", e.value, e.limit)
if e.limit {
reading = false
limitsOrder[sid+1] = append(limitsOrder[sid+1], e.value)
} else {
reading = true
}
// fmt.Printf("reading: %v\n", reading)
for i := lastToken; i < len(tokens); i++ {
t := tokens[i]
// fmt.Printf("token: %s - isLimit: %v\n", t.Val, m.isLimit(t.Val, sid))
if m.isLimit(t.Val, sid) {
if sid == 0 {
limitsOrder[0] = append(limitsOrder[0], t.Val)
}
scores[sid]++
if len(currentVal) > 0 {
// fmt.Printf("appending: %s {%v}\n", bytes.Join(currentVal, []byte{' '}), e.field.name)
mapping[sid] = append(mapping[sid], item{field: e.field, value: bytes.Join(currentVal, []byte{' '})})
currentVal = currentVal[:0]
lastToken = i
continue expecteds
}
lastToken = i + 1
continue expecteds
} else {
if reading {
// fmt.Printf("adding: %s\n", t.Val)
currentVal = append(currentVal, t.Val)
}
}
}
if len(currentVal) > 0 {
// fmt.Printf("appending: %s {%v}\n", bytes.Join(currentVal, []byte{' '}), e.field.name)
mapping[sid] = append(mapping[sid], item{field: e.field, value: bytes.Join(currentVal, []byte{' '})})
}
}
// fmt.Printf("\n\n")
}
order:
for i := 1; i < len(limitsOrder); i++ {
if len(limitsOrder[0]) < len(limitsOrder[i]) {
continue order
}
for j := range limitsOrder[i] {
if !bytes.Equal(limitsOrder[i][j], limitsOrder[0][j]) {
continue order
}
}
scores[i-1]++
}
// fmt.Printf("orders: %s\n\n", limitsOrder)
// fmt.Printf("scores: %v\n", scores)
bestMapping := selectBestMapping(scores)
if bestMapping == -1 {
return nil
}
return mapping[bestMapping]
}
func selectBestMapping(scores []int) int {
bestScore, bestMapping := -1, -1
for id, score := range scores {
if score > bestScore {
bestScore = score
bestMapping = id
}
}
return bestMapping
}
func (m *model) fit(expr string) interface{} {
val := reflect.New(m.tpy)
if len(expr) == 0 {
return val.Interface()
}
exps := m.selectBestSample([]byte(expr))
if len(exps) > 0 {
for _, e := range exps {
switch t := e.field.kind.(type) {
case reflect.Kind:
switch t {
case reflect.String:
val.Elem().Field(e.field.index).SetString(string(e.value))
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
v, _ := strconv.ParseUint(string(e.value), 10, 0)
val.Elem().Field(e.field.index).SetUint(v)
case reflect.Float32, reflect.Float64:
v, _ := strconv.ParseFloat(string(e.value), 64)
val.Elem().Field(e.field.index).SetFloat(v)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
v, _ := strconv.ParseInt(string(e.value), 10, 0)
val.Elem().Field(e.field.index).SetInt(v)
}
case time.Time:
v, _ := time.ParseInLocation(m.timeFormat, string(e.value), m.timeLocation)
val.Elem().Field(e.field.index).Set(reflect.ValueOf(v))
case time.Duration:
v, _ := time.ParseDuration(string(e.value))
val.Elem().Field(e.field.index).Set(reflect.ValueOf(v))
}
}
}
return val.Interface()
}
// isLimit returns true if s is a limit on expected[id]
func (m *model) isLimit(s []byte, id int) bool {
for _, e := range m.expected[id] {
if bytes.Equal(e.value, s) {
return true
}
}
return false
}
// setSample converts the []string samples to [][]byte
func (m *model) setSamples(samples []string) {
for _, s := range samples {
m.samples = append(m.samples, []byte(s))
}
}