From e00b8fa6094c510e18d269a2487fdc777231b5f3 Mon Sep 17 00:00:00 2001 From: Carlo Strub Date: Sun, 14 May 2017 21:16:04 +0000 Subject: [PATCH] simlify the learn function by moving the two actions into private functions --- learn.go | 85 +++++++++++++++++++++++++++++++++----------------------- 1 file changed, 50 insertions(+), 35 deletions(-) diff --git a/learn.go b/learn.go index 482ea5a..d33127a 100644 --- a/learn.go +++ b/learn.go @@ -7,51 +7,41 @@ import ( "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) - - list, err := m.cleanWordlist() - if err != nil { - return err - } - +// learnWordlist adds the mail key to the respective word's list +func (m *Mail) learnWordlist(w string, db *bolt.DB) error { 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 - } + err := db.Update(func(tx *bolt.Tx) (err error) { + b := tx.Bucket([]byte("Wordlists")) + + bucket := b.Bucket([]byte(wordKey)) + wordRaw := bucket.Get([]byte(w)) + 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)) + word.Add([]byte(m.Key)) - err = bucket.Put([]byte(val), word.Marshal()) + err = bucket.Put([]byte(w), word.Marshal()) - return err - }) - if err != nil { - return err - } - } + return err + }) - // Update the statistics counter - err = db.Update(func(tx *bolt.Tx) error { + return err +} + +// 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")) key := "ProcessedGood" @@ -79,3 +69,28 @@ func (m *Mail) Learn(db *bolt.DB) error { 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 + +}