Skip to content

Instantly share code, notes, and snippets.

@coyotesqrl
Last active July 29, 2022 16: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 coyotesqrl/c9e57fee6cf61cafd7128648e83123f7 to your computer and use it in GitHub Desktop.
Save coyotesqrl/c9e57fee6cf61cafd7128648e83123f7 to your computer and use it in GitHub Desktop.
Hacked together word frequency counter
(ns coyotesqrl.word-freq
(:require [clojure.java.io :as io]
[clojure.string :as str]))
(defn line-freq [l]
(->> (str/split l #"\W+")
(map str/lower-case)
(frequencies)))
(defn word-freqs [f]
(sort-by val >
(with-open [rdr (io/reader f)]
(reduce (fn [a v]
(merge-with + a (line-freq v)))
{}
(line-seq rdr)))))
(comment
(let [result (word-freqs "kjvbible.txt")]
{"Distinct words" (count result)
"Most frequent word" (first result)}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment