Created
February 27, 2010 14:01
-
-
Save alandipert/316709 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 scorewords | |
(:use [clojure.test])) | |
(defn score-sub [words] | |
(map (fn [[word n]] [word (if (> n 3) | |
0 | |
(/ 1 n))]) (partition 2 (interleave words (iterate inc 2))))) | |
(defn score-words | |
([term words base] | |
(let [[front back] (split-with (partial not= term) words)] | |
(concat (reverse (score-sub (reverse front))) | |
[[term base]] | |
(score-sub (rest back))))) | |
([term words] | |
(score-words term words 1))) | |
(deftest test-score-words | |
(let [words ["bobby" "fire" "truck" "city" "department" "state" "colorado"] | |
multiwords ["bobby" "fire" "jon" "city" "truck" "jon" "department" "state" "colorado"]] | |
(is (= [["bobby" 1/3] ["fire" 1/2] ["truck" 1] ["city" 1/2] ["department" 1/3] ["state" 0] ["colorado" 0]] | |
(score-words "truck" words))) | |
(is (= [["bobby" 10/3] ["fire" 5] ["truck" 10] ["city" 5] ["department" 10/3] ["state" 0] ["colorado" 0]] | |
(score-words "truck" words 10))) | |
(is (= [["bobby 1/3"] ["fire" 1/2] ["jon" 1] ["city" 1/2] ["truck" 1/2] ["jon" 1] ["department" 1/2] ["state" 1/3] ["colorado" 0]] | |
(score-words "jon" multiwords)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment