Created
December 25, 2018 11:09
-
-
Save pepijndevos/64e8672e22282ed5868502be7cc9b898 to your computer and use it in GitHub Desktop.
Parse Whatsapp chats
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
import re | |
import sys | |
import glob | |
import pandas as pd | |
import matplotlib.pyplot as plt | |
regex = """(?P<datetime>\d{1,2}\/\d{1,2}\/\d{1,4}, \d{1,2}:\d{1,2}( (?i)[ap]m)*) - (?P<name>.*(?::\s*\w+)*|[\w\s]+?)(?:\s+(?P<action>joined|left|was removed|changed the (?:subject to "\w+"|group's icon))|:\s(?P<message>(?:.+|\n(?!\d{1,2}\/\d{1,2}\/\d{1,4}, \d{1,2}:\d{1,2}( (?i)[ap]m)*))+))""" | |
files = sys.argv[1:] | |
for fname in files: | |
with open(fname) as f: | |
text = f.read() | |
matches = re.findall(regex, text) | |
messages = [] | |
for match in matches: | |
messages.append(match[::2]) | |
df = pd.DataFrame(messages, columns=['datetime', 'name', 'message']) | |
df.datetime = pd.to_datetime(df.datetime, dayfirst=True) | |
df.set_index('datetime', inplace=True) | |
lengths = df.message.str.len() | |
monthly = lengths.groupby(pd.Grouper(freq='M')).count() #.sum() | |
#monthly = lengths.groupby(pd.Grouper(freq='D')).count().rolling('30d', min_periods=1).sum() | |
#plt.figure() | |
#ax = monthly.plot(title=fname[19:-4]) | |
ax = monthly.plot() | |
ax.legend([fname[19:-4] for fname in files]) | |
#plt.ylabel("bytes/month") | |
plt.ylabel("messages/month") | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment