Text_Analytics_Group / pages /2_Analysis.py
SuchithTissera's picture
Update pages/2_Analysis.py
ed4bc65 verified
Raw History Blame Contribute Delete
3.83 kB
#importing the necessary libraries for analysis
import streamlit as st
import pandas as pd
import plotly.express as px
from collections import Counter
#adding the css presentation from the styles file in utils
from utils.styles import load_css
load_css()
#creating the topics and content
st.markdown('<div class="main_title">📊 Analysis of Data</div>', unsafe_allow_html=True)
st.markdown('<div class="feature_text">In our model we label the data we pre-processed the content throught the following steps,</div>', unsafe_allow_html=True)
st.markdown("""
<div class="feature_text">
<ul>
<li>Making words lower case</li>
<li>Removing punctuations</li>
<li>Removing stop words</li>
</ul>
</div>
""", unsafe_allow_html=True)
st.markdown('<div class="feature_text">This page highlights the below explanatory data analysis,</div>', unsafe_allow_html=True)
st.markdown("""
<div class="feature_text">
<ul>
<li>Length of the normal and pre-processed content</li>
<li>A bar chart of topic distribution</li>
<li>A pie chart of the most frequent words</li>
</ul>
</div>
""", unsafe_allow_html=True)
#dividing the page
st.divider()
#getting the csv file from the classification page
if "uploaded_content" not in st.session_state:
st.warning("Please upload and classify data first.")
st.stop()
uploaded_content = st.session_state["uploaded_content"]
#1.carrying out the text analytics
#creating the lengths
#the normal lengths
word_counts_orig = uploaded_content["content"].astype(str).apply(lambda x: len(x.split()))
mean_orig = word_counts_orig.mean()
max_orig = word_counts_orig.max()
min_orig = word_counts_orig.min()
#the pre-processed lengths
word_counts_new = uploaded_content["pre-processed"].astype(str).apply(lambda x: len(x.split()))
mean_new = word_counts_new.mean()
max_new = word_counts_new.max()
min_new = word_counts_new.min()
#carrying out the text analysis
st.subheader("Text Length Comparison")
with st.container(border=True):
st.markdown("### **Before Pre-Processing**")
c1, c2, c3 = st.columns(3)
c1.metric("Max Words", int(max_orig))
c2.metric("Min Words", int(min_orig))
c3.metric("Mean Words", f"{mean_orig:.1f}")
st.divider()
st.markdown("### **After Pre-Processing**")
c4, c5, c6 = st.columns(3)
c4.metric("Max Words", int(max_new))
c5.metric("Min Words", int(min_new))
c6.metric("Mean Words", f"{mean_new:.1f}", delta=f"{mean_new - mean_orig:.1f}")
#dividing the page
st.divider()
#2.carrying out the bar chart
st.subheader("Bar Chart of News Topics")
class_counts = uploaded_content["class"].value_counts().reset_index()
class_counts.columns = ['Topics (classes)', 'Number of Articles']
#creating the bar chart content
fig = px.bar(
class_counts,
x='Topics (classes)',
y='Number of Articles',
text='Number of Articles',
color='Number of Articles',
color_continuous_scale=['#1f4e79', '#f2c94c'],
template='plotly_white'
)
fig.update_traces(textposition='outside')
fig.update_layout(xaxis_title="<b>Category</b>", yaxis_title="<b>Count</b>", coloraxis_showscale=False)
#displaying the bar chart
st.plotly_chart(fig, use_container_width=True)
st.divider()
#3.carrying out the pie chart
st.subheader("Word Frequency Analysis")
all_words = " ".join(uploaded_content["pre-processed"].astype(str)).split()
word_counts = Counter(all_words).most_common(10)
word_df = pd.DataFrame(word_counts, columns=['Word', 'Frequency'])
#creating the pie chart
fig_pie = px.pie(
word_df,
values='Frequency',
names='Word',
title='',
hole=0.4,
color_discrete_sequence=px.colors.sequential.Blues_r
)
fig_pie.update_traces(textinfo='percent+label', marker=dict(line=dict(color='#FFFFFF', width=2)))
#displaying the pie chart
st.plotly_chart(fig_pie, use_container_width=True)