Spaces:
Build error
Build error
Delete helper.py
Browse files
helper.py
DELETED
@@ -1,132 +0,0 @@
|
|
1 |
-
import matplotlib.pyplot as plt
|
2 |
-
from urlextract import URLExtract
|
3 |
-
from collections import Counter
|
4 |
-
from wordcloud import WordCloud, STOPWORDS ,ImageColorGenerator
|
5 |
-
import pandas as pd
|
6 |
-
import matplotlib.pylab as plt
|
7 |
-
import emoji
|
8 |
-
|
9 |
-
extract=URLExtract()
|
10 |
-
def fetch_stats(selected_user,df):
|
11 |
-
|
12 |
-
if selected_user!= "Group analysis":
|
13 |
-
df=df[df['users']==selected_user]
|
14 |
-
num_messages = df.shape[0]
|
15 |
-
words = []
|
16 |
-
for message in df['message']:
|
17 |
-
words.extend(message.split())
|
18 |
-
|
19 |
-
|
20 |
-
links=[]
|
21 |
-
for message in df['message']:
|
22 |
-
links.extend(extract.find_urls(message))
|
23 |
-
|
24 |
-
return num_messages, len(words),len(links)
|
25 |
-
|
26 |
-
def most_busy_users(df):
|
27 |
-
x = df['users'].value_counts().head()
|
28 |
-
df=round((df['users'].value_counts() / df.shape[0]) * 100, 2).reset_index().rename(
|
29 |
-
columns={'index': 'name', 'user': 'percent'})
|
30 |
-
return x,df
|
31 |
-
|
32 |
-
def most_common_words(selected_user,df):
|
33 |
-
f = open('stop_hinglish.txt', 'r')
|
34 |
-
stop_words = f.read()
|
35 |
-
|
36 |
-
if selected_user != "Group analysis":
|
37 |
-
df = df[df['users'] == selected_user]
|
38 |
-
temp = df[df['users'] != 'group_notification']
|
39 |
-
temp = temp[temp['message'] != '<Media omitted>\n']
|
40 |
-
|
41 |
-
words = []
|
42 |
-
|
43 |
-
for message in temp['message']:
|
44 |
-
for word in message.lower().split():
|
45 |
-
if word not in stop_words:
|
46 |
-
words.append(word)
|
47 |
-
most_common_df=pd.DataFrame(Counter(words).most_common(30))
|
48 |
-
return most_common_df
|
49 |
-
|
50 |
-
def word_cloud(selected_user,df):
|
51 |
-
if selected_user != "Group analysis":
|
52 |
-
df = df[df['users'] == selected_user]
|
53 |
-
|
54 |
-
stopwords = set('STOPWORDS')
|
55 |
-
|
56 |
-
# wordcloud
|
57 |
-
wordcloud = WordCloud(stopwords=stopwords, background_color="Black").generate(''.join(df['message']))
|
58 |
-
plt.figure(figsize=(10, 8), facecolor='k')
|
59 |
-
plt.imshow(wordcloud, interpolation='bilinear')
|
60 |
-
plt.show()
|
61 |
-
|
62 |
-
return wordcloud
|
63 |
-
|
64 |
-
def emoji_helper(selected_user,df):
|
65 |
-
if selected_user != "Group analysis":
|
66 |
-
df = df[df['users'] == selected_user]
|
67 |
-
emojis = []
|
68 |
-
for message in df['message']:
|
69 |
-
emojis.extend([c for c in message if c in emoji.EMOJI_DATA.keys()])
|
70 |
-
emoji_df=pd.DataFrame(Counter(emojis).most_common(len(Counter(emojis))))
|
71 |
-
|
72 |
-
return emoji_df
|
73 |
-
|
74 |
-
def monthly_timeline(selected_user,df):
|
75 |
-
if selected_user != "Group analysis":
|
76 |
-
df = df[df['users'] == selected_user]
|
77 |
-
|
78 |
-
timeline = df.groupby(['year', 'Month_name', 'Month']).count()['message'].reset_index()
|
79 |
-
time = []
|
80 |
-
for i in range(timeline.shape[0]):
|
81 |
-
time.append(timeline['Month_name'][i] + "-" + str(timeline['year'][i]))
|
82 |
-
timeline['time'] = time
|
83 |
-
|
84 |
-
return timeline
|
85 |
-
def Daily_timeline(selected_user,df):
|
86 |
-
if selected_user != "Group analysis":
|
87 |
-
df = df[df['users'] == selected_user]
|
88 |
-
|
89 |
-
daily_timeline = df.groupby('Date').count()['message'].reset_index()
|
90 |
-
|
91 |
-
return daily_timeline
|
92 |
-
|
93 |
-
def week_activity_map(selected_user,df):
|
94 |
-
if selected_user != "Group analysis":
|
95 |
-
df = df[df['users'] == selected_user]
|
96 |
-
return df['Day_name'].value_counts()
|
97 |
-
|
98 |
-
def month_activity_map(selected_user,df):
|
99 |
-
if selected_user != "Group analysis":
|
100 |
-
df = df[df['users'] == selected_user]
|
101 |
-
return df['Month_name'].value_counts()
|
102 |
-
|
103 |
-
def activity_heatmap(selected_user,df):
|
104 |
-
if selected_user != "Group analysis":
|
105 |
-
df = df[df['users'] == selected_user]
|
106 |
-
|
107 |
-
Activity_heatmap= df.pivot_table(index='Day_name', columns='period', values='message', aggfunc='count').fillna(0)
|
108 |
-
return Activity_heatmap
|
109 |
-
|
110 |
-
def pos_words(selected_user,df):
|
111 |
-
if selected_user != "Group analysis":
|
112 |
-
df = df[df['users'] == selected_user]
|
113 |
-
|
114 |
-
pos_word = df[df['vader_Analysis'] == 'Positive']
|
115 |
-
pos_word = pos_word.pop('message')
|
116 |
-
return pos_word
|
117 |
-
|
118 |
-
def neg_words(selected_user,df):
|
119 |
-
if selected_user != "Group analysis":
|
120 |
-
df = df[df['users'] == selected_user]
|
121 |
-
|
122 |
-
neg_word = df[df['Analysis'] == 'Negative']
|
123 |
-
neg_word = neg_word.pop('message')
|
124 |
-
return neg_word
|
125 |
-
|
126 |
-
def neu_words(selected_user,df):
|
127 |
-
if selected_user != "Group analysis":
|
128 |
-
df = df[df['users'] == selected_user]
|
129 |
-
|
130 |
-
neu_word = df[df['vader_Analysis'] == 'Neutral']
|
131 |
-
neu_word = neu_word.pop('message')
|
132 |
-
return neu_word
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|