panchadip commited on
Commit
fc66e40
·
verified ·
1 Parent(s): a389b8d

Create helper.py

Browse files
Files changed (1) hide show
  1. helper.py +139 -0
helper.py ADDED
@@ -0,0 +1,139 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from urlextract import URLExtract
2
+ from wordcloud import WordCloud
3
+ import pandas as pd
4
+ from collections import Counter
5
+ import emoji
6
+
7
+ extract = URLExtract()
8
+
9
+ def fetch_stats(selected_user,df):
10
+
11
+ if selected_user != 'Overall':
12
+ df = df[df['user'] == selected_user]
13
+
14
+ # fetch the number of messages
15
+ num_messages = df.shape[0]
16
+
17
+ # fetch the total number of words
18
+ words = []
19
+ for message in df['message']:
20
+ words.extend(message.split())
21
+
22
+ # fetch number of media messages
23
+ num_media_messages = df[df['message'] == '<Media omitted>\n'].shape[0]
24
+
25
+ # fetch number of links shared
26
+ links = []
27
+ for message in df['message']:
28
+ links.extend(extract.find_urls(message))
29
+
30
+ return num_messages,len(words),num_media_messages,len(links)
31
+
32
+ def most_busy_users(df):
33
+ x = df['user'].value_counts().head()
34
+ df = round((df['user'].value_counts() / df.shape[0]) * 100, 2).reset_index().rename(
35
+ columns={'index': 'name', 'user': 'percent'})
36
+ return x,df
37
+
38
+ def create_wordcloud(selected_user,df):
39
+
40
+ f = open('stop_hinglish.txt', 'r')
41
+ stop_words = f.read()
42
+
43
+ if selected_user != 'Overall':
44
+ df = df[df['user'] == selected_user]
45
+
46
+ temp = df[df['user'] != 'group_notification']
47
+ temp = temp[temp['message'] != '<Media omitted>\n']
48
+
49
+ def remove_stop_words(message):
50
+ y = []
51
+ for word in message.lower().split():
52
+ if word not in stop_words:
53
+ y.append(word)
54
+ return " ".join(y)
55
+
56
+ wc = WordCloud(width=500,height=500,min_font_size=10,background_color='white')
57
+ temp['message'] = temp['message'].apply(remove_stop_words)
58
+ df_wc = wc.generate(temp['message'].str.cat(sep=" "))
59
+ return df_wc
60
+
61
+ def most_common_words(selected_user,df):
62
+
63
+ f = open('stop_hinglish.txt','r')
64
+ stop_words = f.read()
65
+
66
+ if selected_user != 'Overall':
67
+ df = df[df['user'] == selected_user]
68
+
69
+ temp = df[df['user'] != 'group_notification']
70
+ temp = temp[temp['message'] != '<Media omitted>\n']
71
+
72
+ words = []
73
+
74
+ for message in temp['message']:
75
+ for word in message.lower().split():
76
+ if word not in stop_words:
77
+ words.append(word)
78
+
79
+ most_common_df = pd.DataFrame(Counter(words).most_common(20))
80
+ return most_common_df
81
+
82
+ def emoji_helper(selected_user,df):
83
+ if selected_user != 'Overall':
84
+ df = df[df['user'] == selected_user]
85
+
86
+ emojis = []
87
+ for message in df['message']:
88
+ emojis.extend([c for c in message if c in emoji.UNICODE_EMOJI['en']])
89
+
90
+ emoji_df = pd.DataFrame(Counter(emojis).most_common(len(Counter(emojis))))
91
+
92
+ return emoji_df
93
+
94
+ def monthly_timeline(selected_user,df):
95
+
96
+ if selected_user != 'Overall':
97
+ df = df[df['user'] == selected_user]
98
+
99
+ timeline = df.groupby(['year', 'month_num', 'month']).count()['message'].reset_index()
100
+
101
+ time = []
102
+ for i in range(timeline.shape[0]):
103
+ time.append(timeline['month'][i] + "-" + str(timeline['year'][i]))
104
+
105
+ timeline['time'] = time
106
+
107
+ return timeline
108
+
109
+ def daily_timeline(selected_user,df):
110
+
111
+ if selected_user != 'Overall':
112
+ df = df[df['user'] == selected_user]
113
+
114
+ daily_timeline = df.groupby('only_date').count()['message'].reset_index()
115
+
116
+ return daily_timeline
117
+
118
+ def week_activity_map(selected_user,df):
119
+
120
+ if selected_user != 'Overall':
121
+ df = df[df['user'] == selected_user]
122
+
123
+ return df['day_name'].value_counts()
124
+
125
+ def month_activity_map(selected_user,df):
126
+
127
+ if selected_user != 'Overall':
128
+ df = df[df['user'] == selected_user]
129
+
130
+ return df['month'].value_counts()
131
+
132
+ def activity_heatmap(selected_user,df):
133
+
134
+ if selected_user != 'Overall':
135
+ df = df[df['user'] == selected_user]
136
+
137
+ user_heatmap = df.pivot_table(index='day_name', columns='period', values='message', aggfunc='count').fillna(0)
138
+
139
+ return user_heatmap