Skip to content

Instantly share code, notes, and snippets.

@Kah0ona
Created March 29, 2020 15: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 Kah0ona/b266612a99dd37ac78a4b7c6381324db to your computer and use it in GitHub Desktop.
Save Kah0ona/b266612a99dd37ac78a4b7c6381324db to your computer and use it in GitHub Desktop.
Clojure script to count number of chats per person from WhatsApp export
(ns user
(:require [clojure.string :refer [lower-case includes? trim]]))
(defn parse-line
"Returns the name of the message, or nil if it can't be found"
[l]
(->> l
trim
(re-find #"\[.{19}\].(.[A-Za-z\ \-]+):")
second))
(defn chats-by-user
([in-file]
(chats-by-user in-file parse-line))
([in-file filter-fn]
(with-open [rdr (clojure.java.io/reader in-file)]
(let [lines (line-seq rdr)
histogram (reduce
(fn [acc l]
(if-let [n (filter-fn l)]
(update acc n (fnil inc 0))
;;else ignore
acc))
{}
lines)]
(->> histogram
(sort-by second)
reverse
(map (fn [[name amount]]
{:name name
:amount amount}))
clojure.pprint/print-table)))))
;;Usage
(chats-by-user "export.txt")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment