File size: 5,212 Bytes
02794f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import plotly.express as px
from PIL import Image

def run():
    #Membuat title
    st.title('Text-Based Twitter Sentiment Analysis')

    #Tambahkan gambar
    image = Image.open('twittersentiment.jpg')
    st.image(image, caption = 'Twitter Sentiment')

    #Membuat garis
    st.markdown('----')

    #Masukkan pandas dataframe

    #Show dataframe
    df = pd.read_csv('tweets-update.csv')
    st.dataframe(df)
    st.write('Source : https://www.kaggle.com/datasets/yasserh/twitter-tweets-sentiment-dataset')

    st.markdown('----')
    st.title('Exploratory Data Analysis')
    #Bar Plot
    st.write('#### Distribution of Sentiments')
    fig_sentiments = plt.figure(figsize=(10, 6))
    sns.countplot(x='sentiment', data=df)
    plt.xlabel('Sentiment Label')
    plt.ylabel('Count')
    plt.title('Distribution of Sentiments')
    st.pyplot(fig_sentiments)

    # Positive Sentiment Tweets Bar
    st.write('#### Distribution of Text Length for Positive Sentiment Tweets')
    fig_length_positive = plt.figure(figsize=(14, 7))

    # Handle NaN values in 'text_processed'
    df['length'] = df['text_processed'].apply(lambda x: len(str(x).split()) if pd.notna(x) else 0)

    ax1 = fig_length_positive.add_subplot(122)
    sns.histplot(df[df['sentiment']=='positive']['length'], ax=ax1, color='green')
    describe_positive = df.length[df.sentiment=='positive'].describe().to_frame().round(2)

    ax2 = fig_length_positive.add_subplot(121)
    ax2.axis('off')
    font_size = 14
    bbox = [0, 0, 1, 1]
    table_positive = ax2.table(cellText=describe_positive.values, rowLabels=describe_positive.index, bbox=bbox, colLabels=describe_positive.columns)
    table_positive.set_fontsize(font_size)
    fig_length_positive.suptitle('Distribution of text length for positive sentiment tweets.', fontsize=16)

    st.pyplot(fig_length_positive)

    # negative Sentiment Tweets Bar
    st.write('#### Distribution of Text Length for negative Sentiment Tweets')
    fig_length_negative = plt.figure(figsize=(14, 7))

    # Handle NaN values in 'text_processed'
    df['length'] = df['text_processed'].apply(lambda x: len(str(x).split()) if pd.notna(x) else 0)

    ax1 = fig_length_negative.add_subplot(122)
    sns.histplot(df[df['sentiment']=='negative']['length'], ax=ax1, color='red')
    describe_negative = df.length[df.sentiment=='negative'].describe().to_frame().round(2)

    ax2 = fig_length_negative.add_subplot(121)
    ax2.axis('off')
    font_size = 14
    bbox = [0, 0, 1, 1]
    table_negative = ax2.table(cellText=describe_negative.values, rowLabels=describe_negative.index, bbox=bbox, colLabels=describe_negative.columns)
    table_negative.set_fontsize(font_size)
    fig_length_negative.suptitle('Distribution of text length for negative sentiment tweets.', fontsize=16)

    st.pyplot(fig_length_negative)

    # neutral Sentiment Tweets Bar
    st.write('#### Distribution of Text Length for neutral Sentiment Tweets')
    fig_length_neutral = plt.figure(figsize=(14, 7))

    # Handle NaN values in 'text_processed'
    df['length'] = df['text_processed'].apply(lambda x: len(str(x).split()) if pd.notna(x) else 0)

    ax1 = fig_length_neutral.add_subplot(122)
    sns.histplot(df[df['sentiment']=='neutral']['length'], ax=ax1, color='blue')
    describe_neutral = df.length[df.sentiment=='neutral'].describe().to_frame().round(2)

    ax2 = fig_length_neutral.add_subplot(121)
    ax2.axis('off')
    font_size = 14
    bbox = [0, 0, 1, 1]
    table_neutral = ax2.table(cellText=describe_neutral.values, rowLabels=describe_neutral.index, bbox=bbox, colLabels=describe_neutral.columns)
    table_neutral.set_fontsize(font_size)
    fig_length_neutral.suptitle('Distribution of text length for neutral sentiment tweets.', fontsize=16)

    st.pyplot(fig_length_neutral)

    # Membuat pie chart
    st.write('#### Pie Chart - Sentiment Distribution')
    labels = ['Neutral', 'Positive', 'Negative']
    size = df['sentiment'].value_counts()
    colors = ['lightgreen', 'lightskyblue', 'lightcoral']
    explode = [0.01, 0.01, 0.1]

    fig, axes = plt.subplots(figsize=(6, 5))
    plt.pie(size, colors=colors, explode=explode,
            labels=labels, shadow=True, startangle=90, autopct='%.2f%%')
    plt.title('Sentiment Distribution', fontsize=20)
    plt.legend()

    st.pyplot(fig)
    # #Membuat histogram
    # st.write('#### Histogram of Age')
    # fig = plt.figure(figsize=(15,5))
    # sns.histplot(df['Overall'], bins = 30, kde = True)
    # st.pyplot(fig)

    # #membuat histogram berdasarkan inputan user
    # st.write('#### Histogram berdasarkan input user')
    # #kalo mau pake radio button, ganti selectbox jadi radio
    # option = st.selectbox('Pilih Column : ', ('Age', 'Weight', 'Height', 'ShootingTotal'))
    # fig = plt.figure(figsize= (15,5))
    # sns.histplot(df[option], bins = 30, kde = True)
    # st.pyplot(fig)

    # #Membuat Plotly plot

    # st.write('#### Plotly Plot - ValueEUR vs Overall')
    # fig = px.scatter(df, x = 'ValueEUR', y = 'Overall', hover_data = ['Name', 'Age'])
    # st.plotly_chart(fig)

if __name__ == '__main__':
    run()