Created
February 4, 2022 01:52
-
-
Save HaiderZaidiDev/4aff97b6c97815fbd3eba114778309c4 to your computer and use it in GitHub Desktop.
Instagram Group Chat Frequency Calculator
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
import matplotlib.pyplot as plt | |
import json | |
def message_count(*args): | |
""" | |
Counts the frequency of messages sent by users in an Instagram group chat, | |
Parameters | |
---------- | |
args: str | |
Filenames for instagram json dumps. | |
""" | |
senders = {} | |
for filename in args: | |
# Parsing data dumps. | |
message_content = open(filename) | |
message_data = json.load(message_content) | |
# Calculating frequency of messages sent by each user. | |
for message in message_data['messages']: | |
sender = message['sender_name'] | |
if sender in senders: | |
# If the user is already in the dictionary. | |
senders[sender] += 1 | |
else: | |
# If they're not in the dictionary. | |
senders[sender] = 1 | |
# Compiling data into lists, for matplotlib. | |
names = [key for key, value in senders.items()] | |
values = [value for key, value in senders.items()] | |
# Creating Graph | |
plt.bar(names, values) | |
plt.xlabel('Names') | |
plt.ylabel('# of Messages Sent') | |
plt.xticks(rotation=90) # Rotating names by 90 degrees. | |
plt.show() | |
plt.savefig('academics.png') | |
message_count('message_1.json', 'message_2.json') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment