2
0
mirror of https://github.com/carlostrub/sisyphus synced 2024-10-31 09:20:15 +00:00
This commit is contained in:
Carlo Strub 2017-03-19 22:35:13 +00:00
parent 6613f768da
commit a6b1d02ac2
2 changed files with 64 additions and 15 deletions

53
mail.go
View File

@ -20,7 +20,7 @@ import (
type Mail struct { type Mail struct {
Key string Key string
Subject, Body *string Subject, Body *string
Junk bool Junk, New bool
} }
// CreateDirs creates all the required dirs -- if not already there. // CreateDirs creates all the required dirs -- if not already there.
@ -212,18 +212,63 @@ func (m *Mail) Classify(db *bolt.DB) error {
} }
list := m.Wordlist() list := m.Wordlist()
scoreG, scoreJ, junk := LogScores(db, list) scoreG, scoreJ, ju := LogScores(db, list)
m.Junk = junk
log.Print("Classified " + m.Key + " as Junk=" + strconv.FormatBool(m.Junk) + log.Print("Classified " + m.Key + " as Junk=" + strconv.FormatBool(m.Junk) +
" (good: " + strconv.FormatFloat(scoreG, 'f', 4, 64) + " (good: " + strconv.FormatFloat(scoreG, 'f', 4, 64) +
", junk: " + strconv.FormatFloat(scoreJ, 'f', 4, 64) + ")") ", junk: " + strconv.FormatFloat(scoreJ, 'f', 4, 64) + ")")
// Move mails around after classification
if m.New && ju {
m.Junk = ju
err := os.Rename("./new/"+m.Key, "./.Junk/cur/"+m.Key)
if err != nil {
return err
}
log.Print("Moved " + m.Key + " from new to Junk folder")
}
if m.New == false && m.Junk && ju == false {
err := os.Rename("./.Junk/cur/"+m.Key, "./cur/"+m.Key)
if err != nil {
return err
}
m.Junk = ju
log.Print("Moved " + m.Key + " from Junk to Good folder")
}
if m.New == false && ju && m.Junk == false {
err := os.Rename("./cur/"+m.Key, "./.Junk/cur/"+m.Key)
if err != nil {
return err
}
m.Junk = ju
log.Print("Moved " + m.Key + " from Good to Junk folder")
}
// Inform the DB about a processed mail
db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("Processed"))
bMails := b.Bucket([]byte("Mails"))
if ju {
err := bMails.Put([]byte(m.Key), []byte(junk))
if err != nil {
return err
}
} else {
err := bMails.Put([]byte(m.Key), []byte(good))
if err != nil {
return err
}
}
return err
})
return nil return nil
} }
// Learn adds the words to the respective list and unlearns on the other, if // Learn adds the words to the respective list and unlearns on the other, if
// the mail has been moved from there. // the mail has been moved from there.
func (m *Mail) Learn() error { func (m *Mail) Learn(db *bolt.DB) error {
return nil return nil
} }

26
main.go
View File

@ -134,25 +134,26 @@ func main() {
for i := range mails { for i := range mails {
db.View(func(tx *bolt.Tx) error { db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("Processed")) b := tx.Bucket([]byte("Processed"))
v := b.Get([]byte(mails[i].Key)) bMails := b.Bucket([]byte("Mails"))
v := bMails.Get([]byte(mails[i].Key))
if len(v) == 0 { if len(v) == 0 {
err = mails[i].Classify(db) err = mails[i].Classify(db)
if err != nil { if err != nil {
log.Print(err) log.Print(err)
} }
err = mails[i].Learn() err = mails[i].Learn(db)
if err != nil { if err != nil {
log.Print(err) log.Print(err)
} }
} }
if string(v) == good && mails[i].Junk == true { if string(v) == good && mails[i].Junk == true {
err = mails[i].Learn() err = mails[i].Learn(db)
if err != nil { if err != nil {
log.Print(err) log.Print(err)
} }
} }
if string(v) == junk && mails[i].Junk == false { if string(v) == junk && mails[i].Junk == false {
err = mails[i].Learn() err = mails[i].Learn(db)
if err != nil { if err != nil {
log.Print(err) log.Print(err)
} }
@ -179,13 +180,16 @@ func main() {
Key: mailName[len(mailName)-1], Key: mailName[len(mailName)-1],
} }
err = m.Classify(db) if mailName[len(mailName)-2] == "new" {
if err != nil { err = m.Classify(db)
log.Print(err) if err != nil {
} log.Print(err)
err = m.Learn() }
if err != nil { } else {
log.Print(err) err = m.Learn(db)
if err != nil {
log.Print(err)
}
} }
} }