-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtextPage.py
74 lines (68 loc) · 2.86 KB
/
textPage.py
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 streamlit.components.v1 as components
from textblob import TextBlob
from PIL import Image
import text2emotion as te
import plotly.graph_objects as go
def plotPie(labels, values):
fig = go.Figure(
go.Pie(
labels = labels,
values = values,
hoverinfo = "label+percent",
textinfo = "value"
))
st.plotly_chart(fig)
def getPolarity(userText):
tb = TextBlob(userText)
polarity = round(tb.polarity, 2)
subjectivity = round(tb.subjectivity, 2)
if polarity>0:
return polarity, subjectivity, "Positive"
elif polarity==0:
return polarity, subjectivity, "Neutral"
else:
return polarity, subjectivity, "Negative"
def getSentiments(userText, type):
if(type == 'Positive/Negative/Neutral - TextBlob'):
polarity, subjectivity, status = getPolarity(userText)
if(status=="Positive"):
image = Image.open('./images/positive.PNG')
elif(status == "Negative"):
image = Image.open('./images/negative.PNG')
else:
image = Image.open('./images/neutral.PNG')
col1, col2, col3 = st.columns(3)
col1.metric("Polarity", polarity, None)
col2.metric("Subjectivity", subjectivity, None)
col3.metric("Result", status, None)
st.image(image, caption=status)
elif(type == 'Happy/Sad/Angry/Fear/Surprise - text2emotion'):
emotion = dict(te.get_emotion(userText))
col1, col2, col3, col4, col5 = st.columns(5)
col1.metric("Happy 😊", emotion['Happy'], None)
col2.metric("Sad 😔", emotion['Sad'], None)
col3.metric("Angry 😠", emotion['Angry'], None)
col4.metric("Fear 😨", emotion['Fear'], None)
col5.metric("Surprise 😲", emotion['Surprise'], None)
plotPie(list(emotion.keys()), list(emotion.values()))
def renderPage():
st.title("Sentiment Analysis 😊😐😕😡")
components.html("""<hr style="height:3px;border:none;color:#333;background-color:#333; margin-bottom: 10px" /> """)
# st.markdown("### User Input Text Analysis")
st.subheader("User Input Text Analysis")
st.text("Analyzing text data given by the user and find sentiments within it.")
st.text("")
userText = st.text_input('User Input', placeholder='Input text HERE')
st.text("")
type = st.selectbox(
'Type of analysis',
('Positive/Negative/Neutral - TextBlob', 'Happy/Sad/Angry/Fear/Surprise - text2emotion'))
st.text("")
if st.button('Predict'):
if(userText!="" and type!=None):
st.text("")
st.components.v1.html("""
<h3 style="color: #0284c7; font-family: Source Sans Pro, sans-serif; font-size: 28px; margin-bottom: 10px; margin-top: 50px;">Result</h3>
""", height=100)
getSentiments(userText, type)