Skip to content
Browse files

Working edits1 function.

  • Loading branch information...
1 parent b76c15b commit 62ea0d18329eb55cd5ff211b31895b37bd6bd0b8 @smerrill committed Aug 9, 2012
Showing with 10 additions and 11 deletions.
  1. +10 −11 src/norvig_checker/core.clj
21 src/norvig_checker/core.clj
@@ -1,19 +1,18 @@
(ns norvig-checker.core)
(use '[clojure.string :only (lower-case)])
(defn words [x] (re-seq #"[a-z]+" (lower-case x)))
-(def NWORDS (frequencies (words (slurp "small.txt")))) ; @TODO: Move from slurp.
-;(words (slurp "big.txt"))
+;(def NWORDS (frequencies (words (slurp "big.txt")))) ; @TODO: Move from slurp.
(def alphabet "abcdefghijklmnopqrstuvwxyz")
(defn correct [words] (filter #(contains? NWORDS %) (list words)))
+(defn deletion [a word] (list (apply str (concat (take a word) (drop (inc a) word)))))
+(defn replaces [a word] (map #(apply str (concat (take a word) (list %) (drop (inc a) word))) alphabet))
+(defn transposition [a word] (list (apply str (if (< a (dec (count word))) (concat (take a word) (list (get word (inc a))) (list (get word a)) (drop (inc (inc a)) word))))))
+(defn inserts [a word] (map #(apply str (concat (take a word) (list %) (drop a word))) alphabet))
(defn edits1 [word]
- (let [len (count word)]
- (for [a (range len)]
- (concat
- (concat (take a word) (drop (inc a) word)) ; Deletions
- (if (< a (dec len)) (concat (take a word) (list (get word (inc a))) (list (get word a)) (drop (inc (inc a)) word)) nil) ; Transposes (yuck)
- (map #(concat (take a word) (list %) (drop (inc a) word)) alphabet) ; Replaces
- (map #(concat (take a word) (list %) (drop a word)) alphabet) ; Inserts
- )
- )))
+ (remove empty?
+ (flatten
+ (for [a (range (count word))]
+ (list (deletion a word) (inserts a word) (replaces a word) (transposition a word))))))
(edits1 "hi")

0 comments on commit 62ea0d1

Please sign in to comment.
Something went wrong with that request. Please try again.