Created
January 7, 2014 09:25
-
-
Save JCallicoat/8296891 to your computer and use it in GitHub Desktop.
Translation of the first part of Norvig's solution to xkcd 1313 to clojure
This file contains 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
(use '[clojure.string :only [split, join]]) | |
(use '[clojure.set :only [difference]]) | |
(def winners | |
(let [w "washington adams jefferson jefferson madison madison monroe | |
monroe adams jackson jackson vanburen harrison polk taylor pierce buchanan | |
lincoln lincoln grant grant hayes garfield cleveland harrison cleveland mckinley | |
mckinley roosevelt taft wilson wilson harding coolidge hoover roosevelt | |
roosevelt roosevelt roosevelt truman eisenhower eisenhower kennedy johnson nixon | |
nixon carter reagan reagan bush clinton clinton bush bush obama obama"] | |
(set (remove empty? (split w #"\s"))))) | |
(def losers | |
(let [l "clinton jefferson adams pinckney pinckney clinton king adams | |
jackson adams clay vanburen vanburen clay cass scott fremont breckinridge | |
mcclellan seymour greeley tilden hancock blaine cleveland harrison bryan bryan | |
parker bryan roosevelt hughes cox davis smith hoover landon wilkie dewey dewey | |
stevenson stevenson nixon goldwater humphrey mcgovern ford carter mondale | |
dukakis bush dole gore kerry mccain romney"] | |
(difference (set (remove empty? (split l #"\s"))) winners))) | |
(defn verify | |
"Verify that regex matches all winners but no losers." | |
[regex winners losers] | |
(let [missed-winners (remove empty? (for [name winners] (when-not (re-find regex name) name))) | |
missed-losers (remove empty? (for [name losers] (when (re-find regex name) name)))] | |
(when-not (empty? missed-winners) (prn (str "Error: Should match but did not: " (join ", " missed-winners)))) | |
(when-not (empty? missed-losers) (prn (str "Error: Should not match but did: " (join ", " missed-losers)))) | |
(and (empty? missed-winners) (empty? missed-losers)))) | |
(verify #"bu|[rn]t|[coy]e|[mtg]a|j|iso|n[hl]|[ae]d|lev|sh|[lnd]i|[po]o|ls" winners losers) | |
;;;=> "Error: Should not match but did: fremont" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi -
Cool. I found this searching for xkcd and clojure. I have a clojure version too, though it uses simulated annealing instead of Peter's algorithm. You may be one of the few people in the universe who might care. Here's the code and
a blog post. It "wins" by 2 strokes, at a ridiculous cost in cpu.
A while back, I tried "improving" PN's spell-checking using some primitive classification. That was even less successful, though it was slightly better at correcting misspellings of "ein" in German texts.