Skip to content
Permalink
796c20d445
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
56 lines (49 sloc) 1.58 KB
(ns ulysses.components.word-cloud
(:require [re-frame.core :refer [subscribe]]
[ulysses.utils :refer [map-subels]]
[ulysses.lib.packer :refer [fit-words-memoized]]
[clojure.string :as string]))
;; ----------------------------------------------------------------------------
;; helpers
;; ----------------------------------------------------------------------------
(defn normalize [keywords]
(let [scores (map :score keywords)
s-min (or (apply min scores) 0)
s-max (or (apply max scores) 100)
denom (- s-max s-min)]
(map
(fn [kw]
(assoc kw :weight
(/ (- (:score kw) s-min)
denom)))
keywords)))
(defn clamp [keywords]
(map
(fn [{:keys [weight] :as kw}]
(if-not (< weight 0.15) kw
(assoc kw :weight 0.15)))
keywords))
(defn normalize-sort-scale [keywords]
(->> keywords
(take 200)
(normalize)
(sort-by :weight)
(reverse)
(clamp)))
; TODO different width calculation for profile word clouds
(defn width-from-window [{width :width}]
(-> width
(* 0.98)
(* (- 1 0.2 0.01))
(* 0.495)
(- (* 15 2))))
;; ----------------------------------------------------------------------------
;; main
;; ----------------------------------------------------------------------------
(defn word-cloud [keywords]
(let [window-size (subscribe [:window-size])]
(fn [keywords]
(let [w (width-from-window @window-size)]
[:div.word-cloud
[fit-words-memoized w 500
(normalize-sort-scale keywords)]]))))