func (p *ActionPredictor) Train(iterations int, dataset DataSet, tokenizer *Tokenizer) { set := &mlearning.SimpleIterator{} for _, data := range dataset { tok := tokenizer.Tokenize(data.Sentence) set.FeatureSlice = append(set.FeatureSlice, &Meaning{ Tokens: tok, Vars: data.Vars, }) set.ClassSlice = append(set.ClassSlice, mlearning.Class(data.Action)) } for it := 0; it < iterations; it++ { set.Reset(true) c, n := p.Perceptron.Train(set) fmt.Printf("ActionPredictor iter %d: %d/%d=%.3f\n", it, c, n, float64(c)/float64(n)*100) } }
func (p *VarPredictor) dataToIterator(dataset DataSet) *mlearning.SimpleIterator { vs := &mlearning.SimpleIterator{} for _, data := range dataset { sen := data.CleanedSentence("[name]") tok := p.Tokenizer.Tokenize(sen) for i, t := range tok { if !strings.HasPrefix(t.Value, "[") { continue } name := strings.Trim(t.Value, "[]") vs.FeatureSlice = append(vs.FeatureSlice, &varFeature{ Sentence: tok, Action: data.Action, Pos: i, }) vs.ClassSlice = append(vs.ClassSlice, mlearning.Class(name)) } } return vs }
func (p *ActionPredictor) ReinforceMeaning(m *Meaning, action string) { feats := m.Features() guess, _ := p.Perceptron.Predict(feats) p.Perceptron.Update(mlearning.Class(action), guess, feats) }