File size: 1,804 Bytes
1ea9964
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import spacy
from collections import Counter

# Load Spacy model
nlp = spacy.load("en_core_web_sm")

# App title
st.title("NLP Blog with Sidebar and Buttons")

# Sidebar options
st.sidebar.title("Select NLP Task")
task = st.sidebar.selectbox("Choose a task:", ["Sentiment Analysis", "Keyword Extraction", "Named Entity Recognition (NER)"])

# Input text area
st.write("Enter text for analysis below:")
user_text = st.text_area("Input your text here:", height=200)

# Buttons
if st.button("Analyze"):
    if user_text.strip():
        if task == "Sentiment Analysis":
            # Perform sentiment analysis
            blob = TextBlob(user_text)
            sentiment = blob.sentiment
            st.subheader("Sentiment Analysis Result")
            st.write(f"Polarity: {sentiment.polarity:.2f}")
            st.write(f"Subjectivity: {sentiment.subjectivity:.2f}")
            
        elif task == "Keyword Extraction":
            # Extract keywords
            doc = nlp(user_text)
            keywords = [token.text for token in doc if token.is_alpha and not token.is_stop]
            most_common_keywords = Counter(keywords).most_common(10)
            st.subheader("Keyword Extraction Result")
            st.write("Most Common Keywords:")
            st.write(most_common_keywords)

        elif task == "Named Entity Recognition (NER)":
            # Perform Named Entity Recognition
            doc = nlp(user_text)
            st.subheader("Named Entity Recognition Result")
            for ent in doc.ents:
                st.write(f"Entity: {ent.text}, Label: {ent.label_}")
    else:
        st.error("Please enter some text for analysis.")

# Footer
st.sidebar.write("---")
st.sidebar.write("Developed with ❤️ using Streamlit.")