-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
141 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,4 +37,5 @@ | |
:plugins [ | ||
[codox "0.10.3"] | ||
[lein-ring "0.12.2"] | ||
[lein-cljfmt "0.5.7"] | ||
[lein-cloverage "1.0.10"]]}}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,60 @@ | ||
(ns ars-magna.handler | ||
(:require | ||
[clojure.string :as s] | ||
[compojure.handler :as handler] | ||
[compojure.route :as route] | ||
[compojure.core :refer [defroutes GET POST]] | ||
[ring.logger.timbre :as logger.timbre] | ||
[metrics.ring.expose :refer [expose-metrics-as-json]] | ||
[metrics.ring.instrument :refer [instrument]] | ||
[ars-magna.json :refer :all] | ||
[ars-magna.dict :refer :all] | ||
[ars-magna.solver :refer :all])) | ||
[clojure.string :as s] | ||
[compojure.handler :as handler] | ||
[compojure.route :as route] | ||
[compojure.core :refer [defroutes GET POST]] | ||
[ring.logger.timbre :as logger.timbre] | ||
[metrics.ring.expose :refer [expose-metrics-as-json]] | ||
[metrics.ring.instrument :refer [instrument]] | ||
[ars-magna.json :refer :all] | ||
[ars-magna.dict :refer :all] | ||
[ars-magna.solver :refer :all])) | ||
|
||
(defn clean [word] | ||
(-> | ||
word | ||
s/lower-case | ||
(s/replace #"\W" ""))) | ||
word | ||
s/lower-case | ||
(s/replace #"\W" ""))) | ||
|
||
(defn min-size [req default] | ||
(Integer/parseInt | ||
(or | ||
(get-in req [:params :min]) | ||
(str default)))) | ||
(or | ||
(get-in req [:params :min]) | ||
(str default)))) | ||
|
||
(def make-indexes | ||
(memoize | ||
(fn [lang] | ||
(let [dict (load-word-list lang)] | ||
{:dict dict | ||
:index { | ||
:word-length (partition-by-word-length dict) | ||
:sorted-letter (partition-by-letters dict)}})))) | ||
(fn [lang] | ||
(let [dict (load-word-list lang)] | ||
{:dict dict | ||
:index {:word-length (partition-by-word-length dict) | ||
:sorted-letter (partition-by-letters dict)}})))) | ||
|
||
(defroutes app-routes | ||
(GET "/multi-word/:word" [word :as req] | ||
(json-exception-handler | ||
(to-json identity | ||
(sort | ||
(multi-word | ||
(get-in (make-indexes :en-GB) [:index :word-length]) | ||
(clean word) | ||
(min-size req 3)))))) | ||
(to-json identity | ||
(sort | ||
(multi-word | ||
(get-in (make-indexes :en-GB) [:index :word-length]) | ||
(clean word) | ||
(min-size req 3)))))) | ||
|
||
(GET "/longest/:word" [word :as req] | ||
(json-exception-handler | ||
(to-json identity | ||
(sort-by | ||
(juxt (comp - count) identity) | ||
(longest | ||
(get-in (make-indexes :en-GB) [:index :sorted-letter]) | ||
(clean word) | ||
(min-size req 4))))))) | ||
(to-json identity | ||
(sort-by | ||
(juxt (comp - count) identity) | ||
(longest | ||
(get-in (make-indexes :en-GB) [:index :sorted-letter]) | ||
(clean word) | ||
(min-size req 4))))))) | ||
|
||
(def app | ||
(-> | ||
app-routes | ||
(logger.timbre/wrap-with-logger) | ||
(expose-metrics-as-json) | ||
(instrument) | ||
(handler/api))) | ||
(-> | ||
app-routes | ||
(logger.timbre/wrap-with-logger) | ||
(expose-metrics-as-json) | ||
(instrument) | ||
(handler/api))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,26 @@ | ||
(ns ars-magna.solver | ||
(:require | ||
[clojure.string :as s] | ||
[clojure.math.combinatorics :as c] | ||
[ars-magna.dict :refer :all])) | ||
[clojure.string :as s] | ||
[clojure.math.combinatorics :as c] | ||
[ars-magna.dict :refer :all])) | ||
|
||
(defn multi-word | ||
([index word min-size] | ||
(multi-word index word min-size nil)) | ||
|
||
([index word min-size prefix] | ||
(if (empty? word) | ||
(s/trim prefix) | ||
(flatten | ||
(for [w (find-in index word min-size :en-GB)] | ||
(multi-word | ||
index | ||
(remaining-chars word w) | ||
min-size | ||
(str w " " prefix))))))) | ||
(if (empty? word) | ||
(s/trim prefix) | ||
(flatten | ||
(for [w (find-in index word min-size :en-GB)] | ||
(multi-word | ||
index | ||
(remaining-chars word w) | ||
min-size | ||
(str w " " prefix))))))) | ||
|
||
(defn longest [index word min-size] | ||
(->> | ||
(range min-size (inc (count word))) | ||
(mapcat #(map sort (c/combinations word %))) | ||
(mapcat index))) | ||
(range min-size (inc (count word))) | ||
(mapcat #(map sort (c/combinations word %))) | ||
(mapcat index))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.