Clojure Programming/Examples/Norvig Spelling Corrector

From Wikibooks, open books for an open world
< Clojure Programming‎ | Examples
Jump to: navigation, search

Peter Norvig wrote an excellent example of a spelling corrector in Python. You can see How to Write a Spelling Corrector for a more detailed explanation.

This code, translated into Clojure by language creator Rich Hickey, demonstrates the Python-like brevity of Clojure:


(defn train [features]


(defn edits1 [word]
    (distinct (concat
      (for [i (range (dec n))]

(defn known [words nwords] (not-empty (set (for [w words :when (nwords w)]  w))))

(defn known-edits2 [word nwords]
  (not-empty (set (for [e1 (edits1 word) e2 (edits1 e1) :when (nwords e2)]  e2))))

(defn correct [word nwords]
                       (known-edits2 word nwords) [word])]

To use:

(correct "misstake" *nwords*)
(correct "speling" *nwords*)

Note: training with big.txt may require the jvm to be started with a heap bigger than its default. You can increase the heap size either by starting java with the "-server" option or setting the maximum heap size explicitly: "java -Xmx128m ..."