File size: 12,863 Bytes
d5ba1b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
from urlextract import URLExtract
from wordcloud import WordCloud
import pandas as pd
from collections import Counter
import emoji
import plotly.express as px
import matplotlib.pyplot as plt
import seaborn as sns

extract = URLExtract()

def fetch_stats(selected_user, df):
    if selected_user != 'Overall':
        df = df[df['user'] == selected_user]
    num_messages = df.shape[0]
    words = sum(len(msg.split()) for msg in df['message'])
    num_media_messages = df[df['unfiltered_messages'] == '<media omitted>\n'].shape[0]
    links = sum(len(extract.find_urls(msg)) for msg in df['unfiltered_messages'])
    return num_messages, words, num_media_messages, links

def most_busy_users(df):
    x = df['user'].value_counts().head()
    df = round((df['user'].value_counts() / df.shape[0]) * 100, 2).reset_index().rename(
        columns={'index': 'percentage', 'user': 'Name'})
    return x, df

def create_wordcloud(selected_user, df):
    if selected_user != 'Overall':
        df = df[df['user'] == selected_user]
    temp = df[df['user'] != 'group_notification']
    temp = temp[~temp['message'].str.lower().str.contains('<media omitted>')]
    wc = WordCloud(width=500, height=500, min_font_size=10, background_color='white')
    df_wc = wc.generate(temp['message'].str.cat(sep=" "))
    return df_wc

def most_common_words(selected_user, df):
    if selected_user != 'Overall':
        df = df[df['user'] == selected_user]
    temp = df[df['user'] != 'group_notification']
    temp = temp[~temp['message'].str.lower().str.contains('<media omitted>')]
    words = [word for msg in temp['message'] for word in msg.lower().split()]
    return pd.DataFrame(Counter(words).most_common(20))

def emoji_helper(selected_user, df):
    if selected_user != 'Overall':
        df = df[df['user'] == selected_user]
    emojis = [c for msg in df['unfiltered_messages'] for c in msg if c in emoji.EMOJI_DATA]
    return pd.DataFrame(Counter(emojis).most_common(len(Counter(emojis))))

def monthly_timeline(selected_user, df):
    if selected_user != 'Overall':
        df = df[df['user'] == selected_user]
    timeline = df.groupby(['year', 'month']).count()['message'].reset_index()
    timeline['time'] = timeline['month'] + "-" + timeline['year'].astype(str)
    return timeline

def daily_timeline(selected_user, df):
    if selected_user != 'Overall':
        df = df[df['user'] == selected_user]
    return df.groupby('date').count()['message'].reset_index()

def week_activity_map(selected_user, df):
    if selected_user != 'Overall':
        df = df[df['user'] == selected_user]
    return df['day_of_week'].value_counts()

def month_activity_map(selected_user, df):
    if selected_user != 'Overall':
        df = df[df['user'] == selected_user]
    return df['month'].value_counts()

def plot_topic_distribution(df):
    topic_counts = df['topic'].value_counts().sort_index()
    fig = px.bar(x=topic_counts.index, y=topic_counts.values, title="Topic Distribution", color_discrete_sequence=['viridis'])
    return fig

def topic_distribution_over_time(df, time_freq='M'):
    df['time_period'] = df['date'].dt.to_period(time_freq)
    return df.groupby(['time_period', 'topic']).size().unstack(fill_value=0)

def plot_topic_distribution_over_time_plotly(topic_distribution):
    topic_distribution = topic_distribution.reset_index()
    topic_distribution['time_period'] = topic_distribution['time_period'].dt.to_timestamp()
    topic_distribution = topic_distribution.melt(id_vars='time_period', var_name='topic', value_name='count')
    fig = px.line(topic_distribution, x='time_period', y='count', color='topic', title="Topic Distribution Over Time")
    fig.update_layout(legend_title_text='Topics', xaxis_tickangle=-45)
    return fig

def plot_clusters(reduced_features, clusters):
    fig = px.scatter(x=reduced_features[:, 0], y=reduced_features[:, 1], color=clusters, title="Message Clusters (t-SNE)")
    return fig
def most_common_words(selected_user, df):
    # f = open('stop_hinglish.txt','r')
    stop_words = df

    if selected_user != 'Overall':
        df = df[df['user'] == selected_user]

    temp = df[df['user'] != 'group_notification']
    temp = temp[~temp['message'].str.lower().str.contains('<media omitted>')]

    words = []

    for message in temp['message']:
        for word in message.lower().split():
            if word not in stop_words:
                words.append(word)

    most_common_df = pd.DataFrame(Counter(words).most_common(20))
    return most_common_df

