Skip to content

Instantly share code, notes, and snippets.

@alandipert
Created February 27, 2010 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alandipert/316709 to your computer and use it in GitHub Desktop.
Save alandipert/316709 to your computer and use it in GitHub Desktop.
(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