diff --git a/.gitignore b/.gitignore index 978a51a..31aed0c 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ pom.xml.asc /.idea/* marrano-bot.imlconfig.edn db.edn +/.cpcache/ diff --git a/README.org b/README.org index 9d43081..03ce42a 100644 --- a/README.org +++ b/README.org @@ -10,8 +10,7 @@ java -Dconfig="" -jar marrano-bot-0.1.0-SNAPSHOT-standalone. ** Options -``` edn -#+BEGIN_SRC edn +#+BEGIN_SRC clojure {:webhook "https://bot.example.net" :token "botABC:123467"} #+END_SRC diff --git a/src/marrano_bot/db.clj b/src/marrano_bot/db.clj index a3d02d3..1880b0b 100644 --- a/src/marrano_bot/db.clj +++ b/src/marrano_bot/db.clj @@ -151,3 +151,7 @@ (defn inc! [& k] (apply-in! k inc 1)) + +(defn inc-by! + [n & k] + (apply-in! k inc n)) diff --git a/src/marrano_bot/grumpyness.clj b/src/marrano_bot/grumpyness.clj new file mode 100644 index 0000000..74bf5fa --- /dev/null +++ b/src/marrano_bot/grumpyness.clj @@ -0,0 +1,25 @@ +(ns marrano-bot.grumpyness + (:require [marrano-bot.stats :refer [get-all-stats]] + [clojure.string :as string])) + +(def score + [[-3 :russacchiotta :polacchina :potta] + [-2 :bell] + [-1 :amiga :commodore :retro :warez] + [1 :marrano :umme :mj] + [2 :umme :brutt] + [3 :bigdata :coin :chiesa]]) + +(defn calculate-thing-grumpyness + [thing] + (->> + (map #(if (.contains % thing) (first %) 0) + score) + (reduce + 0))) + +(defn calculate-grumpyness + [phrase] + (->> phrase + (get-all-stats) + (map calculate-thing-grumpyness) + (reduce +))) diff --git a/src/marrano_bot/marrano.clj b/src/marrano_bot/marrano.clj index 3195f4f..e2f218f 100644 --- a/src/marrano_bot/marrano.clj +++ b/src/marrano_bot/marrano.clj @@ -5,6 +5,7 @@ (:require [marrano-bot.parse :as p] [marrano-bot.db :as db] [marrano-bot.stats :refer [get-stats-from-phrase]] + [marrano-bot.grumpyness :refer [calculate-grumpyness]] [morse.handlers :as h] [morse.api :as t] [muuntaja.core :as m] @@ -74,6 +75,7 @@ ;; Photo +;; (defn ricorda-photo @@ -91,6 +93,7 @@ #(= (:photo_id %) photo))) ;; Help message +;; (defn- paris-help [] (let [list (->> (keys (db/get-in! [:commands])) @@ -99,7 +102,8 @@ (apply str))] (str "Helpy *paris*:\n\n" list))) -;; links +;; Link +;; (defn- as-json [data] (->> data @@ -136,23 +140,34 @@ :text "🖕 LMGIFY"}] results))))) +;; Grumpyness +;; +(defn update-grumpyness + [username text] + (when (and username text) + (let [grumpyness (calculate-grumpyness text)] + (db/inc-by! grumpyness :grumpyness username)))) + +(defn get-grumpyness + [] + (->> (db/get-in! [:grumpyness]) + (sort-by second) + (map #(str "- _" (first %) "_ *" (second %) "*")) + (s/join "\n"))) + ;; Stats ;; (defn update-stats [text] "increments stats based on spoken words" - (loop [stats (seq (get-stats-from-phrase text))] - (when (first stats) - (debug "stats:" stats) - (db/inc! :stats (first stats))) - (when (next stats) - (recur (next stats))))) + (doseq [stat (get-stats-from-phrase text)] + (db/inc! :stats stat))) (defn get-stats [] (->> (db/get-in! [:stats]) (sort-by second) - (map #(str "- `" (first %) "` :: *" (second %) "*")) + (map #(str "- _" (first %) "_ *" (second %) "*")) (s/join "\n"))) (defn prometheus-metrics @@ -181,9 +196,10 @@ parts)) (defn bot-api - [{{id :id chat-type :type} :chat caption :caption photo :photo text :text}] + [{{username :username} :from {id :id chat-type :type} :chat caption :caption photo :photo text :text}] (when text - (update-stats text)) + (do (update-stats text) + (update-grumpyness username text))) (let [caption (or caption "") text (or text "")] (cond (s/starts-with? text "/paris") @@ -229,6 +245,12 @@ (send-message {:chat_id id :text (get-stats)}) ;; + ;; Grumpyness + ;; + (and text (s/starts-with? text "/grumpy")) + (send-message {:chat_id id + :text (get-grumpyness)}) + ;; ;; /ricorda ;; (s/starts-with? text "/ricorda") diff --git a/src/marrano_bot/stats.clj b/src/marrano_bot/stats.clj index 65348d1..a761bce 100644 --- a/src/marrano_bot/stats.clj +++ b/src/marrano_bot/stats.clj @@ -8,17 +8,23 @@ (def stat-words [[:russacchiotta "russa" "russacchiotta"] [:polacchina "pupy" "pupa" "poska" "polacchina"] + [:potta "figa" "potta" "signorina" "tette" "pheega"] + [:aliemmo "aliemmo" "aliem" "lorenzo" "lallini" "lallo"] + [:gatto "grumpycat" "gatto" "gattino" "bibbiano"] [:lukke "lukke" "luca" "bertolovski"] [:suppah "suppah" "supphppa" "suppahsrv" "munne"] - [:gatto "grumpycat" "gatto" "gattino" "bibbiano"] - [:aliemmo "aliemmo" "aliem" "lorenzo" "lallini"] - [:marrano "marran" "marrans" "marrani" "mrrny"] - [:amiga "amiga" "vampire" "cd32" "a1200" "a600" "acceleratore" "blitter" "aga" "terriblefire" "tf330" "tf530" "warp"] + [:amiga "amiga" "vampire" "cd32" "a1200" "a600" "acceleratore" "blitter" "terriblefire" "tf330" "tf530" "warp"] [:commodore "commodore" "c64" "vic20"] [:retro "spectrum" "speccy" "coleco" "atari" "falcon"] + [:warez "warez" "crack" "key" "chiave"] + [:bigdata "mongo" "elasticsearch" "elastic" "bigdata"] + [:coin "bitcoin" "litecoin" "coin" "musk" "elon" "elon musk" "speculazione"] [:umme "umme" "ummme" "ummmeee" "umm3"] - [:potta "figa" "potta" "signorina" "tette"] - [:bigdata "mongo" "elasticsearch" "elastic" "bigdata"]]) + [:chiesa "dio" "papa" "chiesa" "religione" "porco"] + [:mj "lsd" "mj" "marjuana" "erba" "pianta" "piantina"] + [:marrano "marran" "marrans" "marrani" "mrrny"] + [:brutt "grumpy" "grumpizza" "ummatore" "cattiv" "biden" "trump" "putin" "puteen" "moo" "pastoso" "lento" "manno" "mannosu" "brutto"] + [:bell "brav" "bella" "ciccia" "pizza" "pasta" "vodka" "essi" "essisu" "massisu"]]) (defn calculate-distance [w1 w2] @@ -31,15 +37,18 @@ 0 words)) (defn calculate-rank - [phrase words] - (->> (string/split phrase #"\s+") - (map #(calculate-rank-word % words)) - (apply max))) + [word words] + (->> words + (map #(calculate-rank-word word %)))) (defn get-stats-from-phrase - [phrase] + [word] (->> stat-words - (filterv #(< 0.4 (calculate-rank phrase - (rest %)))) - (map first) - (reduce conj #{}))) + (filterv #(< 0.6 (calculate-rank-word word (rest %)))) + (map first))) + +(defn get-all-stats + [phrase] + (let [words (string/split phrase #"\s+")] + (->> words + (mapcat get-stats-from-phrase))))