File size: 2,930 Bytes
55946c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
from textblob import TextBlob

def load_data(uploaded_file):
    # Load Excel file, supports both .xlsx and .xls
    try:
        df = pd.read_excel(uploaded_file)  # Automatically detects file format
        df.columns = df.columns.str.strip().str.lower()  # Normalize column names
        return df
    except Exception as e:
        st.error(f"Error loading file: {e}")
        return None

def analyze_sentiment(text):
    polarity = TextBlob(str(text)).sentiment.polarity
    if polarity >= 0.6:
        return "Very Positive"
    elif polarity >= 0.2:
        return "Positive"
    elif polarity > -0.2:
        return "Neutral"
    elif polarity > -0.6:
        return "Negative"
    else:
        return "Very Negative"

st.title("Sentiment Analysis with Pie Chart")

# File uploader supports .xlsx and .xls
uploaded_file = st.file_uploader("Upload an Excel file with text data", type=["xlsx", "xls"])

if uploaded_file is not None:
    df = load_data(uploaded_file)
    if df is not None:
        st.write("Columns in your file:", df.columns.tolist())
        
        # Allow the user to select a column if 'text' is not already present
        if "text" not in df.columns:
            selected_column = st.selectbox("Select the column to use as text data:", df.columns)
            if st.button("Confirm Selection"):
                df.rename(columns={selected_column: "text"}, inplace=True)
                st.success(f"Column '{selected_column}' renamed to 'text'.")
        
        if "text" in df.columns:  # Check again if 'text' column is present after renaming
            df["Sentiment"] = df["text"].apply(analyze_sentiment)
            
            st.write("Here is a preview of the data:")
            st.write(df.head())
            
            sentiment_counts = df["Sentiment"].value_counts()
            
            fig, ax = plt.subplots()
            ax.pie(sentiment_counts, labels=sentiment_counts.index, autopct="%1.1f%%", colors=["green", "lightgreen", "gray", "orange", "red"])
            ax.set_title("Sentiment Distribution")
            st.pyplot(fig)
            
            # Export processed data to Excel file (.xlsx) without explicitly using xlsxwriter
            output_file = "sentiment_results.xlsx"
            df.to_excel(output_file, index=False, sheet_name="Sentiment Analysis")

            # Download button for Excel file
            with open(output_file, "rb") as f:
                st.download_button(
                    "Download Sentiment Data (Excel)",
                    f,
                    "sentiment_results.xlsx",
                    "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                )
        else:
            st.warning("Please select a column to rename as 'text' and proceed.")
else:
    st.write("Please upload an Excel file to get started.")