Last active
July 29, 2022 16:09
-
-
Save coyotesqrl/c9e57fee6cf61cafd7128648e83123f7 to your computer and use it in GitHub Desktop.
Hacked together word frequency counter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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