2
0
mirror of https://github.com/carlostrub/sisyphus synced 2024-10-31 09:20:15 +00:00
sisyphus/learn.go
2017-05-10 17:23:25 +00:00

87 lines
1.4 KiB
Go

package sisyphus
import (
"log"
"github.com/boltdb/bolt"
"github.com/retailnext/hllpp"
)
// Learn adds the the mail key to the list of words using hyper log log algorithm.
func (m *Mail) Learn(db *bolt.DB) error {
log.Println("learn mail " + m.Key)
err := m.Clean()
if err != nil {
return err
}
list, err := m.Wordlist()
if err != nil {
return err
}
wordKey := "Good"
if m.Junk {
wordKey = "Junk"
}
// Learn words
for _, val := range list {
err = db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("Wordlists"))
bucket := b.Bucket([]byte(wordKey))
wordRaw := bucket.Get([]byte(val))
var word *hllpp.HLLPP
if len(wordRaw) == 0 {
word = hllpp.New()
} else {
word, err = hllpp.Unmarshal(wordRaw)
if err != nil {
return err
}
}
word.Add([]byte(m.Key))
err = bucket.Put([]byte(val), word.Marshal())
return err
})
if err != nil {
return err
}
}
// Update the statistics counter
err = db.Update(func(tx *bolt.Tx) error {
p := tx.Bucket([]byte("Statistics"))
key := "ProcessedGood"
if m.Junk {
key = "ProcessedJunk"
}
keyRaw := p.Get([]byte(key))
var counter *hllpp.HLLPP
if len(keyRaw) == 0 {
counter = hllpp.New()
} else {
counter, err = hllpp.Unmarshal(keyRaw)
if err != nil {
return err
}
}
counter.Add([]byte(m.Key))
err = p.Put([]byte(key), counter.Marshal())
return err
})
return err
}