-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclassify.go
179 lines (155 loc) · 4.52 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
// 2019, Georg Sauthoff <[email protected]>
// SPDX-License-Identifier: GPL-3.0-or-later
package main
import (
"encoding/binary"
"errors"
"fmt"
bolt "go.etcd.io/bbolt"
"io"
"log"
"math"
"os"
)
func get_uint32(b *bolt.Bucket, key []byte) uint32 {
v := b.Get(key)
if v == nil {
return 0
}
return binary.LittleEndian.Uint32(v)
}
type class_result struct {
prob float64
err error
}
func classify_words(db *bolt.DB, class_name []byte, ch chan []byte,
vocabulary uint32, done chan class_result) {
var prob float64
err := db.View(func(tx *bolt.Tx) error {
b := tx.Bucket(class_name)
if b == nil {
return errors.New("main bucket not found")
}
word_bag := b.Bucket([]byte("word_bag"))
if word_bag == nil {
return errors.New("word_bag not found")
}
stat := b.Bucket([]byte("stat"))
if stat == nil {
return errors.New("stat bag not found")
}
bag_size := get_uint32(stat, []byte("bag_size"))
for word := range ch {
f := get_uint32(word_bag, word)
// using pseudo-counts here
a := float64(f + 1) / float64(bag_size + vocabulary)
// we do multiplications in log-space
prob += math.Log(a)
}
return nil
})
done <- class_result{prob, err}
}
func get_vocabulary(db *bolt.DB) (uint32, error) {
var vocabulary uint32
err := db.View(func(tx *bolt.Tx) error {
for _, class_name := range [][]byte{[]byte("ham"), []byte("spam")} {
b := tx.Bucket(class_name)
if b == nil {
return errors.New("main bucket not found")
}
stat := b.Bucket([]byte("stat"))
if stat == nil {
return errors.New("stat bag not found")
}
v := get_uint32(stat, []byte("vocabulary"))
if v > vocabulary {
v = vocabulary
}
}
return nil
})
return vocabulary, err
}
func open_wo_creat(name string, flags int, mode os.FileMode) (*os.File, error) {
return os.OpenFile(name, flags & (^os.O_CREATE), mode)
}
func classify_file(in io.Reader, args *args) (bool, error) {
opts := *bolt.DefaultOptions
opts.ReadOnly = true
opts.OpenFile = open_wo_creat
db, err := bolt.Open(args.db_filename, 0666, &opts)
if err != nil {
return true, err
}
if args.sandbox {
blacklist_open(args.sandbox_debug)
}
ch := make(chan []byte, 100)
cw := new_channel_writer(ch)
cwo := new_keep_open_writer(cw)
ch1 := make(chan []byte, 100)
ch2 := make(chan []byte, 100)
go tee_words(ch, ch1, ch2)
h := new_word_split_writer(2, new_header_filter_writer(
new_replace_chars_writer(
new_mark_copy_header_writer(byte('h'), cwo))))
b := new_word_split_writer(-1,
new_replace_chars_writer(
new_mark_copy_body_writer(byte('b'), cwo)))
m := new_word_split_writer(-1, new_header_filter_writer(
new_replace_chars_writer(
new_mark_copy_header_writer(byte('m'), cwo))))
ham_done := make(chan class_result)
spam_done := make(chan class_result)
vocabulary, err := get_vocabulary(db)
if err != nil {
return true, err
}
go classify_words(db, []byte("ham"), ch1, vocabulary, ham_done )
go classify_words(db, []byte("spam"), ch2, vocabulary, spam_done)
if err := write_message(in, h, b, m); err != nil {
return true, err
}
if err := h.Close(); err != nil {
return true, err
}
if err := b.Close(); err != nil {
return true, err
}
if err := m.Close(); err != nil {
return true, err
}
if err := cw.Close(); err != nil {
return true, err
}
ham := <- ham_done
if ham.err != nil {
return true, err
}
spam := <- spam_done
if spam.err != nil {
return true, err
}
score := ham.prob/(ham.prob+spam.prob)
debugf("Ham %v vs. Spam %v score %v\n", ham.prob, spam.prob, score)
is_ham := score < 0.5 + 0.01
if err := db.Close(); err != nil {
return true, err
}
return is_ham, nil
}
func classify_message(args *args) {
in := open_input(args.in_filename)
is_ham, err := classify_file(in, args)
if err != nil {
log.Fatal(err)
}
if is_ham {
fmt.Printf("HAM\n")
os.Exit(10)
} else {
fmt.Printf("SPAM\n")
os.Exit(11)
}
}