Update app.py
Browse files
app.py
CHANGED
@@ -63,7 +63,7 @@ color_palette = px.colors.sequential.Viridis
|
|
63 |
# Visualisation for Domain Distribution
|
64 |
def create_pie_chart(df, column, title):
|
65 |
fig = px.pie(df, names=column, title=title, hole=0.35)
|
66 |
-
fig.update_layout(margin=dict(l=20, r=20, t=50, b=20), legend=dict(x=0.1, y=1), font=dict(size=10)
|
67 |
fig.update_traces(marker=dict(colors=color_palette))
|
68 |
return fig
|
69 |
|
@@ -71,7 +71,7 @@ def create_pie_chart(df, column, title):
|
|
71 |
def create_gender_ethnicity_distribution_chart(df):
|
72 |
df['GenderOrEthnicity'] = df['Domain'].apply(lambda x: "Gender: Women & LGBTQIA+" if x in ["Women", "LGBTQIA+"] else "Ethnicity")
|
73 |
fig = px.pie(df, names='GenderOrEthnicity', title='Distribution of Gender versus Ethnicity', hole=0.35)
|
74 |
-
fig.update_layout(margin=dict(l=20, r=20, t=50, b=20), legend=dict(x=0.1, y=1), font=dict(size=10)
|
75 |
return fig
|
76 |
|
77 |
# Visualization for Sentiment Distribution Across Domains
|
@@ -79,7 +79,7 @@ def create_sentiment_distribution_chart(df):
|
|
79 |
df['Discrimination'] = df['Discrimination'].replace({"Non Discriminative": "Non-Discriminative"}) # Assuming typo in the original script
|
80 |
domain_counts = df.groupby(['Domain', 'Sentiment']).size().reset_index(name='counts')
|
81 |
fig = px.bar(domain_counts, x='Domain', y='counts', color='Sentiment', title="Sentiment Distribution Across Domains", barmode='stack')
|
82 |
-
fig.update_layout(margin=dict(l=20, r=20, t=50, b=20), xaxis_title="Domain", yaxis_title="Counts", font=dict(size=10)
|
83 |
return fig
|
84 |
|
85 |
# Visualization for Correlation between Sentiment and Discrimination
|
@@ -96,7 +96,7 @@ def create_sentiment_discrimination_grouped_chart(df):
|
|
96 |
# Proceeding to plot only if we have data to plot
|
97 |
if not melted_df.empty:
|
98 |
fig = px.bar(melted_df, x='Sentiment', y='Count', color='Discrimination', barmode='group', title="Sentiment vs. Discrimination")
|
99 |
-
fig.update_layout(margin=dict(l=20, r=20, t=50, b=20), xaxis_title="Sentiment", yaxis_title="Count", font=dict(size=10)
|
100 |
return fig
|
101 |
else:
|
102 |
return "No data to display for the selected filters."
|
@@ -111,7 +111,7 @@ def create_top_negative_sentiment_domains_chart(df):
|
|
111 |
colors = ['limegreen', 'crimson', 'darkcyan']
|
112 |
fig = px.bar(domain_counts_subset, x='Count', y='Domain', title='Top Domains with Negative Sentiment', color='Domain',
|
113 |
orientation='h', color_discrete_sequence=colors)
|
114 |
-
fig.update_layout(margin=dict(l=20, r=20, t=50, b=20), xaxis_title="Negative sentiment content Count", yaxis_title="Domain", font=dict(size=10)
|
115 |
return fig
|
116 |
|
117 |
# Function for Key Phrases in Negative Sentiment Content Chart
|
@@ -122,26 +122,37 @@ def create_key_phrases_negative_sentiment_chart(df):
|
|
122 |
ngram_freq = pd.DataFrame(sorted([(count_values[i], k) for k, i in cv.vocabulary_.items()], reverse=True))
|
123 |
ngram_freq.columns = ['frequency', 'ngram']
|
124 |
fig = px.bar(ngram_freq.head(10), x='frequency', y='ngram', orientation='h', title='Key phrases in Negative Sentiment Content')
|
125 |
-
fig.update_layout(margin=dict(l=20, r=20, t=50, b=20), xaxis_title="Frequency", yaxis_title="Trigram", font=dict(size=10)
|
126 |
return fig
|
127 |
|
128 |
# Function for Key Phrases in Positive Sentiment Content Chart
|
129 |
def create_key_phrases_positive_sentiment_chart(df):
|
|
|
|
|
|
|
|
|
130 |
cv = CountVectorizer(ngram_range=(3, 3), stop_words='english')
|
131 |
-
|
|
|
|
|
|
|
|
|
132 |
count_values = trigrams.toarray().sum(axis=0)
|
133 |
ngram_freq = pd.DataFrame(sorted([(count_values[i], k) for k, i in cv.vocabulary_.items()], reverse=True))
|
134 |
ngram_freq.columns = ['frequency', 'ngram']
|
|
|
|
|
135 |
fig = px.bar(ngram_freq.head(10), x='frequency', y='ngram', orientation='h', title='Key phrases in Positive Sentiment Content')
|
136 |
-
|
137 |
-
|
|
|
138 |
|
139 |
# Function for Prevalence of Discriminatory Content Chart
|
140 |
def create_prevalence_discriminatory_content_chart(df):
|
141 |
domain_counts = df.groupby(['Domain', 'Discrimination']).size().unstack(fill_value=0)
|
142 |
fig = px.bar(domain_counts, x=domain_counts.index, y=['Discriminative', 'Non-Discriminative'], barmode='group',
|
143 |
title='Prevalence of Discriminatory Content')
|
144 |
-
fig.update_layout(margin=dict(l=20, r=20, t=50, b=20), xaxis_title="Domain", yaxis_title="Count", font=dict(size=10)
|
145 |
return fig
|
146 |
|
147 |
# Function for Top Domains with Discriminatory Content Chart
|
@@ -152,7 +163,7 @@ def create_top_discriminatory_domains_chart(df):
|
|
152 |
domain_counts_subset = domain_counts_subset.rename(columns={'Discriminative': 'Count'})
|
153 |
fig = px.bar(domain_counts_subset, x='Count', y=domain_counts_subset.index, orientation='h',
|
154 |
title='Top Domains with Discriminatory Content')
|
155 |
-
fig.update_layout(margin=dict(l=20, r=20, t=50, b=20), xaxis_title="Discriminatory Content Count", yaxis_title="Domain", font=dict(size=10)
|
156 |
return fig
|
157 |
|
158 |
# Function for Channel-wise Sentiment Over Time Chart
|
|
|
63 |
# Visualisation for Domain Distribution
|
64 |
def create_pie_chart(df, column, title):
|
65 |
fig = px.pie(df, names=column, title=title, hole=0.35)
|
66 |
+
fig.update_layout(margin=dict(l=20, r=20, t=50, b=20), legend=dict(x=0.1, y=1), font=dict(size=10))
|
67 |
fig.update_traces(marker=dict(colors=color_palette))
|
68 |
return fig
|
69 |
|
|
|
71 |
def create_gender_ethnicity_distribution_chart(df):
|
72 |
df['GenderOrEthnicity'] = df['Domain'].apply(lambda x: "Gender: Women & LGBTQIA+" if x in ["Women", "LGBTQIA+"] else "Ethnicity")
|
73 |
fig = px.pie(df, names='GenderOrEthnicity', title='Distribution of Gender versus Ethnicity', hole=0.35)
|
74 |
+
fig.update_layout(margin=dict(l=20, r=20, t=50, b=20), legend=dict(x=0.1, y=1), font=dict(size=10))
|
75 |
return fig
|
76 |
|
77 |
# Visualization for Sentiment Distribution Across Domains
|
|
|
79 |
df['Discrimination'] = df['Discrimination'].replace({"Non Discriminative": "Non-Discriminative"}) # Assuming typo in the original script
|
80 |
domain_counts = df.groupby(['Domain', 'Sentiment']).size().reset_index(name='counts')
|
81 |
fig = px.bar(domain_counts, x='Domain', y='counts', color='Sentiment', title="Sentiment Distribution Across Domains", barmode='stack')
|
82 |
+
fig.update_layout(margin=dict(l=20, r=20, t=50, b=20), xaxis_title="Domain", yaxis_title="Counts", font=dict(size=10))
|
83 |
return fig
|
84 |
|
85 |
# Visualization for Correlation between Sentiment and Discrimination
|
|
|
96 |
# Proceeding to plot only if we have data to plot
|
97 |
if not melted_df.empty:
|
98 |
fig = px.bar(melted_df, x='Sentiment', y='Count', color='Discrimination', barmode='group', title="Sentiment vs. Discrimination")
|
99 |
+
fig.update_layout(margin=dict(l=20, r=20, t=50, b=20), xaxis_title="Sentiment", yaxis_title="Count", font=dict(size=10))
|
100 |
return fig
|
101 |
else:
|
102 |
return "No data to display for the selected filters."
|
|
|
111 |
colors = ['limegreen', 'crimson', 'darkcyan']
|
112 |
fig = px.bar(domain_counts_subset, x='Count', y='Domain', title='Top Domains with Negative Sentiment', color='Domain',
|
113 |
orientation='h', color_discrete_sequence=colors)
|
114 |
+
fig.update_layout(margin=dict(l=20, r=20, t=50, b=20), xaxis_title="Negative sentiment content Count", yaxis_title="Domain", font=dict(size=10))
|
115 |
return fig
|
116 |
|
117 |
# Function for Key Phrases in Negative Sentiment Content Chart
|
|
|
122 |
ngram_freq = pd.DataFrame(sorted([(count_values[i], k) for k, i in cv.vocabulary_.items()], reverse=True))
|
123 |
ngram_freq.columns = ['frequency', 'ngram']
|
124 |
fig = px.bar(ngram_freq.head(10), x='frequency', y='ngram', orientation='h', title='Key phrases in Negative Sentiment Content')
|
125 |
+
fig.update_layout(margin=dict(l=20, r=20, t=50, b=20), xaxis_title="Frequency", yaxis_title="Trigram", font=dict(size=10))
|
126 |
return fig
|
127 |
|
128 |
# Function for Key Phrases in Positive Sentiment Content Chart
|
129 |
def create_key_phrases_positive_sentiment_chart(df):
|
130 |
+
# Filter the DataFrame for positive sentiments and drop any rows with NaN in 'Content'
|
131 |
+
positive_df = df[df['Sentiment'] == 'Positive'].dropna(subset=['Content'])
|
132 |
+
|
133 |
+
# Create a CountVectorizer instance
|
134 |
cv = CountVectorizer(ngram_range=(3, 3), stop_words='english')
|
135 |
+
|
136 |
+
# Apply CountVectorizer only on non-null content
|
137 |
+
trigrams = cv.fit_transform(positive_df['Content'])
|
138 |
+
|
139 |
+
# Sum the frequency of each n-gram and create a DataFrame
|
140 |
count_values = trigrams.toarray().sum(axis=0)
|
141 |
ngram_freq = pd.DataFrame(sorted([(count_values[i], k) for k, i in cv.vocabulary_.items()], reverse=True))
|
142 |
ngram_freq.columns = ['frequency', 'ngram']
|
143 |
+
|
144 |
+
# Create the bar chart
|
145 |
fig = px.bar(ngram_freq.head(10), x='frequency', y='ngram', orientation='h', title='Key phrases in Positive Sentiment Content')
|
146 |
+
|
147 |
+
# Update layout settings to fit and look better
|
148 |
+
fig.update_layout(margin=dict(l=20, r=20, t=50, b=20), xaxis_title="Frequency", yaxis_title="Trigram", font=dict(size=10))
|
149 |
|
150 |
# Function for Prevalence of Discriminatory Content Chart
|
151 |
def create_prevalence_discriminatory_content_chart(df):
|
152 |
domain_counts = df.groupby(['Domain', 'Discrimination']).size().unstack(fill_value=0)
|
153 |
fig = px.bar(domain_counts, x=domain_counts.index, y=['Discriminative', 'Non-Discriminative'], barmode='group',
|
154 |
title='Prevalence of Discriminatory Content')
|
155 |
+
fig.update_layout(margin=dict(l=20, r=20, t=50, b=20), xaxis_title="Domain", yaxis_title="Count", font=dict(size=10))
|
156 |
return fig
|
157 |
|
158 |
# Function for Top Domains with Discriminatory Content Chart
|
|
|
163 |
domain_counts_subset = domain_counts_subset.rename(columns={'Discriminative': 'Count'})
|
164 |
fig = px.bar(domain_counts_subset, x='Count', y=domain_counts_subset.index, orientation='h',
|
165 |
title='Top Domains with Discriminatory Content')
|
166 |
+
fig.update_layout(margin=dict(l=20, r=20, t=50, b=20), xaxis_title="Discriminatory Content Count", yaxis_title="Domain", font=dict(size=10))
|
167 |
return fig
|
168 |
|
169 |
# Function for Channel-wise Sentiment Over Time Chart
|