forked from shixzie/nlp
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnlp.go
501 lines (471 loc) · 13.2 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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
// Package nlp provides general purpose Natural Language Processing.
package nlp
import (
"bytes"
"errors"
"fmt"
"reflect"
"strconv"
"time"
"unicode"
"encoding/json"
"github.com/cdipaolo/goml/base"
"github.com/cdipaolo/goml/text"
"github.com/itrabbit/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
}
// Structures for Export/Import
type wordSaved struct {
Count []uint64 `json:"c"`
Seen uint64 `json:"s"`
DocsSeen uint64 `json:"ds"`
}
type naiveBayesSaved struct {
Words map[string]wordSaved `json:"w"`
Count []uint64 `json:"c"`
Probabilities []float64 `json:"p"`
DocumentCount uint64 `json:"d"`
DictCount uint64 `json:"v"`
Tokenizer map[string]interface{} `json:"t,omitempty"`
}
type itemSaved struct {
Limit bool `json:"l,omitempty"`
Value []byte `json:"v,omitempty"`
FieldIndex int `json:"f,omitempty"`
}
type modelSaved struct {
Type string `json:"t"`
Expected [][]itemSaved `json:"e"`
Samples [][]byte `json:"s"`
}
// For save load alg
type nlSaved struct {
Models []modelSaved `json:"m,omitempty"`
NaiveBayes naiveBayesSaved `json:"n,omitempty"`
Output []byte `json:"o,omitempty"`
}
func (n nlSaved) indexOfModelByType(tpy reflect.Type) int {
t := tpy.Name()
for i, model := range n.Models {
if model.Type == t {
return i
}
}
return -1
}
// New returns a *NL
func New() *NL { return &NL{Output: bytes.NewBufferString("")} }
// Export
func (nl NL) Export() ([]byte, error) {
naive := naiveBayesSaved{
Words: make(map[string]wordSaved),
Count: nl.naive.Count,
Probabilities: nl.naive.Probabilities,
DocumentCount: nl.naive.DocumentCount,
DictCount: nl.naive.DictCount,
}
v := reflect.Indirect(reflect.ValueOf(nl.naive.Words))
words := reflect.Indirect(v.FieldByName("words"))
if words.Kind() == reflect.Map {
for _, key := range words.MapKeys() {
value := words.MapIndex(key)
if !value.IsValid() {
continue
}
if value.Kind() != reflect.Struct {
continue
}
s := value.FieldByName("Seen").Uint()
ds := value.FieldByName("DocsSeen").Uint()
c := make([]uint64, 0)
countVal := reflect.Indirect(value.FieldByName("Count"))
if countVal.Kind() == reflect.Slice || countVal.Kind() == reflect.Array {
for i := 0; i < countVal.Len(); i++ {
c = append(c, countVal.Index(i).Uint())
}
}
naive.Words[key.String()] = wordSaved{
Count: c,
Seen: s,
DocsSeen: ds,
}
}
}
models := make([]modelSaved, len(nl.models), len(nl.models))
for i, model := range nl.models {
e := make([][]itemSaved, len(model.expected), len(model.expected))
for j, arr := range model.expected {
sub := make([]itemSaved, len(arr), len(arr))
for y, item := range arr {
sub[y] = itemSaved{
Limit: item.limit,
Value: item.value,
FieldIndex: item.field.index,
}
}
e[j] = sub
}
models[i].Expected = e
models[i].Samples = model.samples
models[i].Type = model.tpy.Name()
}
m := map[string]interface{}{
"n": &naive,
"m": models,
"o": nl.Output.Bytes(),
}
return json.Marshal(&m)
}
// Import
func (nl *NL) Import(p []byte) error {
s := nlSaved{}
if err := json.Unmarshal(p, &s); err != nil {
return err
}
nl.naive = text.NewNaiveBayes(nil, uint8(len(nl.models)), base.OnlyWordsAndNumbers)
for k, v := range s.NaiveBayes.Words {
nl.naive.Words.Set(k, text.Word{
Count: v.Count,
Seen: v.Seen,
DocsSeen: v.DocsSeen,
})
}
nl.naive.DictCount = s.NaiveBayes.DictCount
nl.naive.DocumentCount = s.NaiveBayes.DocumentCount
nl.naive.Probabilities = s.NaiveBayes.Probabilities
nl.naive.Count = s.NaiveBayes.Count
nl.Output = &bytes.Buffer{}
nl.Output.Write(s.Output)
nl.naive.Output = nl.Output
if len(nl.models) != len(s.Models) {
return fmt.Errorf("invalid models")
}
for _, model := range nl.models {
index := s.indexOfModelByType(model.tpy)
if index < 0 {
continue
}
savedModel := s.Models[index]
model.expected = make([][]item, len(savedModel.Expected), len(savedModel.Expected))
for i, arr := range savedModel.Expected {
a := make([]item, len(arr), len(arr))
for j, obj := range arr {
a[j] = item{
limit: obj.Limit,
value: obj.Value,
field: model.fields[obj.FieldIndex],
}
}
model.expected[i] = a
}
model.samples = savedModel.Samples
}
return nil
}
// 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))
}
}