Spaces:
Sleeping
Sleeping
File size: 1,447 Bytes
d30dd77 |
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 |
import streamlit as st
import spacy
from spacytextblob.spacytextblob import SpacyTextBlob
st.set_page_config(layout='wide', initial_sidebar_state='expanded')
st.title('Text Analysis using Spacy Textblob')
st.markdown('Type a sentence in the below text box and choose the desired option in the adjacent menu.')
side = st.sidebar.selectbox("Select an option below", ("Sentiment", "Subjectivity", "NER"))
Text = st.text_input("Enter the sentence")
@st.cache
def sentiment(text):
nlp = spacy.load('en_core_web_sm')
nlp.add_pipe('spacytextblob')
doc = nlp(text)
if doc._.polarity<0:
return "Negative"
elif doc._.polarity==0:
return "Neutral"
else:
return "Positive"
@st.cache
def subjectivity(text):
nlp = spacy.load('en_core_web_sm')
nlp.add_pipe('spacytextblob')
doc = nlp(text)
if doc._.subjectivity > 0.5:
return "Highly Opinionated sentence"
elif doc._.subjectivity < 0.5:
return "Less Opinionated sentence"
else:
return "Neutral sentence"
@st.cache
def ner(sentence):
nlp = spacy.load("en_core_web_sm")
doc = nlp(sentence)
ents = [(e.text, e.label_) for e in doc.ents]
return ents
def run():
if side == "Sentiment":
st.write(sentiment(Text))
if side == "Subjectivity":
st.write(subjectivity(Text))
if side == "NER":
st.write(ner(Text))
if __name__ == '__main__':
run()
|