summaryrefslogtreecommitdiff
path: root/internal/flood/flood.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/flood/flood.go')
-rw-r--r--internal/flood/flood.go62
1 files changed, 62 insertions, 0 deletions
diff --git a/internal/flood/flood.go b/internal/flood/flood.go
new file mode 100644
index 0000000..b8a71d1
--- /dev/null
+++ b/internal/flood/flood.go
@@ -0,0 +1,62 @@
+package flood
+
+import (
+ "math"
+ "sort"
+ "strings"
+ "unicode"
+ "unicode/utf8"
+)
+
+const (
+ runes = 6
+ words = 6
+ black = "\u24B6\u262D\u272F\u262E\u2721\u5350\u534D\u2719\u0FD5\u0FD6\u16CB\uA5A6\u0FD7\u0FD8"
+)
+
+func entropy(s string) (e float64) {
+ n := make(map[rune]float64)
+ for _, r := range s {
+ n[r] += 1 / float64(len(s))
+ }
+ for _, v := range n {
+ e -= v * math.Log2(v)
+ }
+ return e
+}
+
+func Is(s string) bool {
+ if strings.ContainsAny(s, black) {
+ return true
+ }
+ if utf8.RuneCountInString(s) <= runes {
+ return false
+ }
+ if v := strings.Fields(s); len(v) >= words {
+ return commonWord(v) >= len(v)/2
+ }
+ return entropy(s) <= 1
+}
+
+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 noSpaceCompare(a, b string) bool {
+ dropSpaces := func(r rune) rune {
+ if unicode.IsSpace(r) {
+ return -1
+ }
+ return r
+ }
+ return strings.Map(dropSpaces, a) == strings.Map(dropSpaces, b)
+}