-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.py
207 lines (156 loc) · 6.1 KB
/
helper.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import re
import matplotlib.pyplot as plt
from wordcloud import WordCloud, STOPWORDS
import pandas as pd
from collections import Counter
import emoji
import seaborn as sb
def link_extractor(message):
url_pattern = re.compile(r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+')
return url_pattern.findall(message)
def fetch_stats(selected_user, df):
if selected_user == 'Overall':
msg = df.shape[0]
words = df['Message'].apply(lambda x: len(x.split())).sum()
media = df[df['Message'].str.strip().str.lower().str.contains(
'<media omitted>', na=False)].shape[0]
links = df['Message'].apply(link_extractor).apply(len).sum()
else:
user = df[df['Sender'] == selected_user]
msg = user.shape[0]
words = user['Message'].apply(lambda x: len(x.split())).sum()
media = user[user['Message'].str.strip().str.lower(
).str.contains('<media omitted>', na=False)].shape[0]
links = user['Message'].apply(link_extractor).apply(len).sum()
return msg, words, media, links
def active_users(df):
count = df['Sender'].value_counts()
if 'System' in count.index:
count = count.drop('System')
prcnt = round((count / count.sum()) * 100, 2)
# contribution = pd.DataFrame({
# 'User': count.index,
# 'Messages': count.values,
# 'Contribution(%)': prcnt.values
# })
if len(count) > 10:
count = count.head(10)
fig, ax = plt.subplots()
ax.pie(count.values, labels=count.index, autopct='%1.1f%%', startangle=90)
return fig
def Cloud(df, selected_user):
with open('stop_hinglish.txt', 'r') as f:
stop_hinglish = set(f.read().splitlines())
stopwords = STOPWORDS.union(stop_hinglish)
if selected_user == "Overall":
user = df[df['Sender'] != 'System']
else:
user = df[df['Sender'] == selected_user]
text = ' '.join(user['Message']).replace(
"<Media omitted>", "").replace("This message was deleted", "").replace("You deleted this message", "").replace("Missed voice call","")
wc = WordCloud(background_color='white', width=1600, height=800, stopwords=stopwords).generate(text)
fig, ax = plt.subplots(figsize=(10, 10))
ax.imshow(wc, interpolation='bilinear')
ax.axis('off')
return fig
def Common(df, selected_user):
with open('stop_hinglish.txt', 'r') as f:
stop_hinglish = set(f.read().splitlines())
if selected_user == "Overall":
user = df[df['Sender'] != 'System']
else:
user = df[df['Sender'] == selected_user]
text = ' '.join(user['Message']).replace("<Media omitted>", "").replace(
"This message was deleted", "").replace("You deleted this message", "").replace("Missed voice call","").lower()
words = text.split()
words = [word for word in words if word not in stop_hinglish]
counts = Counter(words).most_common(20)
words, word_counts = zip(*counts)
fig, ax = plt.subplots(figsize=(10, 8))
ax.barh(words, word_counts, color='skyblue')
ax.set_xlabel('Count')
ax.invert_yaxis()
return fig
def emojis(df, selected_user):
if selected_user == "Overall":
user = df[df['Sender'] != 'System']
else:
user = df[df['Sender'] == selected_user]
emoji_list = []
for message in user['Message']:
emoji_list.extend([emoji.emojize(e) for e in message if emoji.emoji_count(e)])
counts = Counter(emoji_list).most_common(10)
counts_df = pd.DataFrame(counts, columns=['Emoji', 'Counts'])
counts_df = counts_df.astype({'Emoji': str, 'Counts': int})
fig, ax = plt.subplots()
ax.pie(counts_df['Counts'], labels=counts_df['Emoji'], startangle=90, autopct='%1.1f%%')
ax.set_title('Emoji Distribution')
return fig, counts_df
def monthly_timeline(df,selected_user):
if selected_user == "Overall":
user = df[df['Sender'] != 'System']
else:
user = df[df['Sender'] == selected_user]
timeline = user.groupby(['Year','Month_Num','Month']).count()['Message'].reset_index()
time = []
for i in range(timeline.shape[0]):
time.append(timeline['Month'][i]+"-"+str(timeline['Year'][i]))
timeline["Time"] = time
fig,ax = plt.subplots()
ax.plot(timeline['Time'],timeline['Message'])
ax.set_xticklabels(timeline['Time'], rotation=45, ha='right')
return fig
def daily_timeline(df,selected_user):
if selected_user == "Overall":
user = df[df['Sender'] != 'System']
else:
user = df[df['Sender'] == selected_user]
timeline = user.groupby('Date').count()['Message'].reset_index()
time = []
fig,ax = plt.subplots(figsize=(15,10))
ax.plot(timeline['Date'],timeline['Message'])
ax.set_xticklabels(timeline['Date'], rotation=45, ha='right')
return fig
def week_activity(df,selected_user):
if selected_user == "Overall":
user = df[df['Sender'] != 'System']
else:
user = df[df['Sender'] == selected_user]
bzday= user['Day_name'].value_counts()
fig,ax =plt.subplots()
ax.bar(bzday.index,bzday.values)
ax.set_xticklabels(bzday.index, rotation=45, ha='right')
return fig
def month_activity(df,selected_user):
if selected_user == "Overall":
user = df[df['Sender'] != 'System']
else:
user = df[df['Sender'] == selected_user]
bzmonth= user['Month'].value_counts()
fig,ax =plt.subplots()
ax.bar(bzmonth.index,bzmonth.values)
ax.set_xticklabels(bzmonth.index, rotation=45, ha='right')
return fig
def activity_heatmap(selected_user, df):
if selected_user == "Overall":
user = df[df['Sender'] != 'System']
else:
user = df[df['Sender'] == selected_user]
user_heatmap = user.pivot_table(index='Day_name', columns='period', values='Message', aggfunc='count').fillna(0)
cmap = plt.cm.Blues
fig, ax = plt.subplots(figsize=(10,5))
ax = sb.heatmap(user_heatmap, cmap=cmap, linewidths=.5, annot=True, fmt="g", square=True,cbar=False)
ax.invert_yaxis()
return fig
def search_messages(df, selected_user, word):
if selected_user == "Overall":
user = df[df['Sender'] != 'System']
else:
user = df[df['Sender'] == selected_user]
temp = user[user['Message'].str.contains(word, case=False, na=False)].reset_index()
temp['Full Date'] = (temp['Date'].astype(str) + '/' +
temp['Month_Num'].astype(str) + '/' +
temp['Year'].astype(str) + ', ' +
temp['Hour'].astype(str) + ':' +
temp['Minutes'].astype(str))
return temp[['Sender', 'Message', 'Full Date']]