File size: 1,670 Bytes
40ab78a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9298029
 
 
 
 
 
40ab78a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7bc0e85
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
import streamlit as st
from textblob import TextBlob
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
from flair.models import TextClassifier
from flair.data import Sentence
import matplotlib.pyplot as plt

# Function to perform sentiment analysis using TextBlob model
def textblob_sentiment(text):
    blob = TextBlob(text)
    return blob.sentiment.polarity

# Function to perform sentiment analysis using VADER model
def vader_sentiment(text):
    analyzer = SentimentIntensityAnalyzer()
    scores = analyzer.polarity_scores(text)
    return scores['compound']

# Function to perform sentiment analysis using Flair model
def flair_sentiment(text):
    classifier = TextClassifier.load('en-sentiment')
    sentence = Sentence(text)
    classifier.predict(sentence)
    if len(sentence.labels) > 0:
        if sentence.labels[0].value == 'POSITIVE':
            return 1.0
        elif sentence.labels[0].value == 'NEGATIVE':
            return -1.0
    return 0.0

# Set up the Streamlit app
st.title('Sentiment Analysis App')

# Get user input
text = st.text_input('Enter text to analyze')

# Perform sentiment analysis using each model
textblob_score = textblob_sentiment(text)
vader_score = vader_sentiment(text)
flair_score = flair_sentiment(text)

# Display the sentiment scores
st.write('TextBlob score:', textblob_score)
st.write('VADER score:', vader_score)
st.write('Flair score:', flair_score)

# Create a graph of the sentiment scores
fig, ax = plt.subplots()
ax.bar(['TextBlob', 'VADER', 'Flair'], [textblob_score, vader_score, flair_score])
ax.axhline(y=0, color='gray', linestyle='--')
ax.set_title('Sentiment Scores')
st.pyplot(fig)