-
Notifications
You must be signed in to change notification settings - Fork 5
/
classify.go
247 lines (199 loc) · 5.46 KB
/
classify.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
package sisyphus
import (
"math/rand"
"os"
"path/filepath"
"time"
log "github.com/sirupsen/logrus"
"github.com/boltdb/bolt"
"github.com/gonum/stat"
"github.com/retailnext/hllpp"
)
// classificationPrior returns the prior probabilities for good and junk
// classes.
func classificationPrior(db *bolt.DB) (g float64, err error) {
gTotal, jTotal, err := classificationStatistics(db)
if err != nil {
return g, err
}
return gTotal / (gTotal + jTotal), err
}
// classificationLikelihoodWordcounts gets wordcounts from database to be used
// in Likelihood calculation
func classificationLikelihoodWordcounts(db *bolt.DB, word string) (gN, jN float64, err error) {
err = db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("Wordlists"))
good := b.Bucket([]byte("Good"))
gWordRaw := good.Get([]byte(word))
if len(gWordRaw) > 0 {
var gWordHLL *hllpp.HLLPP
gWordHLL, err = hllpp.Unmarshal(gWordRaw)
if err != nil {
return err
}
gN = float64(gWordHLL.Count())
}
junk := b.Bucket([]byte("Junk"))
jWordRaw := junk.Get([]byte(word))
if len(jWordRaw) > 0 {
var jWordHLL *hllpp.HLLPP
jWordHLL, err = hllpp.Unmarshal(jWordRaw)
if err != nil {
return err
}
jN = float64(jWordHLL.Count())
}
return nil
})
return gN, jN, err
}
// classificationStatistics gets global statistics from database to
// be used in Likelihood calculation
func classificationStatistics(db *bolt.DB) (gTotal, jTotal float64, err error) {
err = db.View(func(tx *bolt.Tx) error {
p := tx.Bucket([]byte("Statistics"))
gRaw := p.Get([]byte("ProcessedGood"))
if len(gRaw) > 0 {
var gHLL *hllpp.HLLPP
gHLL, err = hllpp.Unmarshal(gRaw)
if err != nil {
return err
}
gTotal = float64(gHLL.Count())
}
jRaw := p.Get([]byte("ProcessedJunk"))
if len(jRaw) > 0 {
var jHLL *hllpp.HLLPP
jHLL, err = hllpp.Unmarshal(jRaw)
if err != nil {
return err
}
jTotal = float64(jHLL.Count())
}
if gTotal == 0 && jTotal == 0 {
log.Warning("no mails have yet been learned")
return nil
}
if gTotal == 0 {
log.Warning("no good mails have yet been learned")
return nil
}
if jTotal == 0 {
log.Warning("no junk mails have yet been learned")
return nil
}
return nil
})
return gTotal, jTotal, err
}
// classificationLikelihood returns P(W|C_j) -- the probability of seeing a
// particular word W in a document of this class.
func classificationLikelihood(db *bolt.DB, word string) (g, j float64, err error) {
gN, jN, err := classificationLikelihoodWordcounts(db, word)
if err != nil {
return g, j, err
}
gTotal, jTotal, err := classificationStatistics(db)
if err != nil {
return g, j, err
}
g = gN / gTotal
j = jN / jTotal
return g, j, err
}
// classificationWord produces the conditional probability of a word belonging
// to good or junk using the classic Bayes' rule.
func classificationWord(db *bolt.DB, word string) (g float64, err error) {
priorG, err := classificationPrior(db)
if err != nil {
return g, err
}
likelihoodG, likelihoodJ, err := classificationLikelihood(db, word)
if err != nil {
return g, err
}
g = (likelihoodG * priorG) / (likelihoodG*priorG + likelihoodJ*(1-priorG))
return g, nil
}
// Classify analyses a new mail (a mail that arrived in the "new" directory),
// decides whether it is junk and -- if so -- moves it to the Junk folder. If
// it is not junk, the mail is untouched so it can be handled by the mail
// client.
func (m *Mail) Classify(db *bolt.DB, dir Maildir) (err error) {
m.New = true
err = m.Load(dir)
if err != nil {
return err
}
list, err := m.cleanWordlist()
if err != nil {
return err
}
junk, prob, err := Junk(db, list)
if err != nil {
return err
}
m.Junk = junk
log.WithFields(log.Fields{
"mail": m.Key,
"junk": m.Junk,
"probability": prob,
"dir": string(dir),
}).Info("Classified")
// Move mail around if junk.
if junk {
if !m.DryRun {
err = os.Rename(filepath.Join(string(dir), "new", m.Key), filepath.Join(string(dir), ".Junk", "cur", m.Key))
if err != nil {
return err
}
}
var dryRun string
if m.DryRun {
dryRun = "-- dry run (nothing happened to this mail!)"
}
log.WithFields(log.Fields{
"mail": m.Key,
}).Info("Moved to Junk folder" + dryRun)
}
err = m.Unload(dir)
return err
}
// Junk returns true if the wordlist is classified as a junk mail using Bayes'
// rule. If required, it also returns the calculated probability of being junk,
// but this is typically not needed.
func Junk(db *bolt.DB, wordlist []string) (junk bool, prob float64, err error) {
var probabilities []float64
// If the wordlist is too long, let us only select a random sample
// for analysis. This prevents cheating by adding lots of good text
// to a Junk mail
if len(wordlist) > 50 {
wordlistTemp := make(map[string]interface{})
rand.Seed(time.Now().UnixNano())
for len(wordlistTemp) < 50 {
wordlistTemp[wordlist[rand.Intn(len(wordlist)-1)]] = nil
}
var wordlistTempSlice []string
for key := range wordlistTemp {
wordlistTempSlice = append(wordlistTempSlice, key)
}
wordlist = wordlistTempSlice
}
// initial value should be no junk
prob = 1.0
for _, val := range wordlist {
var p float64
p, err = classificationWord(db, val)
if err != nil {
return false, 0.0, err
}
probabilities = append(probabilities, p)
}
if len(probabilities) > 0 {
prob = stat.HarmonicMean(probabilities, nil)
}
if prob < 0.5 {
return true, (1 - prob), err
}
return false, (1 - prob), err
}