Browse Source

Normalized probs

Loweel 4 years ago
parent
commit
e4a864220c
2 changed files with 19 additions and 27 deletions
  1. 0 26
      bayes.json
  2. 19 1
      classifier.go

+ 0 - 26
bayes.json

@@ -1,26 +0,0 @@
-{
- "/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
- }
-}

+ 19 - 1
classifier.go

@@ -72,7 +72,9 @@ func sanitizeHeaders(s string) string {
 	re := regexp.MustCompile(`[a-zA-Z]{4,32}|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})|([0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12})`)
 	matched := re.FindAllString(s, -1)
 
-	tmpSt := strings.ToLower(strings.Join(matched, " "))
+	uMatched := Unique(matched)
+
+	tmpSt := strings.ToLower(strings.Join(uMatched, " "))
 
 	log.Println("Matched: " + tmpSt)
 
@@ -124,3 +126,19 @@ func formatRequest(req *http.Request) string {
 	return fmt.Sprintf("%s\n", requestDump)
 
 }
+
+//Unique returns unique elements in the string
+func Unique(slice []string) []string {
+	// create a map with all the values as key
+	uniqMap := make(map[string]struct{})
+	for _, v := range slice {
+		uniqMap[v] = struct{}{}
+	}
+
+	// turn the map keys into a slice
+	uniqSlice := make([]string, 0, len(uniqMap))
+	for v := range uniqMap {
+		uniqSlice = append(uniqSlice, v)
+	}
+	return uniqSlice
+}