From 15dab89a9f360d2439e395fe105be5252f6c4c1f Mon Sep 17 00:00:00 2001 From: Jereme Corrado Date: Wed, 3 Oct 2018 11:09:08 -0400 Subject: [PATCH 01/11] Fixed Docker Compose env var Slack example --- doc/GETTING_STARTED.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/GETTING_STARTED.md b/doc/GETTING_STARTED.md index 89ea2bb2..727f33ae 100644 --- a/doc/GETTING_STARTED.md +++ b/doc/GETTING_STARTED.md @@ -26,8 +26,8 @@ config like: ```yaml environment: - - YB_ADAPTERS_MYSLACK_TYPE=Slack - - YB_ADAPTERS_MYSLACK_TOKEN=xoxb-my-token + - YB_ADAPTERS_SLACK_TYPE=slack + - YB_ADAPTERS_SLACK_TOKEN=xoxb-my-token - YB_DB_URL=postgresql://yetibot:yetibot@postgres:5432/yetibot ``` From e89a6ad40b2c766ff6f5be8024acaf9254b1636c Mon Sep 17 00:00:00 2001 From: Jereme Corrado Date: Fri, 12 Oct 2018 16:42:21 -0400 Subject: [PATCH 02/11] Added pirate cmd exploring a couple of ideas for #740. --- resources/pirate/dict.edn | 112 ++++++++++++++++++++++++++++++++ resources/pirate/flavor.edn | 32 +++++++++ src/yetibot/commands/pirate.clj | 107 ++++++++++++++++++++++++++++++ 3 files changed, 251 insertions(+) create mode 100644 resources/pirate/dict.edn create mode 100644 resources/pirate/flavor.edn create mode 100644 src/yetibot/commands/pirate.clj diff --git a/resources/pirate/dict.edn b/resources/pirate/dict.edn new file mode 100644 index 00000000..46763177 --- /dev/null +++ b/resources/pirate/dict.edn @@ -0,0 +1,112 @@ +;; Dictionary lifted from: https://github.com/stevehodges/talk_like_a_pirate + +{"address" "port 'o call" + "admin" "helm" + "am" "be" + "an" "a" + "and" "n'" + "are" "be" + "award" "prize" + "beer" "grog" + "before" "afore" + "belief" "creed" + "between" "betwixt" + "big" "vast" + "boy" "lad" + "boys" "laddies" + "boss" "admiral" + "bourbon" "rum" + "box" "barrel" + "business" "company" + "businesses" "companies" + "calling" "callin'" + "canada" "Great North" + "cash" "doubloons" + "cheat" "hornswaggle" + "comments" "yer words" + "country" "land" + "dashboard" "shanty" + "disconnect" "keelhaul" + "do" "d'" + "dollar" "doubloon" + "dude" "pirate" + "employee" "crew" + "everyone" "all hands" + "eye" "eye-patch" + "family" "kin" + "fee" "debt" + "female" "wench" + "for" "fer" + "friend" "shipmate" + "gin" "rum" + "girl" "lass" + "girls" "lassies" + "go" "sail" + "good" "jolly good" + "group" "maties" + "hand" "hook" + "hello" "ahoy" + "hey" "ahoy" + "hotel" "inn" + "i'm" "i be" + "internet" "series o' tubes" + "invalid" "sunk" + "is" "be" + "island" "isle" + "isn't" "be not" + "it's" "'tis" + "jail" "brig" + "kill" "keelhaul" + "leg" "peg" + "lady" "lass" + "logout" "walk the plank" + "male" "brigand" + "man" "scallwag" + "manager" "admiral" + "money" "doubloons" + "month" "moon" + "my" "me" + "never" "nary" + "no" "nay" + "of" "o'" + "over" "o'er" + "page" "parchment" + "person" "pirate" + "posted" "tacked to the yardarm" + "president" "king" + "prison" "brig" + "quickly" "smartly" + "really" "verily" + "relatives" "kin" + "religion" "creed" + "role" "job" + "say" "cry" + "seconds" "ticks o' tha clock" + "shipping" "cargo" + "small" "puny" + "soldier" "sailor" + "sorry" "yarr" + "spouse" "ball 'n' chain" + "state" "land" + "supervisor" "Cap'n" + "that's" "that be" + "the" "tha" + "them" "'em" + "this" "dis" + "to" "t'" + "vodka" "rum" + "we" "our jolly crew" + "we're" "we's" + "wine" "grog" + "whiskey" "rum" + "whisky" "rum" + "with" "wit'" + "woman" "wench" + "work" "duty" + "yah" "aye" + "yeah" "aye" + "yes" "aye" + "you" "ye" + "you're" "you be" + "you've" "ye" + "your" "yer"} diff --git a/resources/pirate/flavor.edn b/resources/pirate/flavor.edn new file mode 100644 index 00000000..89482f02 --- /dev/null +++ b/resources/pirate/flavor.edn @@ -0,0 +1,32 @@ +["avast" + "splice the mainbrace" + "shiver me timbers" + "ahoy" + "arrrrr" + "arrgh" + "yo ho ho" + "yarrr" + "eh" + "arrrghhh" + "arrr" + "ahoy matey" + "prepare to be boarded" + "hoist the mizzen" + "blow me down" + "swab the poop deck" + "ye landlubber" + "bring 'er alongside" + "hang 'im from the yardarm" + "blow the man down" + "let go and haul" + "heave to" + "take no prisoners" + "belay that" + "me bucko" + "lock 'im in irons" + "and a bottle 'o rum" + "and donae spare the whip" + "pass the grog" + "and swab the deck" + "fire the cannon" + "sleep with t' fishes"] diff --git a/src/yetibot/commands/pirate.clj b/src/yetibot/commands/pirate.clj new file mode 100644 index 00000000..af971861 --- /dev/null +++ b/src/yetibot/commands/pirate.clj @@ -0,0 +1,107 @@ +(ns yetibot.commands.pirate + (:require + [yetibot.core.hooks :refer [cmd-hook]] + [clojure.java.io :as io] + [clojure.edn :as edn] + [clojure.string :as str] + [clj-time.core :as t])) + +;; TODO - Let's derive this from properties of the requesting user. I +;; think this is pretty straightforward with Slack but I have to give +;; IRC some more thought. TZ might be an input to how we sort +;; recommended locations, for Issue #740 - Weather API migration. +(def local-tz "America/New_York") + +;; TODO - We'll use this in Issue #740, too. Should probably move to +;; yetibot.core.util or similar... +(defn get-var + [resource-file] + (-> (io/resource resource-file) + slurp + edn/read-string)) + +(def dict (get-var "pirate/dict.edn")) +(def flavor (get-var "pirate/flavor.edn")) + +(defn wrap-punctuation + "Expects a fn, f, and returns a fn taking one arg: a string. We strip + trailing punctuation before calling the wrapped fn f, replacing on + the return of f." + [f] + (fn [s] + (let [[_ text punc] (re-matches #"^(.*?)?([\.!?,:]+)?$" s)] + (str (f text) punc)))) + +(defn wrap-capitalization + "Expects a fn, f, and returns a fn taking one arg: a string. We + upper-case the first char of the return of the wrapped fn, f, if the + string had an initial upper-case char." + [f] + (fn [s] + (if (Character/isUpperCase (first s)) + (str/replace-first (f s) #"." str/upper-case) + (f s)))) + +(defn sub-word + [s] + (let [s-lc (str/lower-case s)] + (if (contains? dict s-lc) + (get dict s-lc) + s))) + +(defn to-pirate + [s] + (->> (str/split s #"\s+") + (map + (-> sub-word + wrap-punctuation + wrap-capitalization)) + (str/join " "))) + +;; +;; Add some extra flavor +;; +(defn probability + "Return probability, by hour, for configured TZ." + [] + (let [hour (-> (t/to-time-zone (t/now) (t/time-zone-for-id local-tz)) + t/hour)] + (nth (concat (repeat 8 0.95) + (repeat 8 0.25) + (repeat 8 0.75)) + hour))) + +(defn suffix-flavor + "Possibly suffix random pirate flavor, for given probability." + [s prob] + (if (< (rand) prob) + (let [flavor (get flavor (-> (count flavor) rand int))] + (str/replace-first s + #"[\.!?]*$" + #(format ", %s%s" flavor %))) + s)) + +(defn slurrr + "I'm not drunk, you're drunk." + [s prob] + (let [max-repeat 2] + (if (< (rand) prob) + (str/replace s + #"[alr]" + (fn [c] + (apply str (repeat (-> (rand max-repeat) + int + inc) c)))) + s))) + +(defn pirate-cmd + "pirate # translate string into proper pirate, yar " + {:yb/cat #{:info}} + [{:keys [match]}] + (let [prob (probability)] + (-> (to-pirate match) + (suffix-flavor prob) + (slurrr prob)))) + +(cmd-hook #"pirate" + #".+" pirate-cmd) From e24cfe3a849be7ae42220f06558326c4fb9f2508 Mon Sep 17 00:00:00 2001 From: Jereme Corrado Date: Sat, 13 Oct 2018 11:20:50 -0400 Subject: [PATCH 03/11] Added tests for pirate command --- test/yetibot/test/commands/pirate.clj | 33 +++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 test/yetibot/test/commands/pirate.clj diff --git a/test/yetibot/test/commands/pirate.clj b/test/yetibot/test/commands/pirate.clj new file mode 100644 index 00000000..9f488480 --- /dev/null +++ b/test/yetibot/test/commands/pirate.clj @@ -0,0 +1,33 @@ +(ns yetibot.test.commands.pirate + (:require + [midje.sweet :refer [facts fact =>]] + [clojure.string :as str] + [yetibot.commands.pirate :refer :all])) + +(facts "about lower-level wrapper fns" + (fact "wrap-punctuation preserves punctuation even when wrapped fn alters str" + ((wrap-punctuation (fn [_] str "foo")) "bar!") => "foo!") + (fact "wrap-capitalization preserves captilization when when wrapped fn lower-cases str" + ((wrap-capitalization str/lower-case) "Foo") => "Foo")) + +(fact "to-pirate translates strings, preserving captilization and punctuation" + (to-pirate "jello world") => "jello world" + (to-pirate "hello world") => "ahoy world" + (to-pirate "hello world admin") => "ahoy world helm" + (to-pirate "hello world admin!") => "ahoy world helm!" + (to-pirate "hello world admin!?.,:") => "ahoy world helm!?.,:" + (to-pirate "!?.,:") => "!?.,:" + (to-pirate "Hello world admin") => "Ahoy world helm" + (to-pirate "Hello World Admin") => "Ahoy World Helm" + (to-pirate "HeLlO WoRlD admin") => "Ahoy WoRlD helm" + (to-pirate "HeLlO WoRlD admin!!") => "Ahoy WoRlD helm!!") + +(fact "suffix-flavor respects supplied probability constant" + (suffix-flavor "foo" 0) => "foo" + (suffix-flavor "foo" 1) => #"foo,\s+[^\s]+") + +(def test-str "the quick brown fox jumps over the lazy dog") + +(fact "slurrr respects supplied probability constant" + (slurrr test-str 0) => test-str + (slurrr test-str 1) => #"([alr])\1") From 8590aebded1b97dbaeed116a66f83b38241abeed Mon Sep 17 00:00:00 2001 From: Jereme Corrado Date: Sat, 13 Oct 2018 11:28:32 -0400 Subject: [PATCH 04/11] Cleanup --- test/yetibot/test/commands/pirate.clj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/yetibot/test/commands/pirate.clj b/test/yetibot/test/commands/pirate.clj index 9f488480..c59f22ad 100644 --- a/test/yetibot/test/commands/pirate.clj +++ b/test/yetibot/test/commands/pirate.clj @@ -6,7 +6,7 @@ (facts "about lower-level wrapper fns" (fact "wrap-punctuation preserves punctuation even when wrapped fn alters str" - ((wrap-punctuation (fn [_] str "foo")) "bar!") => "foo!") + ((wrap-punctuation (fn [_] "foo")) "bar!") => "foo!") (fact "wrap-capitalization preserves captilization when when wrapped fn lower-cases str" ((wrap-capitalization str/lower-case) "Foo") => "Foo")) From 811b3aa17327901c77fbe5870a3f507578280396 Mon Sep 17 00:00:00 2001 From: Jereme Corrado Date: Sat, 13 Oct 2018 11:44:56 -0400 Subject: [PATCH 05/11] Added additional attribution of piratisms --- resources/pirate/dict.edn | 3 ++- resources/pirate/flavor.edn | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/resources/pirate/dict.edn b/resources/pirate/dict.edn index 46763177..a2f85db9 100644 --- a/resources/pirate/dict.edn +++ b/resources/pirate/dict.edn @@ -1,4 +1,5 @@ -;; Dictionary lifted from: https://github.com/stevehodges/talk_like_a_pirate +;; Dictionary initially lifted from +;; https://github.com/stevehodges/talk_like_a_pirate {"address" "port 'o call" "admin" "helm" diff --git a/resources/pirate/flavor.edn b/resources/pirate/flavor.edn index 89482f02..f2765243 100644 --- a/resources/pirate/flavor.edn +++ b/resources/pirate/flavor.edn @@ -1,3 +1,6 @@ +;; Flavor initially lifted from +;; https://github.com/stevehodges/talk_like_a_pirate + ["avast" "splice the mainbrace" "shiver me timbers" From 0af31697a80398bc133965ee3352f75f6693682f Mon Sep 17 00:00:00 2001 From: Jereme Corrado Date: Sat, 13 Oct 2018 13:44:49 -0400 Subject: [PATCH 06/11] Integrated feedback from @devth in PR #747; plus regex cleanup --- src/yetibot/commands/pirate.clj | 11 ++++------- test/yetibot/test/commands/pirate.clj | 5 +++-- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/yetibot/commands/pirate.clj b/src/yetibot/commands/pirate.clj index af971861..297bfa0d 100644 --- a/src/yetibot/commands/pirate.clj +++ b/src/yetibot/commands/pirate.clj @@ -29,7 +29,7 @@ the return of f." [f] (fn [s] - (let [[_ text punc] (re-matches #"^(.*?)?([\.!?,:]+)?$" s)] + (let [[_ text punc] (re-matches #"^(.*?)?([.!?,:]+)?$" s)] (str (f text) punc)))) (defn wrap-capitalization @@ -44,10 +44,7 @@ (defn sub-word [s] - (let [s-lc (str/lower-case s)] - (if (contains? dict s-lc) - (get dict s-lc) - s))) + (get dict (str/lower-case s) s)) (defn to-pirate [s] @@ -75,9 +72,9 @@ "Possibly suffix random pirate flavor, for given probability." [s prob] (if (< (rand) prob) - (let [flavor (get flavor (-> (count flavor) rand int))] + (let [flavor (get flavor (rand-nth flavor))] (str/replace-first s - #"[\.!?]*$" + #"[.!?]*$" #(format ", %s%s" flavor %))) s)) diff --git a/test/yetibot/test/commands/pirate.clj b/test/yetibot/test/commands/pirate.clj index c59f22ad..6f741786 100644 --- a/test/yetibot/test/commands/pirate.clj +++ b/test/yetibot/test/commands/pirate.clj @@ -23,8 +23,9 @@ (to-pirate "HeLlO WoRlD admin!!") => "Ahoy WoRlD helm!!") (fact "suffix-flavor respects supplied probability constant" - (suffix-flavor "foo" 0) => "foo" - (suffix-flavor "foo" 1) => #"foo,\s+[^\s]+") + (suffix-flavor "foo" 0) => "foo" + (suffix-flavor "foo" 1) => #"^foo,\s+[^\s]+" + (suffix-flavor "foo." 1) => #"^foo,\s+[^\s]+?\.$") (def test-str "the quick brown fox jumps over the lazy dog") From 92160afdd3552d835478a91f41033f9c5f9cbfdd Mon Sep 17 00:00:00 2001 From: Jereme Corrado Date: Sat, 13 Oct 2018 23:14:09 -0400 Subject: [PATCH 07/11] Pull repetitive probability test out of fns --- src/yetibot/commands/pirate.clj | 41 ++++++++++++++------------- test/yetibot/test/commands/pirate.clj | 16 ++++++----- 2 files changed, 31 insertions(+), 26 deletions(-) diff --git a/src/yetibot/commands/pirate.clj b/src/yetibot/commands/pirate.clj index 297bfa0d..773ff299 100644 --- a/src/yetibot/commands/pirate.clj +++ b/src/yetibot/commands/pirate.clj @@ -69,27 +69,30 @@ hour))) (defn suffix-flavor - "Possibly suffix random pirate flavor, for given probability." - [s prob] - (if (< (rand) prob) - (let [flavor (get flavor (rand-nth flavor))] - (str/replace-first s - #"[.!?]*$" - #(format ", %s%s" flavor %))) - s)) + "Suffix random pirate flavor." + [s] + (let [flavor (rand-nth flavor)] + (str/replace-first s + #"[.!?]*$" + #(format ", %s%s" flavor %)))) (defn slurrr "I'm not drunk, you're drunk." - [s prob] + [s] (let [max-repeat 2] - (if (< (rand) prob) - (str/replace s - #"[alr]" - (fn [c] - (apply str (repeat (-> (rand max-repeat) - int - inc) c)))) - s))) + (str/replace s + #"[alr]" + (fn [c] + (apply str (repeat (-> (rand max-repeat) + int + inc) c)))))) + +(defn if-prob + "Optionally apply fn f to string s, based on probability prob" + [s f prob] + (if (< (rand) prob) + (f s) + s)) (defn pirate-cmd "pirate # translate string into proper pirate, yar " @@ -97,8 +100,8 @@ [{:keys [match]}] (let [prob (probability)] (-> (to-pirate match) - (suffix-flavor prob) - (slurrr prob)))) + (if-prob suffix-flavor prob) + (if-prob slurrr prob)))) (cmd-hook #"pirate" #".+" pirate-cmd) diff --git a/test/yetibot/test/commands/pirate.clj b/test/yetibot/test/commands/pirate.clj index 6f741786..117da954 100644 --- a/test/yetibot/test/commands/pirate.clj +++ b/test/yetibot/test/commands/pirate.clj @@ -22,13 +22,15 @@ (to-pirate "HeLlO WoRlD admin") => "Ahoy WoRlD helm" (to-pirate "HeLlO WoRlD admin!!") => "Ahoy WoRlD helm!!") -(fact "suffix-flavor respects supplied probability constant" - (suffix-flavor "foo" 0) => "foo" - (suffix-flavor "foo" 1) => #"^foo,\s+[^\s]+" - (suffix-flavor "foo." 1) => #"^foo,\s+[^\s]+?\.$") +(fact "suffix-flavor suffixes something" + (suffix-flavor "foo") => #"^foo,\s+[^\s]+" + (suffix-flavor "foo.") => #"^foo,\s+.+\.$") (def test-str "the quick brown fox jumps over the lazy dog") -(fact "slurrr respects supplied probability constant" - (slurrr test-str 0) => test-str - (slurrr test-str 1) => #"([alr])\1") +(fact "slurrr permutes text" + (slurrr test-str) => #"([alr])\1") + +(fact "if-prob respects probability constant" + (if-prob 0 inc 0) => 0 + (if-prob 0 inc 1) => 1) From d1ce010424cd5057a851585c9836d767f7ee67a1 Mon Sep 17 00:00:00 2001 From: Jereme Corrado Date: Sun, 14 Oct 2018 08:20:52 -0400 Subject: [PATCH 08/11] slurrr now slurs n perc+fuzz of slurrable words, distributed randomly The old version of slurrr would randomly suffix 0-2 additional letters (the *slurring*) to all applicable words. This had a nice realistic effect - much better than just slurring all words. However, it also meant there was a chance that none would be slurred, which looked janky and intermittently broke tests. This new approach guarantees *some* (percent + fuzz) or potentials candidates will be slurred. --- src/yetibot/commands/pirate.clj | 50 ++++++++++++++++++++++----- test/yetibot/test/commands/pirate.clj | 3 +- 2 files changed, 43 insertions(+), 10 deletions(-) diff --git a/src/yetibot/commands/pirate.clj b/src/yetibot/commands/pirate.clj index 773ff299..f777d83f 100644 --- a/src/yetibot/commands/pirate.clj +++ b/src/yetibot/commands/pirate.clj @@ -48,12 +48,12 @@ (defn to-pirate [s] - (->> (str/split s #"\s+") + (->> (str/split s #"\b") (map (-> sub-word wrap-punctuation wrap-capitalization)) - (str/join " "))) + (apply str))) ;; ;; Add some extra flavor @@ -76,16 +76,48 @@ #"[.!?]*$" #(format ", %s%s" flavor %)))) +(def slurr-re #"[alr]") + +(defn- mk-slurr-map + "Return randomly ordered v of true and nil. The number of trues is a + configurable percentage of n, plus some fuzz. The balance of n are + nils." + [n] + (let [perc 0.7 + fuzz (rand 0.3) + min-t (-> (* perc n) Math/round int) + max-f (- n min-t) + t (+ min-t (-> (* fuzz max-f) Math/round int)) + f (- n t)] + (shuffle (concat (repeat t true) + (repeat f nil))))) + +(defn- slurrable? + "Return s if it's slurrable, nil if not." + [s] + (if (re-find slurr-re s) s nil)) + +(defn- slurr-word + [s] + (str/replace s + slurr-re + (fn [c] + (apply str (repeat (rand-nth [2 3]) c))))) + (defn slurrr "I'm not drunk, you're drunk." [s] - (let [max-repeat 2] - (str/replace s - #"[alr]" - (fn [c] - (apply str (repeat (-> (rand max-repeat) - int - inc) c)))))) + (let [words (str/split s #"\b") + cnt (count (filter slurrable? words)) + sm (mk-slurr-map cnt)] + (loop [word (first words), tail (rest words), sm sm, accum []] + (if (nil? word) + (apply str accum) + (if (slurrable? word) + (if (nil? (first sm)) + (recur (first tail) (rest tail) (rest sm) (conj accum word)) + (recur (first tail) (rest tail) (rest sm) (conj accum (slurr-word word)))) + (recur (first tail) (rest tail) sm (conj accum word))))))) (defn if-prob "Optionally apply fn f to string s, based on probability prob" diff --git a/test/yetibot/test/commands/pirate.clj b/test/yetibot/test/commands/pirate.clj index 117da954..3b5bba82 100644 --- a/test/yetibot/test/commands/pirate.clj +++ b/test/yetibot/test/commands/pirate.clj @@ -20,7 +20,8 @@ (to-pirate "Hello world admin") => "Ahoy world helm" (to-pirate "Hello World Admin") => "Ahoy World Helm" (to-pirate "HeLlO WoRlD admin") => "Ahoy WoRlD helm" - (to-pirate "HeLlO WoRlD admin!!") => "Ahoy WoRlD helm!!") + (to-pirate "HeLlO WoRlD admin!!") => "Ahoy WoRlD helm!!" + (to-pirate "hello world admin") => "ahoy world helm") (fact "suffix-flavor suffixes something" (suffix-flavor "foo") => #"^foo,\s+[^\s]+" From 7523101c789695478359cfad62d92069147d0859 Mon Sep 17 00:00:00 2001 From: Jereme Corrado Date: Sun, 14 Oct 2018 09:08:56 -0400 Subject: [PATCH 09/11] Refactoring --- src/yetibot/commands/pirate.clj | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/yetibot/commands/pirate.clj b/src/yetibot/commands/pirate.clj index f777d83f..09334778 100644 --- a/src/yetibot/commands/pirate.clj +++ b/src/yetibot/commands/pirate.clj @@ -76,18 +76,18 @@ #"[.!?]*$" #(format ", %s%s" flavor %)))) -(def slurr-re #"[alr]") +(def slur-re #"[alr]") -(defn- mk-slurr-map +(defn- mk-slur-map "Return randomly ordered v of true and nil. The number of trues is a configurable percentage of n, plus some fuzz. The balance of n are nils." [n] (let [perc 0.7 fuzz (rand 0.3) - min-t (-> (* perc n) Math/round int) + min-t (* perc n) max-f (- n min-t) - t (+ min-t (-> (* fuzz max-f) Math/round int)) + t (-> (* fuzz max-f) (+ min-t) Math/round) f (- n t)] (shuffle (concat (repeat t true) (repeat f nil))))) @@ -95,12 +95,12 @@ (defn- slurrable? "Return s if it's slurrable, nil if not." [s] - (if (re-find slurr-re s) s nil)) + (if (re-find slur-re s) s nil)) -(defn- slurr-word +(defn- slur-word [s] (str/replace s - slurr-re + slur-re (fn [c] (apply str (repeat (rand-nth [2 3]) c))))) @@ -109,15 +109,15 @@ [s] (let [words (str/split s #"\b") cnt (count (filter slurrable? words)) - sm (mk-slurr-map cnt)] - (loop [word (first words), tail (rest words), sm sm, accum []] + sm (mk-slur-map cnt)] + (loop [[word & tail] words, sm sm, accum []] (if (nil? word) (apply str accum) (if (slurrable? word) (if (nil? (first sm)) - (recur (first tail) (rest tail) (rest sm) (conj accum word)) - (recur (first tail) (rest tail) (rest sm) (conj accum (slurr-word word)))) - (recur (first tail) (rest tail) sm (conj accum word))))))) + (recur tail (rest sm) (conj accum word)) + (recur tail (rest sm) (conj accum (slur-word word)))) + (recur tail sm (conj accum word))))))) (defn if-prob "Optionally apply fn f to string s, based on probability prob" From 18443b6f511dab9b67e7f50f0626c096449c9a3d Mon Sep 17 00:00:00 2001 From: Jereme Corrado Date: Sun, 14 Oct 2018 09:58:36 -0400 Subject: [PATCH 10/11] Minor refactoring --- src/yetibot/commands/pirate.clj | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/yetibot/commands/pirate.clj b/src/yetibot/commands/pirate.clj index 09334778..822baecd 100644 --- a/src/yetibot/commands/pirate.clj +++ b/src/yetibot/commands/pirate.clj @@ -108,8 +108,7 @@ "I'm not drunk, you're drunk." [s] (let [words (str/split s #"\b") - cnt (count (filter slurrable? words)) - sm (mk-slur-map cnt)] + sm (mk-slur-map (count (filter slurrable? words)))] (loop [[word & tail] words, sm sm, accum []] (if (nil? word) (apply str accum) @@ -120,7 +119,7 @@ (recur tail sm (conj accum word))))))) (defn if-prob - "Optionally apply fn f to string s, based on probability prob" + "Optionally apply fn f to string s, based on probability prob." [s f prob] (if (< (rand) prob) (f s) From 569e12de102b01df62a907c6efea5050c80283cc Mon Sep 17 00:00:00 2001 From: Jereme Corrado Date: Sun, 14 Oct 2018 10:18:29 -0400 Subject: [PATCH 11/11] Minor refactoring --- src/yetibot/commands/pirate.clj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/yetibot/commands/pirate.clj b/src/yetibot/commands/pirate.clj index 822baecd..12ac355c 100644 --- a/src/yetibot/commands/pirate.clj +++ b/src/yetibot/commands/pirate.clj @@ -29,7 +29,7 @@ the return of f." [f] (fn [s] - (let [[_ text punc] (re-matches #"^(.*?)?([.!?,:]+)?$" s)] + (let [[_ text punc] (re-matches #"(.*?)?([.!?,:]+)?" s)] (str (f text) punc)))) (defn wrap-capitalization