typestar

word_freq.clj in Clojure

Counts words in a short harbor passage and prints the eight most frequent with their share of the text.

;; Word frequency report over one fixed passage.
(require '[clojure.string :as str])

(def passage
  "The harbor wakes early. Gulls circle the harbor wall while the
   ferry crews coil rope and the tide slides out. By noon the harbor
   is loud, and by dusk the harbor is quiet again.")

;; Splitting on runs of non-letters leaves an empty first piece when
;; the text opens with punctuation, so drop the blanks afterward.
(defn words [text]
  (->> (str/split (str/lower-case text) #"[^a-z']+")
       (remove str/blank?)))

;; frequencies builds a map of item -> count in a single pass.
(def counts (frequencies (words passage)))
(def total (count (words passage)))

;; sort-by can key on a vector: negate the count for descending
;; order, then fall back to the word itself to break ties.
(def ranked (sort-by (juxt (comp - val) key) counts))

(println (format "%-10s %5s %6s" "WORD" "COUNT" "SHARE"))
(println (apply str (repeat 23 "-")))
(doseq [[word n] (take 8 ranked)]
  (println (format "%-10s %5d %5.1f%%" word n (* 100.0 (/ n total)))))
(println (apply str (repeat 23 "-")))
(println (format "%-10s %5d" "WORDS" total))
(println (format "%-10s %5d" "DISTINCT" (count counts)))

How it works

  1. (str/split (str/lower-case text) #"[^a-z']+") cuts the lowercased passage on runs of non-letters, and (remove str/blank?) drops the empty leading piece.
  2. (frequencies (words passage)) builds the word-to-count map, and (sort-by (juxt (comp - val) key) counts) orders it by falling count, then alphabetically.
  3. (format "%-10s %5d %5.1f%%" word n (* 100.0 (/ n total))) prints each row in fixed columns beneath the header.

Keywords and builtins used here

The run, in numbers

Lines
29
Characters to type
1167
Tokens
171
Three-star pace
75 tpm

At the three-star pace of 75 tokens a minute, this run takes about 137 seconds.

Type this snippet

Step 1 of 3 in Encore, step 25 of 27 in Language basics.

← Previous Next →