NLP / app.py
santosh0223's picture
Update app.py
bab61e2 verified
Raw
History Blame Contribute Delete
4.18 kB
import streamlit as st
import re
# MUST BE FIRST STREAMLIT COMMAND
st.set_page_config(
page_title="Learn NLP from Scratch",
page_icon="🧠",
layout="wide"
)
# ---------------------------
# Helper Functions
# ---------------------------
def tokenize(text):
return re.findall(r"\b\w+\b", text.lower())
STOPWORDS = {
"is", "am", "are", "the", "a", "an", "and", "or", "in", "on", "at", "to", "of"
}
def remove_stopwords(tokens):
return [t for t in tokens if t not in STOPWORDS]
def simple_stem(word):
for suffix in ["ing", "ed", "s"]:
if word.endswith(suffix):
return word[:-len(suffix)]
return word
def stem_tokens(tokens):
return [simple_stem(t) for t in tokens]
def simple_pos_tag(tokens):
tagged = []
for word in tokens:
if word.endswith("ing"):
tagged.append((word, "VERB"))
else:
tagged.append((word, "WORD"))
return tagged
# ---------------------------
# App UI
# ---------------------------
st.title("Natural Language Processing (NLP) – From Basics to Practice")
st.write(
"This app explains the **NLP lifecycle**, **core techniques**, and provides a "
"**hands-on playground** to understand how text is processed by machines."
)
tabs = st.tabs([
"NLP Lifecycle",
"NLP Techniques",
"NLP Playground",
"NLP Roadmap"
])
# ---------------------------
# NLP Lifecycle
# ---------------------------
with tabs[0]:
st.header("NLP Lifecycle")
st.markdown("""
**1. Data Collection**
Collect text data such as reviews, emails, chats, or tweets.
**Example:** Amazon product reviews.
**2. Text Preprocessing**
Clean and prepare the text by removing noise.
**Example:**
`I Love NLP!!!` β†’ `i love nlp`
**3. Feature Extraction**
Convert text into numerical form.
**Example:** Bag of Words, TF-IDF.
**4. Model Training**
Train a machine learning or deep learning model.
**Example:** Spam detection model.
**5. Evaluation**
Measure model performance.
**Example:** Accuracy = 90%.
**6. Deployment**
Use the model in real applications.
**Example:** Chatbots, search engines.
""")
# ---------------------------
# NLP Techniques
# ---------------------------
with tabs[1]:
st.header("NLP Techniques")
st.markdown("""
**Tokenization**
Splits text into words.
*Example:* `I love NLP` β†’ `['i', 'love', 'nlp']`
*Use:* Text preprocessing
*Advantage:* Easy to analyze text
**Stopword Removal**
Removes common words like *is, the, and*.
*Use:* Reduces noise
*Advantage:* Improves performance
**Stemming**
Converts words to root form.
*Example:* `playing β†’ play`
*Use:* Search engines
*Advantage:* Smaller vocabulary
**POS Tagging**
Identifies grammatical role of words.
*Example:* `learning β†’ VERB`
*Use:* Grammar analysis
*Advantage:* Better sentence understanding
""")
# ---------------------------
# NLP Playground
# ---------------------------
with tabs[2]:
st.header("NLP Playground")
text = st.text_area(
"Enter text below",
"I am learning Natural Language Processing"
)
if st.button("Run NLP"):
tokens = tokenize(text)
no_stop = remove_stopwords(tokens)
stemmed = stem_tokens(no_stop)
pos = simple_pos_tag(tokens)
st.subheader("Tokens")
st.write(tokens)
st.subheader("After Stopword Removal")
st.write(no_stop)
st.subheader("After Stemming")
st.write(stemmed)
st.subheader("Simple POS Tagging")
st.write(pos)
# ---------------------------
# NLP Roadmap
# ---------------------------
with tabs[3]:
st.header("NLP Roadmap")
st.markdown("""
**Beginner**
- Text cleaning
- Tokenization
- TF-IDF
**Intermediate**
- Machine learning models
- Word embeddings
- POS & NER
**Advanced**
- LSTM
- Transformers
- BERT, GPT
**Applications**
- Chatbots
- Recommendation systems
- Voice assistants
""")