/
suggest.clj
30 lines (26 loc) · 925 Bytes
/
suggest.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
(ns cloudship.util.suggest
(:require [clemence.core :as c]
[clojure.string :as str]))
(defn build-trie [texts]
(c/build-trie texts))
(defn max-dist [text-length]
(case text-length
4 1
5 2
6 3
11 4
20 5
(long (* 0.25 text-length))))
(defn best-suggestions [trie text]
(mapv first (take 5 (sort-by second (c/levenshtein trie text (max-dist (count text)))))))
(defn valid?-or-throw-with-alternatives [throw-name trie text]
(if (empty? (c/levenshtein trie text 0)) ;not in trie
(let [suggestions (best-suggestions trie text)]
(throw (ex-info (str "Got invalid " throw-name " '" text "'. "
(if (empty? suggestions)
""
(str "Did you mean " suggestions)))
{:suggestions suggestions
:input text
:trie true})))
text))