Created
March 21, 2013 03:56
-
-
Save devn/5210579 to your computer and use it in GitHub Desktop.
Cheat at LetterPress because you're a classless jerk .
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
(ns anna.core | |
(:require [yokogiri.core :as y])) | |
(def client (y/make-client)) | |
(def base-uri "http://www.wordsmith.org") | |
(defn query-string [character-string] | |
(str base-uri | |
"/anagram/anagram.cgi?anagram=" | |
character-string | |
"&language=english&t=1000&d=&include=&exclude=&n=&m=&source=adv&a=n&l=y&q=n&k=1")) | |
(def xpath-for-words "//blockquote/text()") | |
(defn get-anagrams | |
"(get-anagrams \"abc\") | |
=> [\"Cab\" \"A\"]" | |
[s] | |
(into [] (map y/node-text | |
(y/xpath (y/get-page client (query-string s)) | |
xpath-for-words)))) | |
(defn contains-high-value-characters? [word s] | |
(.contains word s)) | |
(defn num-high-value-characters [word s] | |
(count (get (group-by true? (map #(.contains word %) (map str (seq s)))) | |
true))) | |
(defn get-rated-anagrams [ls high-value-characters] | |
(->> (map #(vector % (num-high-value-characters % high-value-characters)) | |
(get-anagrams ls)) | |
(sort-by second) | |
reverse)) | |
(defn sort-rated-anagrams [anagrams] | |
(->> anagrams | |
(sort-by second) | |
reverse)) | |
(defmacro anna [ls & high-value-chars] | |
`(->> (get-rated-anagrams (str '~ls) | |
(str '~@high-value-chars)) | |
sort-rated-anagrams | |
(take 10))) | |
;; (anna abcdefg gf) | |
;; => ["Beg" "Gab" ...] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment