2017-02-20 22:24:11 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-02-22 23:01:16 +00:00
|
|
|
"bufio"
|
2017-02-26 21:54:44 +00:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
2017-02-20 22:57:33 +00:00
|
|
|
"log"
|
2017-02-26 21:54:44 +00:00
|
|
|
"os"
|
2017-02-22 23:01:16 +00:00
|
|
|
"strings"
|
2017-02-20 22:57:33 +00:00
|
|
|
|
2017-02-22 23:01:16 +00:00
|
|
|
"github.com/jbrukh/bayesian"
|
2017-02-20 22:57:33 +00:00
|
|
|
"github.com/luksen/maildir"
|
|
|
|
)
|
|
|
|
|
2017-02-22 23:01:16 +00:00
|
|
|
const (
|
|
|
|
// good is the class of good mails that are not supposed to be Spam
|
|
|
|
good bayesian.Class = "Good"
|
|
|
|
// junk is the class of Spam mails
|
|
|
|
junk bayesian.Class = "Junk"
|
|
|
|
)
|
|
|
|
|
2017-02-20 22:57:33 +00:00
|
|
|
var (
|
2017-02-26 21:54:44 +00:00
|
|
|
// Processed is a map of e-mail IDs and the value set to true if Junk
|
|
|
|
Processed map[string]bool
|
2017-02-20 22:24:11 +00:00
|
|
|
)
|
|
|
|
|
2017-02-26 21:54:44 +00:00
|
|
|
// Mail includes the key of a mail in Maildir
|
|
|
|
type Mail struct {
|
|
|
|
Key string
|
|
|
|
Subject, Body *string
|
|
|
|
Junk bool
|
2017-02-22 23:01:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Classifiers contains the classifiers for mail subjects and bodies
|
|
|
|
type Classifiers struct {
|
|
|
|
Subject, Body *bayesian.Classifier
|
|
|
|
}
|
|
|
|
|
2017-02-26 21:54:44 +00:00
|
|
|
// Index loads all mail keys from the Maildir directory for processing.
|
|
|
|
func Index(d string) (m []Mail, err error) {
|
2017-02-22 23:01:16 +00:00
|
|
|
|
2017-02-26 21:54:44 +00:00
|
|
|
g, err := maildir.Dir(d).Keys()
|
2017-02-22 23:01:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return m, err
|
|
|
|
}
|
2017-02-26 21:54:44 +00:00
|
|
|
for _, val := range g {
|
|
|
|
var new Mail
|
|
|
|
new.Key = val
|
|
|
|
m = append(m, new)
|
|
|
|
}
|
2017-02-22 23:01:16 +00:00
|
|
|
|
2017-02-26 21:54:44 +00:00
|
|
|
j, err := maildir.Dir(d + "/.Junk").Keys()
|
2017-02-22 23:01:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return m, err
|
|
|
|
}
|
2017-02-26 21:54:44 +00:00
|
|
|
for _, val := range j {
|
|
|
|
var new Mail
|
|
|
|
new.Key = val
|
|
|
|
new.Junk = true
|
|
|
|
m = append(m, new)
|
|
|
|
}
|
2017-02-20 22:57:33 +00:00
|
|
|
|
2017-02-22 23:01:16 +00:00
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Learn initially classifies all mails and returns the respective classifiers.
|
2017-02-26 21:54:44 +00:00
|
|
|
func (m Mail) Learn() (c Classifiers, err error) {
|
2017-02-22 23:01:16 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-02-26 21:54:44 +00:00
|
|
|
// Clean prepares the mail's subject and body for training
|
|
|
|
func (m Mail) Clean() error {
|
|
|
|
return nil
|
2017-02-22 23:01:16 +00:00
|
|
|
}
|
|
|
|
|
2017-02-26 21:54:44 +00:00
|
|
|
// Load reads a mail's subject and body
|
|
|
|
func (m Mail) Load(d string) error {
|
|
|
|
|
|
|
|
message, err := maildir.Dir(d).Message(m.Key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// get Subject
|
|
|
|
subject := message.Header.Get("Subject")
|
|
|
|
m.Subject = &subject
|
|
|
|
|
|
|
|
// get Body
|
|
|
|
var b []string
|
|
|
|
bScanner := bufio.NewScanner(message.Body)
|
|
|
|
for bScanner.Scan() {
|
|
|
|
b = append(b, bScanner.Text())
|
2017-02-22 23:01:16 +00:00
|
|
|
}
|
|
|
|
|
2017-02-26 21:54:44 +00:00
|
|
|
body := strings.Join(b, " ")
|
|
|
|
m.Body = &body
|
|
|
|
|
|
|
|
return nil
|
2017-02-22 23:01:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2017-02-26 21:54:44 +00:00
|
|
|
// Get the Maildir to be handled
|
|
|
|
wd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
maildir := flag.String("d", wd+"/Maildir", "Path of the Maildir to be handled")
|
|
|
|
flag.Parse()
|
2017-02-20 22:57:33 +00:00
|
|
|
|
2017-02-26 21:54:44 +00:00
|
|
|
// Load the Maildir content
|
|
|
|
mails, err := Index(*maildir)
|
2017-02-22 23:01:16 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
2017-02-20 22:57:33 +00:00
|
|
|
}
|
|
|
|
|
2017-02-26 21:54:44 +00:00
|
|
|
fmt.Println(mails)
|
|
|
|
|
2017-02-22 23:01:16 +00:00
|
|
|
// Create a classifier
|
|
|
|
//classifier := bayesian.NewClassifier(Good, Junk)
|
2017-02-20 22:24:11 +00:00
|
|
|
}
|