-
Notifications
You must be signed in to change notification settings - Fork 0
/
SENTIMENTS.py
82 lines (66 loc) · 2.5 KB
/
SENTIMENTS.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
75
76
77
78
79
80
81
82
# -*- coding: utf-8 -*-
"""
Created on Sat Jun 3 21:29:07 2023
@author: SONE
"""
import streamlit as st
page_bg_img = f"""
<style>
[data-testid="stAppViewContainer"]{{
background-image: url("https://www.freecodecamp.org/news/content/images/2021/06/w-qjCHPZbeXCQ-unsplash.jpg");
}}
[data-testid="stHeader"]{{
background-image: url("https://www.freecodecamp.org/news/content/images/2021/06/w-qjCHPZbeXCQ-unsplash.jpg");
}}
</style>
"""
st.markdown(page_bg_img, unsafe_allow_html = True)
import nltk
import plotly.express as px
from nltk.sentiment import SentimentIntensityAnalyzer
from nltk.sentiment.vader import SentimentIntensityAnalyzer
from wordcloud import WordCloud
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
nltk.download('vader_lexicon')
def analyze_sentiment(text):
analyzer = SentimentIntensityAnalyzer()
scores = analyzer.polarity_scores(text)
if scores['compound'] > 0.05:
return 'Positive'
elif scores['compound'] < -0.05:
return 'Negative'
elif scores['compound'] >= -0.05 and scores['compound'] < 0.05:
return 'Neutral'
def generate_wordcloud(text):
wordcloud = WordCloud(width=800, height=400).generate(text)
sns.histplot(bins=10, kde=True)
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis('off')
st.pyplot()
def main():
st.title('Sentiment Analysis App')
# Input text box
text = st.text_area('Enter some text')
st.set_option('deprecation.showPyplotGlobalUse', False)
if st.button('Analyze'):
# Perform sentiment analysis
sentiment = analyze_sentiment(text)
# Display the result
st.write('Sentiment:', sentiment)
sentiments = SentimentIntensityAnalyzer()
st.write('Sentiment polarity score', sentiments.polarity_scores(text))
# Generate word cloud
generate_wordcloud(text)
# Plot sentiment distribution
data = {'Sentiment': ['Positive', 'Negative', 'Neutral'],
'Count': [0, 0, 0]}
data['Count'][['Positive', 'Negative', 'Neutral'].index(sentiment)] = 1
df = pd.DataFrame(data)
fig = px.bar(df, x='Sentiment', y='Count', color='Sentiment',
labels={'Sentiment': 'Sentiment', 'Count': 'Count'})
fig.update_layout(showlegend=False)
st.plotly_chart(fig)
if __name__ == '__main__':
main()