diff --git a/README.md b/README.md index f84e1be..781e0d8 100644 --- a/README.md +++ b/README.md @@ -127,6 +127,43 @@ returns the same anagrams: ] ``` +### Wildcards + +Find the single words from the given wildcard (use `?` or `.` for a single +character or `*` for a sequence) - at the REPL: + +```clojure +(use 'ars-magna.dict) +(use 'ars-magna.solver) + +(-> + (load-word-list :en-GB) + (wildcard "c..p..e") + sort) +; ("compare" "compete" "compile" +; "compose" "compote" "compute" +; "coppice" "cowpoke" "cripple" + +or querying the web service for the word 'compute': + + $ curl -s http://localhost:3000/wildcard/c..p..e | jq . + +returns the same words: + +```json +[ + "compare", + "compete", + "compile", + "compose", + "compote", + "compute", + "coppice", + "cowpoke", + "cripple" +] + + ## References * https://en.wikipedia.org/wiki/Letter_frequency#Relative_frequencies_of_letters_in_the_English_language diff --git a/src/ars_magna/handler.clj b/src/ars_magna/handler.clj index 899242b..3cb490a 100644 --- a/src/ars_magna/handler.clj +++ b/src/ars_magna/handler.clj @@ -49,7 +49,15 @@ (longest (get-in (make-indexes :en-GB) [:index :sorted-letter]) (clean word) - (min-size req 4))))))) + (min-size req 4)))))) + + (GET "/wildcard/:word" [word :as req] + (json-exception-handler + (to-json identity + (sort + (wildcard + (get-in (make-indexes :en-GB) [:dict]) + word)))))) (def app (-> diff --git a/src/ars_magna/solver.clj b/src/ars_magna/solver.clj index 7fe442d..721ed2c 100644 --- a/src/ars_magna/solver.clj +++ b/src/ars_magna/solver.clj @@ -24,3 +24,18 @@ (range min-size (inc (count word))) (mapcat #(map sort (c/combinations word %))) (mapcat index))) + +(defn- case-insensitive [re] (str "(?i)" re)) + +(defn- assemble-regex [word] + (-> + word + (s/replace "?" ".") + (s/replace "*" ".+") + case-insensitive + re-pattern)) + +(defn wildcard [dict word] + (println word) + (println (assemble-regex word)) + (filter (partial re-matches (assemble-regex word)) dict))