simlify the learn function by moving the two actions into private functions

master
Carlo Strub 7 years ago
parent 1e2b563585
commit e00b8fa609

@ -7,51 +7,41 @@ import (
"github.com/retailnext/hllpp" "github.com/retailnext/hllpp"
) )
// Learn adds the the mail key to the list of words using hyper log log algorithm. // learnWordlist adds the mail key to the respective word's list
func (m *Mail) Learn(db *bolt.DB) error { func (m *Mail) learnWordlist(w string, db *bolt.DB) error {
log.Println("learn mail " + m.Key)
list, err := m.cleanWordlist()
if err != nil {
return err
}
wordKey := "Good" wordKey := "Good"
if m.Junk { if m.Junk {
wordKey = "Junk" wordKey = "Junk"
} }
// Learn words err := db.Update(func(tx *bolt.Tx) (err error) {
for _, val := range list { b := tx.Bucket([]byte("Wordlists"))
err = db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("Wordlists")) bucket := b.Bucket([]byte(wordKey))
wordRaw := bucket.Get([]byte(w))
bucket := b.Bucket([]byte(wordKey)) var word *hllpp.HLLPP
wordRaw := bucket.Get([]byte(val)) if len(wordRaw) == 0 {
var word *hllpp.HLLPP word = hllpp.New()
if len(wordRaw) == 0 { } else {
word = hllpp.New() word, err = hllpp.Unmarshal(wordRaw)
} else { if err != nil {
word, err = hllpp.Unmarshal(wordRaw) return err
if err != nil {
return err
}
} }
}
word.Add([]byte(m.Key)) word.Add([]byte(m.Key))
err = bucket.Put([]byte(val), word.Marshal()) err = bucket.Put([]byte(w), word.Marshal())
return err return err
}) })
if err != nil {
return err
}
}
// Update the statistics counter return err
err = db.Update(func(tx *bolt.Tx) error { }
// learnStatistics adds the mail key to the respective word's list
func (m *Mail) learnStatistics(db *bolt.DB) error {
err := db.Update(func(tx *bolt.Tx) (err error) {
p := tx.Bucket([]byte("Statistics")) p := tx.Bucket([]byte("Statistics"))
key := "ProcessedGood" key := "ProcessedGood"
@ -79,3 +69,28 @@ func (m *Mail) Learn(db *bolt.DB) error {
return err return err
} }
// 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)
list, err := m.cleanWordlist()
if err != nil {
return err
}
// Learn words
for _, val := range list {
err := m.learnWordlist(val, db)
if err != nil {
return err
}
}
// Update the statistics counter
err = m.learnStatistics(db)
return err
}

Loading…
Cancel
Save