Browse Source

New Bayes Engine + dump

Loweel 4 years ago
parent
commit
68c199e045
5 changed files with 147 additions and 11 deletions
  1. 55 0
      bayes.jsonBAD.json
  2. 56 0
      bayes.jsonGOOD.json
  3. 6 0
      bayes.jsonstats.json
  4. 23 10
      file.go
  5. 7 1
      matrix.go

+ 55 - 0
bayes.jsonBAD.json

@@ -0,0 +1,55 @@
+{
+ "Freqs": {
+  "accept": 3,
+  "agent": 3,
+  "curl": 3,
+  "host": 3,
+  "http": 3,
+  "localhost": 3,
+  "qualcosa": 3,
+  "user": 3
+ },
+ "FreqTfs": {
+  "accept": [
+   0.125,
+   0.125,
+   0.125
+  ],
+  "agent": [
+   0.125,
+   0.125,
+   0.125
+  ],
+  "curl": [
+   0.125,
+   0.125,
+   0.125
+  ],
+  "host": [
+   0.125,
+   0.125,
+   0.125
+  ],
+  "http": [
+   0.125,
+   0.125,
+   0.125
+  ],
+  "localhost": [
+   0.125,
+   0.125,
+   0.125
+  ],
+  "qualcosa": [
+   0.125,
+   0.125,
+   0.125
+  ],
+  "user": [
+   0.125,
+   0.125,
+   0.125
+  ]
+ },
+ "Total": 24
+}

+ 56 - 0
bayes.jsonGOOD.json

@@ -0,0 +1,56 @@
+{
+ "Freqs": {
+  "accept": 4,
+  "agent": 4,
+  "curl": 4,
+  "google": 4,
+  "host": 4,
+  "http": 4,
+  "user": 4
+ },
+ "FreqTfs": {
+  "accept": [
+   0.14285714285714285,
+   0.14285714285714285,
+   0.14285714285714285,
+   0.14285714285714285
+  ],
+  "agent": [
+   0.14285714285714285,
+   0.14285714285714285,
+   0.14285714285714285,
+   0.14285714285714285
+  ],
+  "curl": [
+   0.14285714285714285,
+   0.14285714285714285,
+   0.14285714285714285,
+   0.14285714285714285
+  ],
+  "google": [
+   0.14285714285714285,
+   0.14285714285714285,
+   0.14285714285714285,
+   0.14285714285714285
+  ],
+  "host": [
+   0.14285714285714285,
+   0.14285714285714285,
+   0.14285714285714285,
+   0.14285714285714285
+  ],
+  "http": [
+   0.14285714285714285,
+   0.14285714285714285,
+   0.14285714285714285,
+   0.14285714285714285
+  ],
+  "user": [
+   0.14285714285714285,
+   0.14285714285714285,
+   0.14285714285714285,
+   0.14285714285714285
+  ]
+ },
+ "Total": 28
+}

+ 6 - 0
bayes.jsonstats.json

@@ -0,0 +1,6 @@
+
+{"MEH" : "4"}
+{"PASS" : "4"}
+{"BLOCK" : "3"}
+{"YOUNG" : "15"}
+{"LEARN-BAD" : "10"}

+ 23 - 10
file.go

@@ -1,7 +1,7 @@
 package main
 
 import (
-	//	"encoding/json"
+	"encoding/json"
 	"fmt"
 	"io"
 	"log"
@@ -39,24 +39,37 @@ func saveBayesToFile() {
 
 	dumpfile := os.Getenv("DUMPFILE")
 	if dumpfile == "" {
-		dumpfile = "bayes.json"
+		dumpfile = "./"
 	}
 
-	err := ZClassifier.Learning.bayez.WriteClassesToFile(dumpfile)
+	var statsREPORT string
 
+	statsREPORT = "\n"
+
+	ZClassifier.STATS.Range(func(key interface{}, value interface{}) bool {
+
+		statsREPORT += fmt.Sprintf("{\"%s\" : \"%d\"}\n", key.(string), value.(int64))
+		return true
+	})
+
+	writeToFile(dumpfile+"stats.json", statsREPORT)
+
+	badScores, err := json.MarshalIndent(ZClassifier.Learning.bayez.datas[Bad], "", " ")
 	if err != nil {
-		log.Println("Error Writing file: ", dumpfile, err.Error())
+		badScores = []byte(err.Error())
 	}
 
-	//	var statsREPORT string
+	writeToFile(dumpfile+"BAD.json", string(badScores))
+	log.Println(string(badScores))
 
-	//	statsREPORT = "\n"
+	goodScores, err := json.MarshalIndent(ZClassifier.Learning.bayez.datas[Good], "", " ")
+	if err != nil {
+		goodScores = []byte(err.Error())
+	}
 
-	//	Classifier.STATS.Range(func(key interface{}, value interface{}) bool {
+	writeToFile(dumpfile+"GOOD.json", string(goodScores))
 
-	//		statsREPORT += fmt.Sprintf("{\"%s\" : \"%d\"}\n", key.(string), value.(int64))
-	//		return true
-	//	})
+	log.Println(string(goodScores))
 
 	//	dumpfile := os.Getenv("DUMPFILE")
 	//	if dumpfile == "" {

+ 7 - 1
matrix.go

@@ -100,7 +100,7 @@ func (c *ByClassifier) Posterior(hdr string) map[string]float64 {
 	defer c.Working.busy.Unlock()
 
 	log.Println("Going to calculate the Scores")
-	scores, _, _, err := c.Working.bayez.SafeProbScores(strings.Fields(tokens))
+	scores, _, strict, err := c.Working.bayez.SafeProbScores(strings.Fields(tokens))
 	log.Println("Scores calculated")
 	if err == ErrUnderflow {
 		ff["BAD"] = 0.5
@@ -108,6 +108,12 @@ func (c *ByClassifier) Posterior(hdr string) map[string]float64 {
 		return ff
 	}
 
+	if !strict {
+		ff["BAD"] = 0.5
+		ff["GOOD"] = 0.5
+		return ff
+	}
+
 	ff["GOOD"] = scores[0]
 	ff["BAD"] = scores[1]