-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
66 lines (59 loc) · 2.2 KB
/
app.py
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
60
61
62
63
64
65
66
import streamlit as st
#nlp packages
import spacy
from textblob import TextBlob
#tokenization and lemmatization
def token_lemmatization(raw_text):
nlp=spacy.load('en_core_web_sm')
doc=nlp(raw_text)
result = [('Token: {},\n Lemma: {}'.format(token.text,token.lemma_))for token in doc]
return result
#POS TAGGING
def pos_text(raw_text):
nlp=spacy.load('en_core_web_sm')
doc=nlp(raw_text)
result=[('Token: {},\n POS Tag: {}'.format(token.text,token.pos_))for token in doc]
return result
#Named entity
def named_entity_extraction(raw_text):
nlp = spacy.load('en_core_web_sm')
doc = nlp(raw_text)
entities = [(entity.text,entity.label_)for entity in doc.ents]
result = ['Entities: {}'.format(entities)]
return result
def main():
st.title("NLP With Streamlit")
st.markdown("""
This is a Natural Language Processing(NLP) based App for basic NLP task:
Tokenization,Lemmatization,Parts of Speech(POS) Tagging, Named Entity Extraction(NER)
and Sentiment Analysis
""")
given_text = st.text_area("Enter Text","Type Here ..")
process = st.selectbox("Select precess: ",[' ','Tokens and Lemma','POS Tag','Named Entities','Sentiment Analysis'])
if st.button("Analyse"):
#Tokenization and lemmatization
if process=='Tokens and Lemma':
st.subheader("Tokenized text")
nlp_results=token_lemmatization(given_text)
st.json(nlp_results)
#POS Tag
elif process=='POS Tag':
st.subheader("POS Tags")
pos_result = pos_text(given_text)
st.json(pos_result)
#named entities
elif process=='Named Entities':
st.subheader("Extracted entities")
entity_result = named_entity_extraction(given_text)
st.json(entity_result)
#sentiment analysis
elif process=='Sentiment Analysis':
st.subheader("Analysed text")
blob = TextBlob(given_text)
result_sentiment = blob.sentiment
st.success(result_sentiment)
#in case no process is selected
else:
st.warning("Select process")
if __name__ == '__main__':
main()