Skip to content

Commit

Permalink
Add wildcard search
Browse files Browse the repository at this point in the history
  • Loading branch information
rm-hull committed Apr 11, 2018
1 parent a128068 commit a258b42
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion src/ars_magna/handler.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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
(->
Expand Down
15 changes: 15 additions & 0 deletions src/ars_magna/solver.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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))

0 comments on commit a258b42

Please sign in to comment.