-
Notifications
You must be signed in to change notification settings - Fork 42
/
reactions.py
136 lines (113 loc) · 4.24 KB
/
reactions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import json
import os
import matplotlib.pyplot as plt
import numpy as np
import re
import datetime as dt
from datetime import timedelta
import timestring
def reactions():
loc = input('Enter facebook archive extracted location: ')
if not os.path.isdir(loc):
print("The provided location doesn't seem to be right")
exit(1)
fname = loc+'/likes_and_reactions/posts_and_comments.json'
if not os.path.isfile(fname):
print("The file posts_and_commments.json is not present at the entered location.")
exit(1)
with open(fname) as f:
base_data = json.load(f)
data = base_data['reactions']
reactions = []
count = []
# Counting the occurence of each reaction
for ele in data:
reaction = ele['data'][0]['reaction']['reaction']
reactions.append(reaction)
count.append(reactions.count("LIKE"))
count.append(reactions.count("HAHA"))
count.append(reactions.count("WOW"))
count.append(reactions.count("LOVE"))
count.append(reactions.count("ANGER"))
count.append(reactions.count("SORRY"))
# Plotting the counts
x = np.array([0,1,2,3,4,5])
y = np.array(count)
x_ticks = ['LIKE', 'HAHA', 'WOW', 'LOVE', 'ANGER', 'SORRY']
plt.xticks(x,x_ticks)
plt.plot(x,y,linestyle='--',marker='o')
plt.ylabel('Frequency')
plt.show()
# Top10 friends whom you are most likely to react to
pattern1 = r"(?:likes ).+\b"
pattern2 = r"(?:to ).+\b"
names = []
for ele in data:
title = ele["title"]
first_names = re.findall(pattern1, title)
if len(first_names)>0:
if len(first_names[0].split())>1:
names.append(first_names[0].split()[1] + " " + first_names[0].split()[2])
else:
first_names = re.findall(pattern2, title)
if len(first_names)>0:
if len(first_names[0].split())>1:
names.append(first_names[0].split()[1] + " " + first_names[0].split()[2])
name_counter = {}
totalCnt=0
for name in names:
if name in name_counter:
name_counter[name]+=1
else:
name_counter[name]=1
totalCnt+=1
#print(name_counter)
popular_names = sorted(name_counter,key = name_counter.get, reverse = True)
top_10 = popular_names[:10]
top_10per = []
x_ticks = []
for friend in top_10:
per = (name_counter[friend] / totalCnt) * 100
friend = re.sub('\'s', '', friend)
top_10per.append(per)
x_ticks.append(friend)
x = np.array([0,1,2,3,4,5,6,7,8,9])
y = np.array(top_10per)
#x_ticks = ['LIKE', 'HAHA', 'WOW', 'LOVE', 'ANGER', 'SORRY']
plt.xticks(x,x_ticks,rotation = 45)
plt.plot(x,y,linestyle='--',marker='o')
plt.ylabel('Percentage of Reactions')
plt.tight_layout()
plt.show()
# Month Wise Distribution of Reactions
count_month = [0]*12
for ele in data:
timestamp = ele['timestamp']
month = dt.datetime.fromtimestamp(timestamp).month
count_month[month-1]+=1
plt.plot(count_month,linestyle="--", marker="^", color="g")
plt.ylabel("Frequency")
plt.xlabel("Month Number")
plt.show()
# Line plot for each reaction
rxnList = ['LIKE', 'HAHA', 'WOW', 'LOVE', 'ANGER', 'SORRY']
for rxn in rxnList:
dataTemp = [item for item in data if item["data"][0]["reaction"]["reaction"]==rxn]
dates = [timestring.Date(i["timestamp"]).date for i in dataTemp]
dates.reverse()
firstdate = dates[0]
maxdays = int((dates[-1] - firstdate).total_seconds() / 86400) + 1
reactionCount = [0]*int(maxdays)
for i in range(len(dates)):
days_diff = (dates[i] - firstdate).total_seconds() / 86400
reactionCount[int(days_diff)] += 1
xaxis = [ dt.datetime.now() - timedelta(days=maxdays-i) for i in range(maxdays) ]
cumulative_reactions = np.cumsum(reactionCount).tolist()
plt.plot(xaxis,cumulative_reactions,linewidth=3.0, label=rxn)
plt.legend(loc='upper left')
plt.title("Reactions on posts", fontsize=16, fontweight='bold')
plt.xlabel("Time")
plt.ylabel("Cumulative Sum of Reactions")
plt.show()
if __name__ == '__main__':
reactions()