import streamlit as st
import spacy
import nltk
from nltk.stem import PorterStemmer

nltk.download('punkt')

try:
    nlp = spacy.load("en_core_web_sm")
except OSError:
    spacy.cli.download("en_core_web_sm")
    nlp = spacy.load("en_core_web_sm")

stemmer = PorterStemmer()

st.title("NLP with Streamlit and Spacy")

text = st.text_area("Enter text for NLP processing")
option = st.radio("Choose and option", ("Tokenization", "Stemming And Lemmatizing", "NER"))

if st.button("submit"):
    if not text.strip():
        st.warning("Please enter some text to process.")
    else:
        doc = nlp(text)
        if option == "Tokenization":
            for token in doc:
                st.write(token.text)
                
                
        elif option == "Stemming And Lemmatizing":
            st.subheader("Lemmatization")
            for token in doc:
                lem = token.lemma_
                st.write(token.text, "-->", lem)

            st.subheader("Stemming")
            for token in doc:
                stem = stemmer.stem(token.text)
                st.write(token.text, "-->", stem)

        elif option == "NER":
            for ent in doc.ents:
                st.write(ent.text, "-->", ent.label_)
