Skip to content

Commit

Permalink
Reformat with cljfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
rm-hull committed Dec 23, 2017
1 parent 790cea6 commit 1850b92
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 144 deletions.
1 change: 1 addition & 0 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@
:plugins [
[codox "0.10.3"]
[lein-ring "0.12.2"]
[lein-cljfmt "0.5.7"]
[lein-cloverage "1.0.10"]]}})
54 changes: 27 additions & 27 deletions src/ars_magna/dict.clj
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
(ns ars-magna.dict
(:require
[clojure.java.io :as io]
[clojure.string :as s]
[clojure.set :refer [difference]]
[ars-magna.letter-frequencies :refer [rarest-letter]]))
[clojure.java.io :as io]
[clojure.string :as s]
[clojure.set :refer [difference]]
[ars-magna.letter-frequencies :refer [rarest-letter]]))

(defn load-word-list [lang]
(->>
(str "data/" (name lang) "/words")
io/resource
slurp
s/split-lines
(str "data/" (name lang) "/words")
io/resource
slurp
s/split-lines
;(map s/lower-case)
))
))

(defn- partition-words-by [aggregator]
(fn [dict]
(reduce
(fn [acc word]
(update acc (aggregator word) conj word))
{}
dict)))
(reduce
(fn [acc word]
(update acc (aggregator word) conj word))
{}
dict)))

(def partition-by-word-length (partition-words-by count))

Expand All @@ -29,35 +29,35 @@
(defn words-of-size
([index n] (words-of-size index n 0))
([index n min-size]
(if (< n min-size)
nil
(lazy-cat
(index n)
(words-of-size index (dec n) min-size)))))
(if (< n min-size)
nil
(lazy-cat
(index n)
(words-of-size index (dec n) min-size)))))

(defn can-make? [^String source target]
(if (empty? target)
true
(let [c (str (first target))]
(if (.contains source c)
(can-make?
(s/replace-first source c "")
(rest target))
(s/replace-first source c "")
(rest target))
false))))

(defn find-in [index word min-size lang]
(let [rarest-letter (str (rarest-letter word lang))
pred (fn [^String w]
(and
(.contains w rarest-letter)
(can-make? word w)))]
(.contains w rarest-letter)
(can-make? word w)))]
(->>
(words-of-size index (count word) min-size)
(filter pred))))
(words-of-size index (count word) min-size)
(filter pred))))

(defn remaining-chars [word1 word2]
(if (empty? word2)
word1
(recur
(s/replace-first word1 (first word2) "")
(rest word2))))
(s/replace-first word1 (first word2) "")
(rest word2))))
81 changes: 40 additions & 41 deletions src/ars_magna/handler.clj
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)))
30 changes: 15 additions & 15 deletions src/ars_magna/json.clj
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
(ns ars-magna.json
(:require
[ring.util.response :refer [response status content-type charset]]
[clojure.data.json :as json]
[taoensso.timbre :as timbre]))
[ring.util.response :refer [response status content-type charset]]
[clojure.data.json :as json]
[taoensso.timbre :as timbre]))

(defn- value-writer [key value]
(if (instance? java.util.Date value)
Expand All @@ -11,23 +11,23 @@

(defn to-json [f & q]
(->
(apply f q)
(json/write-str :value-fn value-writer :key-fn name)
(response)
(content-type "application/json")
(charset "UTF-8")))
(apply f q)
(json/write-str :value-fn value-writer :key-fn name)
(response)
(content-type "application/json")
(charset "UTF-8")))

(defn json-exception-handler* [f]
(try
(f)
(catch IllegalArgumentException ile
(->
(f)
(catch IllegalArgumentException ile
(->
(to-json identity {:error (.getMessage ile)})
(status 400)))
(catch Exception e
(do
(timbre/error e)
(->
(catch Exception e
(do
(timbre/error e)
(->
(to-json identity {:error (.toString e)})
(status 500))))))

Expand Down
72 changes: 35 additions & 37 deletions src/ars_magna/letter_frequencies.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,45 @@
; From: https://en.wikipedia.org/wiki/Letter_frequency#Relative_frequencies_of_letters_in_the_English_language

(def letter-frequencies
{
:en-GB {
\a 0.08167
\b 0.01492
\c 0.02782
\d 0.04253
\e 0.12702
\f 0.02228
\g 0.02015
\h 0.06094
\i 0.06966
\j 0.00153
\k 0.00772
\l 0.04025
\m 0.02406
\n 0.06749
\o 0.07507
\p 0.01929
\q 0.00095
\r 0.05987
\s 0.06327
\t 0.09056
\u 0.02758
\v 0.00978
\w 0.02361
\x 0.00150
\y 0.01974
\z 0.00074 }})
{:en-GB {\a 0.08167
\b 0.01492
\c 0.02782
\d 0.04253
\e 0.12702
\f 0.02228
\g 0.02015
\h 0.06094
\i 0.06966
\j 0.00153
\k 0.00772
\l 0.04025
\m 0.02406
\n 0.06749
\o 0.07507
\p 0.01929
\q 0.00095
\r 0.05987
\s 0.06327
\t 0.09056
\u 0.02758
\v 0.00978
\w 0.02361
\x 0.00150
\y 0.01974
\z 0.00074}})

(defn by-ascending-frequency [letter-freq]
(->>
letter-freq
(sort-by second)
(map first)))
letter-freq
(sort-by second)
(map first)))

(defn rarest-letter
([word] (rarest-letter word :en-GB))
([word lang]
(let [letters (set word)]
(->>
(get letter-frequencies lang)
(filter #(letters (first %)))
by-ascending-frequency
first))))
(let [letters (set word)]
(->>
(get letter-frequencies lang)
(filter #(letters (first %)))
by-ascending-frequency
first))))
30 changes: 15 additions & 15 deletions src/ars_magna/solver.clj
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)))
7 changes: 3 additions & 4 deletions test/ars_magna/dict_test.clj
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
(ns ars-magna.dict-test
(:require
[clojure.test :refer :all]
[ars-magna.dict :refer :all]))
[clojure.test :refer :all]
[ars-magna.dict :refer :all]))

(def test-dict
["hello" "hat" "gloves" "time" "normally"
"at" "banana" "leaf" "lead" "and" "you"
"melon" "lemon"
])
"melon" "lemon"])

(deftest check-load-word-list
(is (= 99171 (count (load-word-list :en-GB)))))
Expand Down
4 changes: 2 additions & 2 deletions test/ars_magna/letter_frequencies_test.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(ns ars-magna.letter-frequencies-test
(:require
[clojure.test :refer :all]
[ars-magna.letter-frequencies :refer :all]))
[clojure.test :refer :all]
[ars-magna.letter-frequencies :refer :all]))

(deftest check-letter-ordering
(is (= "etaoinshrdlcumwfgypbvkjxqz" ;; From wikipedia
Expand Down
Loading

0 comments on commit 1850b92

Please sign in to comment.