Skip to content

Instantly share code, notes, and snippets.

@msszczep
Created March 20, 2015 19:09
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 msszczep/4b5409d5a256a0717585 to your computer and use it in GitHub Desktop.
Save msszczep/4b5409d5a256a0717585 to your computer and use it in GitHub Desktop.
Clojure exercise: Sentiment Scoring for Words
(defn sentiment-scorer [& maps]
(letfn [(sentiment-scorer-intermediate [m]
(let [words (-> (:c m)
(clojure.string/lower-case)
(clojure.string/replace #"[\.\!\@]" "")
(clojure.string/split #"\s+"))
rs (repeat (count words) [(:r m)])]
(apply hash-map (interleave words rs))))]
(let [intermediate-map (->> (apply map sentiment-scorer-intermediate maps)
(apply merge-with concat))]
(zipmap (keys intermediate-map)
(map #(/ (apply + %) (count %)) (vals intermediate-map))))))
(sentiment-scorer [{:r 5 :c "This is awesome!"}
{:r 1 :c "This sucks...."}])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment