Created
February 23, 2013 20:10
-
-
Save fbmnds/5021142 to your computer and use it in GitHub Desktop.
Levenshtein Distance - 4clojure #101
This file contains hidden or 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
| (fn f [a b] | |
| (letfn [(clear-string [a] | |
| (cond (string? a) a | |
| (vector? a) (clojure.string/join (map str a)) | |
| :else (clojure.string/join | |
| (filter #(not (= % \:)) | |
| (clojure.string/join (map str a)))))) | |
| (make-ls-d [] | |
| (with-local-vars | |
| [ls-d (memoize | |
| (fn f [a b] | |
| (cond (empty? a) (count b) | |
| (empty? b) (count a) | |
| :else (let [cost (if (= (last a) (last b)) 0 1) | |
| a-1 (clojure.string/join (butlast a)) | |
| b-1 (clojure.string/join (butlast b))] | |
| (min | |
| (inc (ls-d a-1 b)) | |
| (inc (ls-d a b-1)) | |
| (+ (ls-d a-1 b-1) cost))))))] | |
| (.bindRoot ls-d @ls-d) | |
| @ls-d))] | |
| (let [ls-d (make-ls-d)] | |
| (ls-d (clear-string a) (clear-string b))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment