Browse Source

Adding stats to the file

Loweel 4 years ago
parent
commit
dfaa2e8758
3 changed files with 37 additions and 13 deletions
  1. 26 1
      bayes.json
  2. 11 6
      file.go
  3. 0 6
      matrix.go

+ 26 - 1
bayes.json

@@ -1 +1,26 @@
-{}
+{
+ "/favicon.ico": {
+  "BadScore": 0,
+  "GoodScore": 1
+ },
+ "cors": {
+  "BadScore": 0,
+  "GoodScore": 1
+ },
+ "favicon ": {
+  "BadScore": 0,
+  "GoodScore": 1
+ },
+ "guns": {
+  "BadScore": 0,
+  "GoodScore": 1
+ },
+ "penis": {
+  "BadScore": 1,
+  "GoodScore": 0
+ },
+ "wallet": {
+  "BadScore": 1,
+  "GoodScore": 0
+ }
+}

+ 11 - 6
file.go

@@ -47,10 +47,15 @@ func saveBayesToFile() {
 		tmpJSON = []byte(err.Error())
 	}
 
-	statsJSON, err := json.MarshalIndent(Classifier.STATS, "", " ")
-	if err != nil {
-		statsJSON = []byte(err.Error())
-	}
+	var statsREPORT string
+
+	statsREPORT = "\n"
+
+	Classifier.STATS.Range(func(key interface{}, value interface{}) bool {
+
+		statsREPORT += fmt.Sprintf("{\"%s\" : \"%d\"}\n", key.(string), value.(int64))
+		return true
+	})
 
 	dumpfile := os.Getenv("DUMPFILE")
 	if dumpfile == "" {
@@ -58,9 +63,9 @@ func saveBayesToFile() {
 	}
 
 	if DebugLog {
-		log.Println("DUMP: ", string(tmpJSON))
+		log.Println("DUMP: ", string(tmpJSON)+statsREPORT)
 	} else {
-		writeToFile(dumpfile, string(tmpJSON)+string(statsJSON))
+		writeToFile(dumpfile, string(tmpJSON)+statsREPORT)
 
 		log.Println("File saved: ", dumpfile)
 	}

+ 0 - 6
matrix.go

@@ -111,20 +111,14 @@ func (c *ByClassifier) Posterior(hdr string) map[string]float64 {
 	var hBadM, hGoodM float64
 
 	for _, tk := range tokens {
-
 		if val, ok := c.Matrix.bScores[tk]; ok {
-			log.Println("Classifier found: ", tk)
 			if val.BadScore > 0 {
 				hBadM += val.BadScore * math.Log2(val.BadScore)
-
 			}
-
 			if val.GoodScore > 0 {
 				hGoodM += val.GoodScore * math.Log2(val.GoodScore)
-
 			}
 		}
-
 	}
 
 	hBadM = math.Log2(lenTokens) - (hBadM / lenTokens)