santarabantoosoo commited on
Commit
571d313
1 Parent(s): c802c7b

basic sentiment analysis

Browse files
Files changed (2) hide show
  1. app.py +124 -0
  2. requirements.txt +6 -0
app.py ADDED
@@ -0,0 +1,124 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ import numpy as np
4
+ import matplotlib.pyplot as plt
5
+ import plotly.express as px
6
+ from stop_words import get_stop_words
7
+ from wordcloud import WordCloud
8
+ from datasets import load_dataset
9
+
10
+
11
+ ## import data
12
+
13
+ dataset = load_dataset("Santarabantoosoo/italian_long_covid_tweets")
14
+ data = pd.DataFrame.from_dict(dataset["train"])
15
+
16
+ # formulate a wordcloud for each emotion
17
+
18
+ stop = get_stop_words('italian')
19
+
20
+ # Wordcloud with anger tweets
21
+ angry_tweets = data['tweet'][data["emotion"] == 'anger']
22
+ stop_words = ["https", "co", "RT"] + list(stop)
23
+ anger_wordcloud = WordCloud(max_font_size=50, max_words=50, background_color="white", stopwords = stop_words).generate(str(angry_tweets))
24
+
25
+ # Wordcloud with sad tweets
26
+ sad_tweets = data['tweet'][data["emotion"] == 'sadness']
27
+ stop_words = ["https", "co", "RT"] + list(stop)
28
+ sad_wordcloud = WordCloud(max_font_size=50, max_words=50, background_color="white", stopwords = stop_words).generate(str(sad_tweets))
29
+
30
+ # Wordcloud with joy tweets
31
+ joy_tweets = data['tweet'][data["emotion"] == 'joy']
32
+ stop_words = ["https", "co", "RT"] + list(stop)
33
+ joy_wordcloud = WordCloud(max_font_size=50, max_words=50, background_color="white", stopwords = stop_words).generate(str(joy_tweets))
34
+
35
+
36
+ # Wordcloud with fear tweets
37
+ fear_tweets = data['tweet'][data["emotion"] == 'fear']
38
+ stop_words = ["https", "co", "RT"] + list(stop)
39
+ fear_wordcloud = WordCloud(max_font_size=50, max_words=50, background_color="white", stopwords = stop_words).generate(str(fear_tweets))
40
+
41
+ # combine wordclouds in a single matplotlib figure
42
+
43
+ wc_fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2,2)
44
+
45
+ wc_fig.tight_layout()
46
+
47
+ ax1.imshow(sad_wordcloud, interpolation="bilinear")
48
+
49
+ ax1.axis("off")
50
+
51
+ ax1.set_title('Sadness', {'fontsize': 30})
52
+
53
+
54
+ ax2.imshow(joy_wordcloud, interpolation="bilinear")
55
+
56
+ ax2.axis("off")
57
+
58
+ ax2.set_title('Joy', {'fontsize': 30})
59
+
60
+
61
+ ax3.imshow(fear_wordcloud, interpolation="bilinear")
62
+
63
+ ax3.axis("off")
64
+
65
+ ax3.set_title('Fear', {'fontsize': 30})
66
+
67
+
68
+ ax4.imshow(anger_wordcloud, interpolation="bilinear")
69
+
70
+ ax4.axis("off")
71
+
72
+ ax4.set_title('Anger', {'fontsize': 30})
73
+
74
+
75
+ plt.show()
76
+
77
+ # plot a pie plot for emotions' distribution
78
+
79
+ number_tweets_per_day = data.groupby(['date', 'emotion']).agg({'id': 'count'}).reset_index()
80
+
81
+ number_tweets_per_day["tweet_date"] = pd.to_datetime(number_tweets_per_day["date"])
82
+
83
+ time_fig = px.line(number_tweets_per_day, x = 'tweet_date', y = 'id', labels = {'id': 'count'}, color = 'emotion',
84
+ color_discrete_sequence=px.colors.qualitative.G10)
85
+
86
+ # create a lineplot for emotions
87
+
88
+ sentiment_counts = data.groupby('emotion').agg({'id' : 'size'}).reset_index()
89
+ sentiment_counts.rename(columns = {'id':'count'}, inplace = True)
90
+ sent_fig = px.pie(sentiment_counts, values='count', names='emotion', title='Tweets within each emotion', labels = {'id': 'count'},
91
+ color_discrete_sequence=px.colors.qualitative.G10)
92
+ sent_fig
93
+
94
+
95
+ def display_plot(image_choice):
96
+
97
+ if image_choice == 'Sentiment distribution':
98
+ return sent_fig
99
+
100
+ elif image_choice == 'Time series':
101
+ return time_fig
102
+
103
+ elif image_choice == 'Word clouds':
104
+ return wc_fig
105
+
106
+
107
+ with gr.Blocks() as demo:
108
+ gr.Markdown("## Choose your adventure")
109
+ with gr.Tabs():
110
+ with gr.TabItem("Sentiment analysis"):
111
+ text_input = [gr.Radio(choices = ['Sentiment distribution', 'Word clouds', 'Time series'], label = 'Choose ur plot')]
112
+ plot_output = gr.Plot()
113
+ text_button = gr.Button("Submit")
114
+
115
+ text_button.click(display_plot, inputs=text_input, outputs=plot_output)
116
+
117
+ with gr.TabItem("Word frequency"):
118
+ gr.Markdown("Nothing here yet")
119
+
120
+ with gr.TabItem("Topic modeling"):
121
+ gr.Markdown("Nothing here yet")
122
+
123
+
124
+ demo.launch();
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ gradio
2
+ pandas
3
+ matplotlib
4
+ plotly
5
+ stop_words
6
+ wordcloud