Katpeeler's picture
Update app.py
7b05af3
raw
history blame
1.87 kB
import streamlit as st
import spacy
from spacytextblob.spacytextblob import SpacyTextBlob
st.set_page_config(layout='wide', initial_sidebar_state='expanded')
st.title('Super cool NLP things for the whole family!')
st.markdown('Type some words in the text box below, and choose a processing option from the side menu.')
side = st.sidebar.selectbox("Select an option here", ("Sentiment", "Subjectivity", "NER"))
Text = st.text_input("Enter some words!")
@st.cache_data
def sentiment(text):
nlp = spacy.load('en_core_web_sm')
nlp.add_pipe('spacytextblob')
doc = nlp(text)
if len(Text) == 0:
return "This setting will try to figure out the tone of the provided text."
elif doc._.polarity<0:
return "The text seems negative"
elif doc._.polarity==0:
return "The text seems neutral"
else:
return "The text seems positive"
@st.cache_data
def subjectivity(text):
nlp = spacy.load('en_core_web_sm')
nlp.add_pipe('spacytextblob')
doc = nlp(text)
if len(Text) == 0:
return "This setting will try to figure out how opionionated the provided text is."
if doc._.subjectivity > 0.5:
return "This is a highly opinionated sentence"
elif doc._.subjectivity < 0.5:
return "This is a less opinionated sentence"
else:
return "This is a neutral sentence"
@st.cache_data
def ner(sentence):
nlp = spacy.load("en_core_web_sm")
doc = nlp(sentence)
ents = [(e.text, e.label_) for e in doc.ents]
if len(Text) == 0:
return "This setting identifies and extracts named entities."
else:
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()