Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avast ye string handlers #747

Merged
merged 15 commits into from Oct 15, 2018
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
112 changes: 112 additions & 0 deletions 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"}
32 changes: 32 additions & 0 deletions 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"
jcorrado marked this conversation as resolved.
Show resolved Hide resolved
"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"]
107 changes: 107 additions & 0 deletions 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")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. I think I've hardcoded Pacific Time elsewhere but obtaining from Slack user would be clever.


;; 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)
jcorrado marked this conversation as resolved.
Show resolved Hide resolved
(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)))
jcorrado marked this conversation as resolved.
Show resolved Hide resolved

(defn suffix-flavor
"Possibly suffix random pirate flavor, for given probability."
[s prob]
(if (< (rand) prob)
(let [flavor (get flavor (-> (count flavor) rand int))]
jcorrado marked this conversation as resolved.
Show resolved Hide resolved
(str/replace-first s
#"[\.!?]*$"
#(format ", %s%s" flavor %)))
s))

(defn slurrr
"I'm not drunk, you're drunk."
jcorrado marked this conversation as resolved.
Show resolved Hide resolved
[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 <string> # translate string into proper pirate, yar <string>"
{:yb/cat #{:info}}
[{:keys [match]}]
(let [prob (probability)]
(-> (to-pirate match)
(suffix-flavor prob)
(slurrr prob))))

(cmd-hook #"pirate"
#".+" pirate-cmd)