Skip to content

Commit

Permalink
Use tree-based command dispatch (#110)
Browse files Browse the repository at this point in the history
* Use print-error

* Move from table based dispatch to tree based dispatch

* Implement Did you mean? based on edit-distance

* Use babashka.cli :require and :validate everywhere
  • Loading branch information
Sohalt authored Nov 15, 2023
1 parent 3e5de9d commit 9c8c595
Show file tree
Hide file tree
Showing 2 changed files with 397 additions and 243 deletions.
28 changes: 28 additions & 0 deletions src/nextjournal/edit_distance.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
(ns nextjournal.edit-distance)

(defn edit-distance [a b]
(let [alen (count a)
blen (count b)
[longlen shortlen a b] (if (> alen blen)
[alen blen a b]
[blen alen b a])
[a' & a-rest] a
[b' & b-rest] b]
(if (zero? shortlen)
longlen
(if (= a' b')
(edit-distance a-rest b-rest)
(+ 1 (min (edit-distance a-rest b-rest)
(edit-distance a-rest b)))))))

(def max-edit-distance 3)
(def max-candidates 3)

(defn candidates [input available-cmds]
(->> available-cmds
(map (fn [c] {:dist (edit-distance c input)
:cmd c}))
(filter (fn [{:keys [dist]}] (<= dist max-edit-distance)))
(sort-by :dist)
(map :cmd)
(take max-candidates)))
Loading

0 comments on commit 9c8c595

Please # to comment.