Skip to content

Instantly share code, notes, and snippets.

@iyahoo
Last active September 5, 2018 08:29
Show Gist options
  • Select an option

  • Save iyahoo/9371d94905adfe91d811c00a266ed2ee to your computer and use it in GitHub Desktop.

Select an option

Save iyahoo/9371d94905adfe91d811c00a266ed2ee to your computer and use it in GitHub Desktop.
levenshetein test
(ns check.core
(:require [clj-fuzzy.metrics :refer [levenshtein]]))
(defn pair-levenshtein-key [nk key]
{key (levenshtein nk (name key))})
(defn check-levenshtein-value-with [k dic]
{:pre [(keyword? k)
(set? dic)
(every? #(keyword? %) dic)]}
(let [nk (name k)]
(reduce #(conj %1 (pair-levenshtein-key nk %2))
{}
dic)))
(defn check-keyword [k correct-words]
(when-not (contains? correct-words k)
(let [most-sim-word (->> (check-levenshtein-value-with k correct-words)
(apply min-key val)
(key))]
(assert nil
(str "Don't exist keyword " k ", "
"do you mean " (keyword most-sim-word) "?")))))
(defn check-keywords-of-map [m correct-words]
(mapcat #(check-keyword % correct-words) (keys m)))
;; check.core> (def x {:foo 123 :baa 234})
;; check.core> (def ks #{:foo :bar})
;; check.core> (check-keywords-of-map x ks)
;; AssertionError Assert failed: Don't exist keyword :baa, do you mean :bar?
;; check.core> (check-keywords-of-map x #{:fo :ba})
;; AssertionError Assert failed: Don't exist keyword :foo, do you mean :fo?
;; check.core> (check-keywords-of-map x #{:foo :hoge})
;; AssertionError Assert failed: Don't exist keyword :baa, do you mean :foo?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment