-
Notifications
You must be signed in to change notification settings - Fork 1
/
filter.go
44 lines (34 loc) · 1.01 KB
/
filter.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
package main
import (
"github.com/jbrukh/bayesian"
)
const (
good bayesian.Class = "Good"
bad bayesian.Class = "Bad"
)
type classifier struct {
classifier *bayesian.Classifier
}
func trainClassifier(goodStuff, badStuff []string) (*classifier, error) {
c := bayesian.NewClassifier(good, bad)
c.Learn(goodStuff, good)
c.Learn(badStuff, bad)
return &classifier{classifier: c}, nil
}
func (c *classifier) classifyMessage(message string) ([]float64, []float64, bool, error) {
scores, _, _ := c.classifier.LogScores([]string{message})
// SafeProbScores helps with underflow.
// See: https://github.com/jbrukh/bayesian/blob/master/bayesian.go#L452
probs, _, _, err := c.classifier.SafeProbScores([]string{message})
if err != nil {
return nil, nil, false, err
}
isBad := false
// If the probability that it is good is less than 40% and
// the probability that it is bad is greater than 75%, then it
// is probably bad.
if probs[0] < 0.4 && probs[1] > 0.75 {
isBad = true
}
return scores, probs, isBad, err
}