File size: 2,629 Bytes
6d53774
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 nltk
from textblob import TextBlob
import matplotlib.pyplot as plt
from wordcloud import WordCloud

# Ensure necessary NLTK datasets are downloaded
nltk.download('punkt')

# Function to analyze sentiment
def analyze_sentiment(text):
    analysis = TextBlob(text)
    return analysis.sentiment.polarity  # Returns sentiment score (-1 to 1)

# Function to categorize microaggressions (basic NLP)
def categorize_microaggression(text):
    keywords = {
        "Microinvalidation": ["you're overreacting", "stop being so sensitive", "I don’t see color"],
        "Microinsult": ["you’re so articulate", "where are you really from", "you must be good at math"],
        "Microassault": ["racial slur", "explicit insult", "offensive joke"]
    }
    
    for category, phrases in keywords.items():
        for phrase in phrases:
            if phrase in text.lower():
                return category
    return "Uncategorized"

# Streamlit UI
st.title("MicroAggression Insight Tool")
st.write("Analyze, categorize, and visualize reported microaggressions.")

# Collect user input
user_input = st.text_area("Enter a microaggression example:")

if st.button("Analyze"):
    if user_input:
        sentiment_score = analyze_sentiment(user_input)
        category = categorize_microaggression(user_input)

        # Display results
        st.write(f"**Predicted Category:** {category}")
        st.write(f"**Sentiment Score:** {sentiment_score:.2f} (Negative: -1, Neutral: 0, Positive: 1)")

        # Store input in a dataframe
        df = pd.DataFrame({"Text": [user_input], "Category": [category], "Sentiment": [sentiment_score]})
        
        # Save locally (optional)
        df.to_csv("data.csv", mode='a', header=False, index=False)

# Load existing data
try:
    data = pd.read_csv("data.csv", names=["Text", "Category", "Sentiment"])
    
    if not data.empty:
        st.subheader("Data Insights")
        
        # Show category distribution
        st.write("### Microaggression Category Distribution")
        category_counts = data["Category"].value_counts()
        fig, ax = plt.subplots()
        category_counts.plot(kind='bar', ax=ax)
        st.pyplot(fig)

        # Generate a word cloud
        st.write("### Common Words in Microaggressions")
        wordcloud = WordCloud(width=800, height=400, background_color="white").generate(" ".join(data["Text"]))
        fig, ax = plt.subplots()
        ax.imshow(wordcloud, interpolation="bilinear")
        ax.axis("off")
        st.pyplot(fig)
except FileNotFoundError:
    st.write("No data available yet.")