Spaces:
Runtime error
Runtime error
Commit
·
9ee4d23
1
Parent(s):
c6fea60
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import spacy
|
| 3 |
+
from spacytextblob.spacytextblob import SpacyTextBlob
|
| 4 |
+
|
| 5 |
+
st.set_page_config(layout='wide', initial_sidebar_state='expanded')
|
| 6 |
+
st.title('Text Analysis using Spacy Textblob')
|
| 7 |
+
st.markdown('Type a sentence in the below text box and choose the desired option in the adjacent menu.')
|
| 8 |
+
side = st.sidebar.selectbox("Select an option below", ("Sentiment", "Subjectivity", "NER"))
|
| 9 |
+
Text = st.text_input("Enter the sentence")
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
@st.cache
|
| 13 |
+
def sentiment(text):
|
| 14 |
+
nlp = spacy.load('en_core_web_sm')
|
| 15 |
+
nlp.add_pipe('spacytextblob')
|
| 16 |
+
doc = nlp(text)
|
| 17 |
+
if doc._.polarity<0:
|
| 18 |
+
return "Negative"
|
| 19 |
+
elif doc._.polarity==0:
|
| 20 |
+
return "Neutral"
|
| 21 |
+
else:
|
| 22 |
+
return "Positive"
|
| 23 |
+
|
| 24 |
+
@st.cache
|
| 25 |
+
def subjectivity(text):
|
| 26 |
+
nlp = spacy.load('en_core_web_sm')
|
| 27 |
+
nlp.add_pipe('spacytextblob')
|
| 28 |
+
doc = nlp(text)
|
| 29 |
+
if doc._.subjectivity > 0.5:
|
| 30 |
+
return "Highly Opinionated sentence"
|
| 31 |
+
elif doc._.subjectivity < 0.5:
|
| 32 |
+
return "Less Opinionated sentence"
|
| 33 |
+
else:
|
| 34 |
+
return "Neutral sentence"
|
| 35 |
+
|
| 36 |
+
@st.cache
|
| 37 |
+
def ner(sentence):
|
| 38 |
+
nlp = spacy.load("en_core_web_sm")
|
| 39 |
+
doc = nlp(sentence)
|
| 40 |
+
ents = [(e.text, e.label_) for e in doc.ents]
|
| 41 |
+
return ents
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def run():
|
| 46 |
+
|
| 47 |
+
if side == "Sentiment":
|
| 48 |
+
st.write(sentiment(Text))
|
| 49 |
+
if side == "Subjectivity":
|
| 50 |
+
st.write(subjectivity(Text))
|
| 51 |
+
|
| 52 |
+
if side == "NER":
|
| 53 |
+
st.write(ner(Text))
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
if __name__ == '__main__':
|
| 59 |
+
run()
|