Clojure 编程/示例/Norvig 拼写纠正程序
展现
< Clojure 编程 | 示例
Peter Norvig 用 Python 编写了一个拼写纠正程序的优秀示例。你可以访问 如何编写拼写纠正程序 查看更详细的说明。
这段代码由该语言的创建者 Rich Hickey 翻译为 Clojure,它体现了 Clojure 类似于 Python 的简洁性。
(defn words [text] (re-seq #"[a-z]+" (.toLowerCase text)))
(defn train [features]
(reduce (fn [model f] (assoc model f (inc (get model f 0)))) {} features))
(def *nwords* (train (words (slurp "big.txt"))))
(defn edits1 [word]
(let [alphabet "abcdefghijklmnopqrstuvwxyz", n (count word)]
(distinct (concat
(for [i (range n)] (str (subs word 0 i) (subs word (inc i))))
(for [i (range (dec n))]
(str (subs word 0 i) (nth word (inc i)) (nth word i) (subs word (+ 2 i))))
(for [i (range n) c alphabet] (str (subs word 0 i) c (subs word (inc i))))
(for [i (range (inc n)) c alphabet] (str (subs word 0 i) c (subs word i)))))))
(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]
(let [candidates (or (known [word] nwords) (known (edits1 word) nwords)
(known-edits2 word nwords) [word])]
(apply max-key #(get nwords % 1) candidates)))
使用方法
(correct "misstake" *nwords*)
(correct "speling" *nwords*)
注意:使用 big.txt 进行培训时,可能需要使用比默认值更大的堆来启动 jvm。可以通过使用“-server”选项启动 Java 或明确设置最大堆大小来增加堆大小:“java -Xmx128m ...”。