def emoji_helper(selected_user, df):
    if selected_user != 'Overall':
        df = df[df['user'] == selected_user]

    emojis = []
    for message in df['unfiltered_messages']:
        emojis.extend([c for c in message if c in emoji.EMOJI_DATA])

    emoji_df = pd.DataFrame(Counter(emojis).most_common(len(Counter(emojis))))

    return emoji_df
def plot_topic_distribution(df):
    """
    Plots the distribution of topics in the chat data.
    """
    topic_counts = df['topic'].value_counts().sort_index()  
    fig, ax = plt.subplots()
    sns.barplot(x=topic_counts.index, y=topic_counts.values, ax=ax, palette="viridis")
    ax.set_title("Topic Distribution")
    ax.set_xlabel("Topic")
    ax.set_ylabel("Number of Messages")
    return fig

def most_frequent_keywords(messages, top_n=10):
    """
    Extracts the most frequent keywords from a list of messages.
    """
    words = [word for msg in messages for word in msg.split()]
    word_freq = Counter(words)
    return word_freq.most_common(top_n)
def plot_topic_distribution_over_time(topic_distribution):
    """
    Plots the distribution of topics over time using a line chart.
    """
    fig, ax = plt.subplots(figsize=(12, 6))
    
    # Plot each topic as a separate line
    for topic in topic_distribution.columns:
        ax.plot(topic_distribution.index.to_timestamp(), topic_distribution[topic], label=f"Topic {topic}")
    
    ax.set_title("Topic Distribution Over Time")
    ax.set_xlabel("Time Period")
    ax.set_ylabel("Number of Messages")
    ax.legend(title="Topics", bbox_to_anchor=(1.05, 1), loc='upper left')
    plt.xticks(rotation=45)
    plt.tight_layout()
    return fig

def plot_most_frequent_keywords(keywords):
    """
    Plots the most frequent keywords.
    """
    words, counts = zip(*keywords)
    fig, ax = plt.subplots()
    sns.barplot(x=list(counts), y=list(words), ax=ax, palette="viridis")
    ax.set_title("Most Frequent Keywords")
    ax.set_xlabel("Frequency")
    ax.set_ylabel("Keyword")
    return fig
def topic_distribution_over_time(df, time_freq='M'):
    """
    Analyzes the distribution of topics over time.
    """
    # Group by time interval and topic
    df['time_period'] = df['date'].dt.to_period(time_freq)
    topic_distribution = df.groupby(['time_period', 'topic']).size().unstack(fill_value=0)
    return topic_distribution

def plot_topic_distribution_over_time(topic_distribution):
    """
    Plots the distribution of topics over time using a line chart.
    """
    fig, ax = plt.subplots(figsize=(12, 6))
    
    # Plot each topic as a separate line
    for topic in topic_distribution.columns:
        ax.plot(topic_distribution.index.to_timestamp(), topic_distribution[topic], label=f"Topic {topic}")
    
    ax.set_title("Topic Distribution Over Time")
    ax.set_xlabel("Time Period")
    ax.set_ylabel("Number of Messages")
    ax.legend(title="Topics", bbox_to_anchor=(1.05, 1), loc='upper left')
    plt.xticks(rotation=45)
    plt.tight_layout()
    return fig

def plot_topic_distribution_over_time_plotly(topic_distribution):
    """
    Plots the distribution of topics over time using Plotly.
    """
    topic_distribution = topic_distribution.reset_index()
    topic_distribution['time_period'] = topic_distribution['time_period'].dt.to_timestamp()
    topic_distribution = topic_distribution.melt(id_vars='time_period', var_name='topic', value_name='count')
    
    fig = px.line(topic_distribution, x='time_period', y='count', color='topic', 
                  title="Topic Distribution Over Time", labels={'time_period': 'Time Period', 'count': 'Number of Messages'})
    fig.update_layout(legend_title_text='Topics', xaxis_tickangle=-45)
    return fig
