Last active
March 16, 2016 22:43
-
-
Save brydavis/763fc4bbce8c8733c3f1 to your computer and use it in GitHub Desktop.
Simple Decision Tree
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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