Created
March 29, 2020 15:09
-
-
Save Kah0ona/b266612a99dd37ac78a4b7c6381324db to your computer and use it in GitHub Desktop.
Clojure script to count number of chats per person from WhatsApp export
This file contains hidden or 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 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