def plot_clusters(reduced_features, clusters):
    """
    Visualize clusters using t-SNE.
    Args:
        reduced_features (np.array): 2D array of reduced features.
        clusters (np.array): Cluster labels.
    Returns:
        fig (plt.Figure): Matplotlib figure object.
    """
    plt.figure(figsize=(10, 8))
    sns.scatterplot(
        x=reduced_features[:, 0],
        y=reduced_features[:, 1],
        hue=clusters,
        palette="viridis",
        legend="full"
    )
    plt.title("Message Clusters (t-SNE Visualization)")
    plt.xlabel("t-SNE Component 1")
    plt.ylabel("t-SNE Component 2")
    plt.tight_layout()
    return plt.gcf()
def get_cluster_labels(df, n_clusters):
    """
    Generate descriptive labels for each cluster based on top keywords.
    """
    from sklearn.feature_extraction.text import TfidfVectorizer
    import numpy as np

    vectorizer = TfidfVectorizer(max_features=5000, stop_words='english')
    tfidf_matrix = vectorizer.fit_transform(df['lemmatized_message'])

    cluster_labels = {}
    for cluster_id in range(n_clusters):
        cluster_indices = df[df['cluster'] == cluster_id].index
        if len(cluster_indices) > 0:
            cluster_tfidf = tfidf_matrix[cluster_indices]
            top_keywords = np.argsort(cluster_tfidf.sum(axis=0).A1)[-3:][::-1]
            cluster_labels[cluster_id] = ", ".join(vectorizer.get_feature_names_out()[top_keywords])
        else:
            cluster_labels[cluster_id] = "No dominant theme"
    return cluster_labels

def get_temporal_trends(df):
    """
    Analyze temporal trends for each cluster (peak day and time).
    """
    temporal_trends = {}
    for cluster_id in df['cluster'].unique():
        cluster_data = df[df['cluster'] == cluster_id]
        if not cluster_data.empty:
            peak_day = cluster_data['day_of_week'].mode()[0]
            peak_time = cluster_data['hour'].mode()[0]
            temporal_trends[cluster_id] = {"peak_day": peak_day, "peak_time": f"{peak_time}:00"}
    return temporal_trends

def get_user_contributions(df):
    """
    Identify top contributors for each cluster.
    """
    user_contributions = {}
    for cluster_id in df['cluster'].unique():
        cluster_data = df[df['cluster'] == cluster_id]
        if not cluster_data.empty:
            top_users = cluster_data['user'].value_counts().head(3).index.tolist()
            user_contributions[cluster_id] = top_users
    return user_contributions

def get_sentiment_by_cluster(df):
    """
    Analyze sentiment distribution for each cluster.
    """
    sentiment_by_cluster = {}
    for cluster_id in df['cluster'].unique():
        cluster_data = df[df['cluster'] == cluster_id]
        if not cluster_data.empty:
            sentiment_counts = cluster_data['sentiment'].value_counts(normalize=True) * 100
            sentiment_by_cluster[cluster_id] = {
                "positive": round(sentiment_counts.get('positive', 0)),
                "neutral": round(sentiment_counts.get('neutral', 0)),
                "negative": round(sentiment_counts.get('negative', 0))
            }
    return sentiment_by_cluster

def detect_anomalies(df):
    """
    Detect anomalies in each cluster (e.g., high link or media share).
    """
    anomalies = {}
    for cluster_id in df['cluster'].unique():
        cluster_data = df[df['cluster'] == cluster_id]
        if not cluster_data.empty:
            link_share = (cluster_data['message'].str.contains('http').mean()) * 100
            media_share = (cluster_data['message'].str.contains('<media omitted>').mean()) * 100
            if link_share > 50:
                anomalies[cluster_id] = f"{round(link_share)}% of messages contain links."
            elif media_share > 50:
                anomalies[cluster_id] = f"{round(media_share)}% of messages are media files."
    return anomalies

def generate_recommendations(df):
    """
    Generate actionable recommendations based on cluster insights.
    """
    recommendations = []
    for cluster_id in df['cluster'].unique():
        cluster_data = df[df['cluster'] == cluster_id]
        if not cluster_data.empty:
            sentiment_counts = cluster_data['sentiment'].value_counts(normalize=True) * 100
            if sentiment_counts.get('negative', 0) > 50:
                recommendations.append(f"Address negative sentiment in Cluster {cluster_id} by revisiting feedback processes.")
            if cluster_data['message'].str.contains('http').mean() > 0.5:
                recommendations.append(f"Pin resources from Cluster {cluster_id} (most-shared links) for easy access.")
    return recommendations