From e3efc283bed46b3b977efb6d20f7f98c400a8a55 Mon Sep 17 00:00:00 2001 From: Morgan Date: Mon, 9 Mar 2015 05:05:19 +0000 Subject: [PATCH] Lookup table support! --- resources/pronouns.tab | 2 ++ src/pronouns/web.clj | 50 +++++++++++++++++++++++++++--------------- 2 files changed, 34 insertions(+), 18 deletions(-) create mode 100644 resources/pronouns.tab diff --git a/resources/pronouns.tab b/resources/pronouns.tab new file mode 100644 index 0000000..890535b --- /dev/null +++ b/resources/pronouns.tab @@ -0,0 +1,2 @@ +ze hir hirs hirs hirself +ze zir zir zirs zirself diff --git a/src/pronouns/web.clj b/src/pronouns/web.clj index 24588c0..971c69b 100644 --- a/src/pronouns/web.clj +++ b/src/pronouns/web.clj @@ -11,34 +11,48 @@ [ring.adapter.jetty :as jetty] [environ.core :refer [env]])) -(def config {:default-server-port 5000}) +(def config {:default-server-port 5000 + :pronoun-table-path "resources/pronouns.tab"}) + +(defn slurp-tabfile [path] + (let [lines (s/split (slurp path) #"\n")] + (map #(s/split % #"\t") lines))) + +(defn lookup [inputs] + (let [pronouns-table (slurp-tabfile (:pronoun-table-path config)) + n (count inputs) + filtered-table (filter #(= inputs (take n %)) pronouns-table)] + (first filtered-table))) + +(defn parse-pronouns-with-lookup [pronouns-string] + (let [inputs (s/split pronouns-string #"/") + n (count inputs)] + (if (>= n 5) + (take 5 inputs) + (lookup inputs)))) (defn render-examples-page - [subject object possessive-determiner possessive-pronoun reflexive] - (s/join "\n" - [(str subject " went to the park") - (str "I went with " object) - (str subject " brought " possessive-determiner " frisbee") - (str "at least I think it was " possessive-pronoun) - (str subject " threw it to " reflexive)])) + ([subject object possessive-determiner possessive-pronoun reflexive] + (s/join "\n" + [(str subject " went to the park") + (str "I went with " object) + (str subject " brought " possessive-determiner " frisbee") + (str "at least I think it was " possessive-pronoun) + (str subject " threw it to " reflexive)])) + ([error-message] + error-message)) (defroutes app-routes (GET "/" [] {:status 200 :headers {"Content-Type" "text/plain"} :body "a blurb explaining how to use this site"}) - (GET "/:subject/:object/:possessive-determiner/:possessive-pronoun/:reflexive" {params :params} + (GET "/*" {params :params} {:status 200 :headers {"Content-Type" "text/plain"} - ;; ew this is super gross there's certainly a better way - :body (let [{:keys - [subject object possessive-determiner possessive-pronoun reflexive]} - params] - (render-examples-page subject - object - possessive-determiner - possessive-pronoun - reflexive))}) + :body (let [pronouns (parse-pronouns-with-lookup {:* params})] + (apply render-examples-page pronouns))}) + (ANY "*" [] (route/not-found (slurp (io/resource "404.html")))))