diff options
author | Dimitri Sokolyuk <demon@dim13.org> | 2015-08-15 14:56:21 +0200 |
---|---|---|
committer | Dimitri Sokolyuk <demon@dim13.org> | 2015-08-15 14:56:21 +0200 |
commit | ef5ab7daa6a5df7794ed0b19a2092009e4adbaf8 (patch) | |
tree | 0aeb92ad8a8860f84e2c94462f8344c45bcf81be /flood.go | |
parent | 2cedfd17b76067124fe1b3455ad0f135a95e5390 (diff) |
Add common word count
Diffstat (limited to 'flood.go')
-rw-r--r-- | flood.go | 38 |
1 files changed, 30 insertions, 8 deletions
@@ -8,10 +8,34 @@ import ( irc "github.com/fluffle/goirc/client" ) -func MedianLength(v []string) int { - if len(v) == 0 { - return 0 +func Flood(s string) bool { + if s == "" { + return false + } + v := strings.Fields(s) + if CommonWord(v) >= len(v)/2 { + return true + } + if len(v) >= 10 && MedianLength(v) == 1 { + return true + } + return false +} + +func CommonWord(v []string) int { + m := make(map[string]int) + for _, w := range v { + m[w]++ + } + l := make([]int, len(m)) + for _, n := range m { + l = append(l, n) } + sort.Sort(sort.Reverse(sort.IntSlice(l))) + return l[0] +} + +func MedianLength(v []string) int { l := make([]int, len(v)) for i, s := range v { l[i] = len(s) @@ -21,10 +45,8 @@ func MedianLength(v []string) int { } func DetectFlood(conn *irc.Conn, line *irc.Line) { - if f := strings.Fields(line.Text()); len(f) > 8 { - if m := MedianLength(f); m < 2 { - log.Println("flood", line.Nick) - conn.Kick(*room, line.Nick, "flood") - } + if Flood(line.Text()) { + log.Println("flood", line.Nick) + conn.Kick(*room, line.Nick, "flood") } } |