Skip to content

Instantly share code, notes, and snippets.

@brydavis
Last active March 16, 2016 22:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brydavis/763fc4bbce8c8733c3f1 to your computer and use it in GitHub Desktop.
Save brydavis/763fc4bbce8c8733c3f1 to your computer and use it in GitHub Desktop.
Simple Decision Tree
package main
import (
"bytes"
"encoding/csv"
// "encoding/json"
"fmt"
"io/ioutil"
"strings"
)
func main() {
data, _ := ioutil.ReadFile("raw.csv")
r := csv.NewReader(bytes.NewReader(data))
result, _ := r.ReadAll()
spamScan1(result)
spamScan2(result)
// j, _ := json.MarshalIndent(h, "", "\t")
// fmt.Println(string(j))
}
func spamScan1(dataset interface{}) {
switch t := dataset.(type) {
case [][]string:
h := hashify(t)
spamScan1(h)
case []map[string]string:
fmt.Println("\nspamScan1 -------------")
for _, row := range t {
if row["contains_images"] == "true" {
if row["suspicious_words"] == "true" {
fmt.Printf("\t%s is SPAM\n", row["id"])
} else {
fmt.Printf("\t%s is HAM\n", row["id"])
}
} else {
if row["unknown_sender"] == "true" {
fmt.Printf("\t%s is SPAM\n", row["id"])
} else {
fmt.Printf("\t%s is HAM\n", row["id"])
}
}
}
}
}
func spamScan2(dataset interface{}) {
switch t := dataset.(type) {
case [][]string:
h := hashify(t)
spamScan2(h)
case []map[string]string:
fmt.Println("\nspamScan2 -------------")
for _, row := range t {
if row["suspicious_words"] == "true" {
fmt.Printf("\t%s is SPAM\n", row["id"])
} else {
fmt.Printf("\t%s is HAM\n", row["id"])
}
}
}
}
func hashify(dataset [][]string) []map[string]string {
headers := []string{}
for _, d := range dataset[0] {
headers = append(headers, strings.TrimSpace(d))
}
hash := []map[string]string{}
for _, row := range dataset[1:] {
m := map[string]string{}
for n, val := range row {
m[headers[n]] = strings.TrimSpace(val)
}
hash = append(hash, m)
}
return hash
}
id suspicious_words unknown_sender contains_images class
376 true false true spam
489 true true false spam
541 true true false spam
693 false true true ham
782 false false false ham
976 false false false ham
853 true true false spam
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment