Update app.py
Browse files
app.py
CHANGED
|
@@ -1,469 +1,211 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
|
| 3 |
-
# Sidebar
|
| 4 |
sidebar = st.sidebar
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
if 'selected_page' not in st.session_state:
|
| 14 |
-
st.session_state.selected_page =
|
| 15 |
-
|
| 16 |
-
# Update the selected page if the user selects a different option
|
| 17 |
-
if sidebar_option != st.session_state.selected_page:
|
| 18 |
-
st.session_state.selected_page = sidebar_option
|
| 19 |
-
|
| 20 |
-
# Dynamically update the title based on the selected option
|
| 21 |
-
def set_title(title, color="black"):
|
| 22 |
-
st.markdown(f"<h1 style='text-align: center; color: {color}; font-size: 36px;'>{title}</h1>", unsafe_allow_html=True)
|
| 23 |
-
|
| 24 |
-
# Function to add a paragraph with consistent formatting
|
| 25 |
-
def add_paragraph(content):
|
| 26 |
-
st.markdown(f"<p style='font-size: 16px; line-height: 1.6;'>{content}</p>", unsafe_allow_html=True)
|
| 27 |
-
|
| 28 |
-
# Function for adding a bullet list
|
| 29 |
-
def add_bullet_list(items):
|
| 30 |
-
for item in items:
|
| 31 |
-
st.markdown(f"- {item}", unsafe_allow_html=True)
|
| 32 |
-
|
| 33 |
-
# Content for "What is NLP?"
|
| 34 |
-
if st.session_state.selected_page == 'π§ What is NLP?':
|
| 35 |
-
set_title('Introduction to Natural Language Processing (NLP)', color="darkblue")
|
| 36 |
-
|
| 37 |
-
st.markdown("<h2 style='text-align: center; color: darkblue;'>π What is NLP?</h2>", unsafe_allow_html=True)
|
| 38 |
-
|
| 39 |
-
add_paragraph("""
|
| 40 |
-
Natural Language Processing (NLP) is a branch of Artificial Intelligence (AI) that focuses on enabling computers to understand, interpret, and generate human language in a way that is meaningful. NLP allows machines to process and analyze large amounts of unstructured natural language data, including text and speech.
|
| 41 |
-
""")
|
| 42 |
-
|
| 43 |
-
st.markdown("<h3 style='text-align: center; color: darkgreen;'>Key Concepts of NLP</h3>", unsafe_allow_html=True)
|
| 44 |
-
|
| 45 |
-
add_bullet_list([
|
| 46 |
-
"π **Syntax**: The arrangement of words in a sentence to ensure proper structure.",
|
| 47 |
-
"π¬ **Semantics**: The study of meaning in words and sentences.",
|
| 48 |
-
"π **Pragmatics**: How context influences the interpretation of language.",
|
| 49 |
-
"π **Discourse**: The way that meaning is influenced by the flow of conversation or written text."
|
| 50 |
-
])
|
| 51 |
-
|
| 52 |
-
st.markdown("<h3 style='text-align: center; color: darkgreen;'>Applications of NLP</h3>", unsafe_allow_html=True)
|
| 53 |
-
|
| 54 |
-
add_bullet_list([
|
| 55 |
-
"π **Machine Translation**: Translating text between languages, such as Google Translate.",
|
| 56 |
-
"π£οΈ **Speech Recognition**: Converting spoken language into text, as seen in Siri or Google Assistant.",
|
| 57 |
-
"π **Sentiment Analysis**: Analyzing text to determine its sentiment, such as positive or negative reviews.",
|
| 58 |
-
"βοΈ **Text Summarization**: Creating concise summaries from longer texts, such as news articles."
|
| 59 |
-
])
|
| 60 |
-
|
| 61 |
-
add_paragraph("""
|
| 62 |
-
NLP is used in a wide variety of industries like healthcare, finance, and customer service to automate tasks, analyze data, and improve customer experiences. By enabling computers to process human language, NLP opens up new possibilities for innovation and efficiency.
|
| 63 |
-
""")
|
| 64 |
-
|
| 65 |
-
# Content for NLP Lifecycle
|
| 66 |
-
elif st.session_state.selected_page == "π NLP Lifecycle":
|
| 67 |
-
lifecycle_option = sidebar.radio("Select NLP Lifecycle Step:", [
|
| 68 |
-
"π Overview of the NLP Life Cycle",
|
| 69 |
-
"π― Problem Definition",
|
| 70 |
-
"π Data Collection",
|
| 71 |
-
"π Simple EDA",
|
| 72 |
-
"π§Ή Data Preprocessing",
|
| 73 |
-
"π Feature Engineering",
|
| 74 |
-
"ποΈββοΈ Model Training",
|
| 75 |
-
"π
Evaluation",
|
| 76 |
-
"π Deployment"
|
| 77 |
-
])
|
| 78 |
-
|
| 79 |
-
if lifecycle_option == "π Overview of the NLP Life Cycle":
|
| 80 |
-
set_title('NLP Life Cycle Overview', color="darkblue")
|
| 81 |
-
|
| 82 |
-
add_paragraph("""
|
| 83 |
-
The NLP lifecycle is a comprehensive process that involves several key stages, from defining the problem to deploying a model and maintaining it. Each step plays a crucial role in transforming raw text data into actionable insights or automated systems. The stages can be broken down as follows:
|
| 84 |
-
""")
|
| 85 |
-
|
| 86 |
-
add_bullet_list([
|
| 87 |
-
"1. **Problem Definition**: Identifying the business problem that can be solved using NLP.",
|
| 88 |
-
"2. **Data Collection**: Gathering relevant textual data that will be used to train models.",
|
| 89 |
-
"3. **Exploratory Data Analysis (EDA)**: Understanding the data's structure, identifying patterns, and spotting potential issues.",
|
| 90 |
-
"4. **Data Preprocessing**: Cleaning and formatting data to prepare it for analysis, such as removing noise or handling missing values.",
|
| 91 |
-
"5. **Feature Engineering**: Creating new features or selecting important attributes to improve model performance.",
|
| 92 |
-
"6. **Model Training**: Selecting the appropriate algorithms and training models using the prepared data.",
|
| 93 |
-
"7. **Evaluation**: Assessing the model's accuracy and performance using various metrics like precision, recall, and F1-score.",
|
| 94 |
-
"8. **Deployment**: Implementing the model into a production environment, ready for real-world use.",
|
| 95 |
-
"9. **Monitoring and Maintenance**: Continuously checking model performance and making updates to keep it effective."
|
| 96 |
-
])
|
| 97 |
-
|
| 98 |
-
add_paragraph("""
|
| 99 |
-
The NLP lifecycle is iterative; as new data becomes available or requirements change, steps like model retraining and maintenance are necessary. This ensures that NLP systems remain relevant and effective over time.
|
| 100 |
-
""")
|
| 101 |
-
|
| 102 |
-
# More specific steps in NLP lifecycle would go here based on lifecycle_option selection...
|
| 103 |
-
|
| 104 |
-
# Content for NLP Techniques
|
| 105 |
-
elif st.session_state.selected_page == 'βοΈ NLP Techniques':
|
| 106 |
-
set_title('Techniques in Natural Language Processing', color="darkblue")
|
| 107 |
-
|
| 108 |
-
st.markdown("<h2 style='text-align: center; color: darkblue;'>π§ Key NLP Techniques</h2>", unsafe_allow_html=True)
|
| 109 |
-
|
| 110 |
-
add_paragraph("""
|
| 111 |
-
NLP involves several techniques and algorithms to process and analyze text data effectively. Some of the key techniques include:
|
| 112 |
-
""")
|
| 113 |
-
|
| 114 |
-
add_bullet_list([
|
| 115 |
-
"π§ **Tokenization**: Breaking text into smaller units like words or sentences to make it easier to process.",
|
| 116 |
-
"π€ **Stemming and Lemmatization**: Reducing words to their base or root form to standardize text (e.g., 'running' to 'run').",
|
| 117 |
-
"π― **Named Entity Recognition (NER)**: Identifying entities like names, dates, or locations within text.",
|
| 118 |
-
"π‘ **Part-of-Speech Tagging**: Assigning a part of speech (e.g., noun, verb) to each word in a sentence.",
|
| 119 |
-
"π **Word Embeddings**: Mapping words into numerical vectors (e.g., Word2Vec, GloVe) to capture semantic meaning.",
|
| 120 |
-
"π **Topic Modeling**: Grouping a set of documents into topics based on common themes, using techniques like LDA (Latent Dirichlet Allocation)."
|
| 121 |
-
])
|
| 122 |
-
|
| 123 |
-
add_paragraph("""
|
| 124 |
-
These techniques are essential for tasks like machine translation, sentiment analysis, and chatbot development. NLP models that leverage these techniques can understand and generate human language with remarkable accuracy.
|
| 125 |
-
""")
|
| 126 |
-
|
| 127 |
-
# Content for NLP Lifecycle
|
| 128 |
-
elif st.session_state.selected_page == "πNLP Lifecycle":
|
| 129 |
-
lifecycle_option = sidebar.radio("Select NLP Lifecycle Step:", [
|
| 130 |
-
"π Overview of the NLP Life Cycle",
|
| 131 |
-
"π― Problem Definition",
|
| 132 |
-
"π Data Collection",
|
| 133 |
-
"π Simple EDA",
|
| 134 |
-
"π§Ή Data Preprocessing",
|
| 135 |
-
"π Feature Engineering",
|
| 136 |
-
"ποΈββοΈ Model Training",
|
| 137 |
-
"π
Model Evaluation",
|
| 138 |
-
"π Deployment"
|
| 139 |
-
])
|
| 140 |
-
|
| 141 |
-
if lifecycle_option == "π Overview of the NLP Life Cycle":
|
| 142 |
-
st.write("""
|
| 143 |
-
#### Overview of the NLP Life Cycle:
|
| 144 |
-
The NLP life cycle outlines a structured approach to building, deploying, and maintaining systems that handle human language. It turns unstructured text into actionable insights, making the process flexible and adaptive to real-world changes.
|
| 145 |
-
|
| 146 |
-
- **The Process Flow**:
|
| 147 |
-
1. Problem identification and text data collection.
|
| 148 |
-
2. Data cleaning and preprocessing.
|
| 149 |
-
3. Model building and testing.
|
| 150 |
-
4. Continuous monitoring and updates to ensure optimal performance.
|
| 151 |
-
|
| 152 |
-
- **Key Features**:
|
| 153 |
-
- Flexible and adaptive: The NLP life cycle evolves as language, trends, and data change.
|
| 154 |
-
- Cross-disciplinary: Combines expertise from linguistics, computer science, and data analysis.
|
| 155 |
-
- Practical and scalable: Solutions are designed to be implemented in real-world applications.
|
| 156 |
-
|
| 157 |
-
#### Major Stages in the NLP Life Cycle:
|
| 158 |
-
1. π§ Problem Definition
|
| 159 |
-
2. π Data Collection
|
| 160 |
-
3. π Simple EDA (Exploratory Data Analysis)
|
| 161 |
-
4. π§Ή Data Preprocessing
|
| 162 |
-
5. π Feature Engineering
|
| 163 |
-
6. ποΈββοΈ Model Training
|
| 164 |
-
7. π
Model Evaluation
|
| 165 |
-
8. βοΈ Model Tuning
|
| 166 |
-
9. π Deployment
|
| 167 |
-
10. π οΈ Monitoring and Maintenance
|
| 168 |
-
""")
|
| 169 |
-
|
| 170 |
-
elif lifecycle_option == "π― Problem Definition":
|
| 171 |
-
st.write("""
|
| 172 |
-
#### π§ 1. Problem Definition:
|
| 173 |
-
The first step in the NLP lifecycle is defining the problem. Understanding the objective is key to determining how NLP can be applied.
|
| 174 |
-
|
| 175 |
-
- **Key Questions to Define the Problem**:
|
| 176 |
-
- π― What is the goal of this analysis (e.g., sentiment classification, topic modeling)?
|
| 177 |
-
- π What type of data are we dealing with (e.g., reviews, articles, social media posts)?
|
| 178 |
-
- π What is the desired outcome (e.g., sentiment score, summary, or label)?
|
| 179 |
-
|
| 180 |
-
**Example Problem**: Classifying customer reviews as positive or negative.
|
| 181 |
-
""")
|
| 182 |
-
|
| 183 |
-
elif lifecycle_option == "π Data Collection":
|
| 184 |
-
st.write("""
|
| 185 |
-
#### π 2. Data Collection:
|
| 186 |
-
Data collection is the foundation of the NLP process. It involves gathering relevant textual data from multiple sources.
|
| 187 |
-
|
| 188 |
-
- **Common Data Collection Methods**:
|
| 189 |
-
- π Public datasets (e.g., Kaggle, government websites).
|
| 190 |
-
- π APIs for data retrieval (e.g., Twitter API for tweets).
|
| 191 |
-
- πΈοΈ Web scraping (e.g., using BeautifulSoup or Scrapy).
|
| 192 |
-
- β Manual data entry in some cases.
|
| 193 |
-
|
| 194 |
-
**Example**: Scraping product reviews from e-commerce websites to analyze customer sentiment.
|
| 195 |
-
|
| 196 |
-
#### Extracting Data from Files:
|
| 197 |
-
Once data is collected, itβs often in formats like CSV, JSON, or Excel. Tools like **Pandas** in Python can be used to extract and structure this data for analysis.
|
| 198 |
-
|
| 199 |
-
**Steps**:
|
| 200 |
-
- Identify the file format (e.g., `.csv`, `.json`).
|
| 201 |
-
- Use functions like `pd.read_csv()` to load the data into a Pandas DataFrame.
|
| 202 |
-
- Clean and verify the data using methods like `df.head()` or `df.info()` to inspect the first few records and data types.
|
| 203 |
-
|
| 204 |
-
**Code Example**:
|
| 205 |
-
```python
|
| 206 |
-
import pandas as pd
|
| 207 |
-
|
| 208 |
-
# Extracting Data from a CSV File
|
| 209 |
-
df = pd.read_csv('data.csv')
|
| 210 |
-
print(df.head())
|
| 211 |
-
```
|
| 212 |
-
""")
|
| 213 |
|
| 214 |
-
elif lifecycle_option == "π Simple EDA":
|
| 215 |
-
st.write("""
|
| 216 |
-
#### π 3. Simple EDA (Exploratory Data Analysis):
|
| 217 |
-
Simple EDA provides an initial understanding of the data. It helps identify patterns, outliers, and potential issues that may impact the analysis.
|
| 218 |
-
|
| 219 |
-
- **Key Tasks in EDA**:
|
| 220 |
-
- Check the data balance: Is the data evenly distributed or skewed?
|
| 221 |
-
- Visualize distributions: Use histograms and boxplots.
|
| 222 |
-
- Check for missing data: Identify any gaps in the data.
|
| 223 |
-
- Outlier detection: Spot extreme values and decide whether to keep or remove them.
|
| 224 |
-
|
| 225 |
-
**Example**: In a classification dataset, check for a 70:30 imbalance between positive and negative classes.
|
| 226 |
-
""")
|
| 227 |
-
|
| 228 |
-
elif lifecycle_option == "π§Ή Data Preprocessing":
|
| 229 |
-
st.write("""
|
| 230 |
-
#### π§Ή 4. Text Preprocessing:
|
| 231 |
-
Text preprocessing is essential for converting raw text into a format suitable for analysis or model training.
|
| 232 |
-
|
| 233 |
-
**Steps in Text Preprocessing**:
|
| 234 |
-
- **Tokenization**: Splitting text into smaller pieces (words or sentences).
|
| 235 |
-
- **Removing Stopwords**: Eliminate common, less meaningful words (e.g., 'the', 'and').
|
| 236 |
-
- **Stemming and Lemmatization**: Reduce words to their root forms (e.g., 'running' to 'run').
|
| 237 |
-
- **Removing Special Characters**: Strip out unnecessary symbols, emojis, and URLs.
|
| 238 |
-
|
| 239 |
-
**Code Example**:
|
| 240 |
-
```python
|
| 241 |
-
import re
|
| 242 |
-
|
| 243 |
-
# Sample text
|
| 244 |
-
text = "The quick brown fox π¦ jumps over the lazy dog!"
|
| 245 |
-
|
| 246 |
-
# Remove special characters
|
| 247 |
-
clean_text = re.sub(r'[^a-zA-Z\s]', '', text)
|
| 248 |
-
print(clean_text)
|
| 249 |
-
```
|
| 250 |
-
**Output**: "The quick brown fox jumps over the lazy dog"
|
| 251 |
-
""")
|
| 252 |
-
|
| 253 |
-
elif lifecycle_option == "π Feature Engineering":
|
| 254 |
-
st.write("""
|
| 255 |
-
#### π 5. Feature Engineering:
|
| 256 |
-
After preprocessing, the text must be transformed into a numerical representation for machine learning models.
|
| 257 |
-
|
| 258 |
-
**Common Feature Engineering Techniques**:
|
| 259 |
-
- **Bag of Words (BoW)**: Counts the frequency of words in the text.
|
| 260 |
-
- **TF-IDF**: Measures word importance based on frequency and rarity.
|
| 261 |
-
- **Word Embeddings**: Transforms words into dense vectors (e.g., Word2Vec, GloVe).
|
| 262 |
-
|
| 263 |
-
**Example**: Converting the sentence "I love NLP" into a numerical vector using BoW.
|
| 264 |
-
""")
|
| 265 |
-
|
| 266 |
-
elif lifecycle_option == "ποΈββοΈ Model Training":
|
| 267 |
-
st.write("""
|
| 268 |
-
#### ποΈββοΈ 6. Model Training:
|
| 269 |
-
Once the data is preprocessed, we can train machine learning models on it. The choice of model depends on the NLP task (e.g., classification, sentiment analysis).
|
| 270 |
-
|
| 271 |
-
**Common Models**:
|
| 272 |
-
- **Text Classification**: Naive Bayes, SVM, or deep learning models.
|
| 273 |
-
- **Sentiment Analysis**: Logistic Regression, Naive Bayes, BERT.
|
| 274 |
-
|
| 275 |
-
**Example**: Training a Naive Bayes model to classify text into categories like "Sports", "Politics", etc.
|
| 276 |
-
""")
|
| 277 |
-
|
| 278 |
-
elif lifecycle_option == "π
Model Evaluation":
|
| 279 |
-
st.write("""
|
| 280 |
-
#### π
7. Model Evaluation:
|
| 281 |
-
Evaluating the performance of your model is crucial to ensure its reliability.
|
| 282 |
-
|
| 283 |
-
- **Metrics**: Accuracy, Precision, Recall, F1-Score.
|
| 284 |
-
- **Confusion Matrix**: Visual representation of true positives, false positives, etc.
|
| 285 |
-
|
| 286 |
-
**Example**: Evaluating a sentiment analysis model with accuracy and F1-score on test data.
|
| 287 |
-
""")
|
| 288 |
-
|
| 289 |
-
elif lifecycle_option == "π Deployment":
|
| 290 |
-
st.write("""
|
| 291 |
-
#### π 8. Deployment:
|
| 292 |
-
Once the model is trained and evaluated, it is deployed to production for real-time use.
|
| 293 |
-
|
| 294 |
-
**Key Steps**:
|
| 295 |
-
- **Integration**: Connecting the model with a live application (e.g., a chatbot).
|
| 296 |
-
- **Monitoring**: Regularly tracking the modelβs performance over time.
|
| 297 |
-
- **Updating**: Periodically retraining the model with new data.
|
| 298 |
-
|
| 299 |
-
**Example**: Deploying a sentiment analysis model via a REST API to evaluate customer reviews in real-time.
|
| 300 |
-
""")
|
| 301 |
-
|
| 302 |
-
# Content for NLP Techniques
|
| 303 |
-
elif st.session_state.selected_page == "βοΈNLP Techniques":
|
| 304 |
-
st.write("""
|
| 305 |
-
#### βοΈ Common NLP Techniques:
|
| 306 |
-
There are several techniques in NLP that enable computers to process language in meaningful ways.
|
| 307 |
-
|
| 308 |
-
**1. Tokenization**: Breaking text into individual words or phrases.
|
| 309 |
-
|
| 310 |
-
**2. Named Entity Recognition (NER)**: Identifying entities such as names, locations, dates, etc.
|
| 311 |
-
|
| 312 |
-
**3. Part-of-Speech (POS) Tagging**: Labeling words based on their parts of speech (e.g., noun, verb).
|
| 313 |
-
|
| 314 |
-
**4. Sentiment Analysis**: Determining the sentiment expressed in the text (positive, negative, or neutral).
|
| 315 |
-
|
| 316 |
-
**5. Word Embeddings**: Converting words into high-dimensional vectors to capture semantic meaning.
|
| 317 |
-
|
| 318 |
-
**6. Text Classification**: Assigning predefined labels to text (e.g., spam detection, topic categorization).
|
| 319 |
-
|
| 320 |
-
**7. Machine Translation**: Translating text from one language to another.
|
| 321 |
-
|
| 322 |
-
**8. Text Summarization**: Condensing long texts into shorter, meaningful summaries.
|
| 323 |
-
""")
|
| 324 |
# Content for "NLP Techniques"
|
| 325 |
-
|
| 326 |
technique_option = sidebar.radio("Select an NLP Technique:", [
|
| 327 |
-
"NLP Techniques Overview",
|
| 328 |
-
"Tokenization",
|
| 329 |
-
"Stop Words Removal",
|
| 330 |
-
"Lemmatization",
|
| 331 |
-
"Stemming",
|
| 332 |
-
"One-Hot Encoding",
|
| 333 |
-
"Bag of Words (BoW)",
|
| 334 |
-
"TF-IDF",
|
| 335 |
-
"Word Embeddings",
|
| 336 |
-
"Named Entity Recognition (NER)",
|
| 337 |
-
"Part-of-Speech (POS) Tagging",
|
| 338 |
-
"Sentiment Analysis"
|
| 339 |
-
])
|
| 340 |
-
|
| 341 |
-
if technique_option == "NLP Techniques Overview":
|
| 342 |
-
st.write("""
|
| 343 |
-
###
|
| 344 |
-
Natural Language Processing
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
|
|
|
| 360 |
|
| 361 |
- **Example**:
|
| 362 |
-
- Sentence: "
|
| 363 |
-
- Tokenized words: `["
|
| 364 |
""")
|
| 365 |
|
| 366 |
-
elif technique_option == "Stop Words Removal":
|
| 367 |
st.write("""
|
| 368 |
-
####
|
| 369 |
-
Stop words are common words
|
| 370 |
|
| 371 |
-
- **Example**:
|
| 372 |
""")
|
| 373 |
|
| 374 |
-
elif technique_option == "Lemmatization":
|
| 375 |
st.write("""
|
| 376 |
-
####
|
| 377 |
-
Lemmatization
|
| 378 |
|
| 379 |
- **Example**:
|
| 380 |
- "Better" β "Good"
|
| 381 |
- "Running" β "Run"
|
| 382 |
""")
|
| 383 |
|
| 384 |
-
elif technique_option == "Stemming":
|
| 385 |
st.write("""
|
| 386 |
-
####
|
| 387 |
-
Stemming is
|
| 388 |
|
| 389 |
- **Example**:
|
| 390 |
- "Running" β "Run"
|
| 391 |
- "Happiness" β "Happi"
|
| 392 |
""")
|
| 393 |
|
| 394 |
-
elif technique_option == "One-Hot Encoding":
|
| 395 |
st.write("""
|
| 396 |
-
####
|
| 397 |
-
One-Hot Encoding
|
| 398 |
|
| 399 |
- **Example**:
|
| 400 |
- Vocabulary: `["cat", "dog", "fish"]`
|
| 401 |
-
-
|
| 402 |
-
-
|
| 403 |
-
|
| 404 |
-
**Pros**: Simple and easy to implement.
|
| 405 |
-
**Cons**: Results in sparse vectors and can create high-dimensional spaces when the vocabulary is large.
|
| 406 |
""")
|
| 407 |
|
| 408 |
-
elif technique_option == "Bag of Words (BoW)":
|
| 409 |
st.write("""
|
| 410 |
-
####
|
| 411 |
-
The Bag of Words model represents text as
|
| 412 |
|
| 413 |
- **Example**:
|
| 414 |
- Text: "I love NLP"
|
| 415 |
- BoW: `{"I": 1, "love": 1, "NLP": 1}`
|
| 416 |
-
|
| 417 |
-
This model simply counts the frequency of each word in the text.
|
| 418 |
""")
|
| 419 |
|
| 420 |
-
elif technique_option == "TF-IDF":
|
| 421 |
st.write("""
|
| 422 |
-
####
|
| 423 |
-
TF-IDF
|
| 424 |
|
| 425 |
-
- **Example**:
|
| 426 |
-
-
|
| 427 |
""")
|
| 428 |
|
| 429 |
-
elif technique_option == "Word Embeddings":
|
| 430 |
st.write("""
|
| 431 |
-
####
|
| 432 |
-
Word embeddings
|
| 433 |
|
| 434 |
-
- **Example**:
|
|
|
|
| 435 |
""")
|
| 436 |
|
| 437 |
-
elif technique_option == "Named Entity Recognition (NER)":
|
| 438 |
st.write("""
|
| 439 |
-
####
|
| 440 |
-
NER
|
| 441 |
|
| 442 |
- **Example**:
|
| 443 |
-
- Text: "
|
| 444 |
-
-
|
| 445 |
""")
|
| 446 |
|
| 447 |
-
elif technique_option == "Part-of-Speech (POS) Tagging":
|
| 448 |
st.write("""
|
| 449 |
-
####
|
| 450 |
-
POS tagging assigns grammatical labels
|
| 451 |
|
| 452 |
- **Example**:
|
| 453 |
-
- Sentence: "The
|
| 454 |
-
- POS Tags: `[("The", "DT"), ("
|
| 455 |
""")
|
| 456 |
|
| 457 |
-
elif technique_option == "Sentiment Analysis":
|
| 458 |
st.write("""
|
| 459 |
-
####
|
| 460 |
-
Sentiment analysis
|
| 461 |
|
| 462 |
- **Example**:
|
| 463 |
-
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
|
| 3 |
+
# Custom Sidebar Styles
|
| 4 |
sidebar = st.sidebar
|
| 5 |
+
sidebar.title("π NLP Techniques")
|
| 6 |
+
|
| 7 |
+
# Apply custom CSS styles to the sidebar
|
| 8 |
+
st.markdown("""
|
| 9 |
+
<style>
|
| 10 |
+
.css-1d391kg {
|
| 11 |
+
background-color: #2C3E50;
|
| 12 |
+
color: white;
|
| 13 |
+
}
|
| 14 |
+
.sidebar .sidebar-content {
|
| 15 |
+
background-color: #34495E;
|
| 16 |
+
}
|
| 17 |
+
</style>
|
| 18 |
+
""", unsafe_allow_html=True)
|
| 19 |
+
|
| 20 |
+
# Sidebar radio button with emojis and unique color
|
| 21 |
+
st.session_state.selected_page = sidebar.radio(
|
| 22 |
+
"Select a page:",
|
| 23 |
+
["π§ NLP Techniques", "π Text Preprocessing", "π Model Evaluation"],
|
| 24 |
+
index=0
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
# Initialize session state for the selected page
|
| 28 |
if 'selected_page' not in st.session_state:
|
| 29 |
+
st.session_state.selected_page = "π§ NLP Techniques"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
# Content for "NLP Techniques"
|
| 32 |
+
if st.session_state.selected_page == "π§ NLP Techniques":
|
| 33 |
technique_option = sidebar.radio("Select an NLP Technique:", [
|
| 34 |
+
"π οΈ NLP Techniques Overview",
|
| 35 |
+
"βοΈ Tokenization",
|
| 36 |
+
"π« Stop Words Removal",
|
| 37 |
+
"π Lemmatization",
|
| 38 |
+
"πͺ Stemming",
|
| 39 |
+
"π― One-Hot Encoding",
|
| 40 |
+
"π¬ Bag of Words (BoW)",
|
| 41 |
+
"π TF-IDF",
|
| 42 |
+
"π Word Embeddings",
|
| 43 |
+
"π·οΈ Named Entity Recognition (NER)",
|
| 44 |
+
"π Part-of-Speech (POS) Tagging",
|
| 45 |
+
"π Sentiment Analysis"
|
| 46 |
+
], index=0)
|
| 47 |
+
|
| 48 |
+
if technique_option == "π οΈ NLP Techniques Overview":
|
| 49 |
+
st.write("""
|
| 50 |
+
### π§ Key NLP Techniques
|
| 51 |
+
NLP (Natural Language Processing) involves a variety of techniques for processing and analyzing text data.
|
| 52 |
+
These techniques help in making sense of the text and extracting valuable insights.
|
| 53 |
+
|
| 54 |
+
1. **Tokenization**: Breaking down text into smaller units (words or sentences).
|
| 55 |
+
2. **Part-of-Speech (POS) Tagging**: Identifying the grammatical role of words.
|
| 56 |
+
3. **Named Entity Recognition (NER)**: Identifying named entities (e.g., persons, locations).
|
| 57 |
+
4. **Dependency Parsing**: Analyzing sentence structure.
|
| 58 |
+
5. **Sentiment Analysis**: Analyzing the sentiment of the text (positive, negative, neutral).
|
| 59 |
+
6. **Word Embeddings**: Representing words as vectors that capture their meanings.
|
| 60 |
+
|
| 61 |
+
**Example**: Sentiment analysis can help identify whether a customer review is positive, negative, or neutral based on the words used.
|
| 62 |
+
""")
|
| 63 |
+
|
| 64 |
+
elif technique_option == "βοΈ Tokenization":
|
| 65 |
+
st.write("""
|
| 66 |
+
#### βοΈ Tokenization
|
| 67 |
+
Tokenization is the process of breaking text into smaller, meaningful pieces called tokens. These can be words, sentences, or subwords.
|
| 68 |
|
| 69 |
- **Example**:
|
| 70 |
+
- Sentence: "NLP is exciting!"
|
| 71 |
+
- Tokenized words: `["NLP", "is", "exciting"]`
|
| 72 |
""")
|
| 73 |
|
| 74 |
+
elif technique_option == "π« Stop Words Removal":
|
| 75 |
st.write("""
|
| 76 |
+
#### π« Stop Words Removal
|
| 77 |
+
Stop words are common words that don't add significant meaning to text analysis. Removing them helps reduce noise and dimensionality.
|
| 78 |
|
| 79 |
+
- **Example**: The sentence "NLP is amazing!" could be reduced to `["NLP", "amazing"]` after removing the word "is".
|
| 80 |
""")
|
| 81 |
|
| 82 |
+
elif technique_option == "π Lemmatization":
|
| 83 |
st.write("""
|
| 84 |
+
#### π Lemmatization
|
| 85 |
+
Lemmatization reduces words to their base or dictionary form while considering the context. Unlike stemming, lemmatization ensures the root word is valid.
|
| 86 |
|
| 87 |
- **Example**:
|
| 88 |
- "Better" β "Good"
|
| 89 |
- "Running" β "Run"
|
| 90 |
""")
|
| 91 |
|
| 92 |
+
elif technique_option == "πͺ Stemming":
|
| 93 |
st.write("""
|
| 94 |
+
#### πͺ Stemming
|
| 95 |
+
Stemming is the process of trimming prefixes and suffixes from words to obtain their root form, which might not always be a valid word.
|
| 96 |
|
| 97 |
- **Example**:
|
| 98 |
- "Running" β "Run"
|
| 99 |
- "Happiness" β "Happi"
|
| 100 |
""")
|
| 101 |
|
| 102 |
+
elif technique_option == "π― One-Hot Encoding":
|
| 103 |
st.write("""
|
| 104 |
+
#### π― One-Hot Encoding
|
| 105 |
+
One-Hot Encoding converts words into binary vectors. Each word in the vocabulary is assigned a unique vector where only one element is 1, and all others are 0.
|
| 106 |
|
| 107 |
- **Example**:
|
| 108 |
- Vocabulary: `["cat", "dog", "fish"]`
|
| 109 |
+
- "cat": `[1, 0, 0]`
|
| 110 |
+
- "dog": `[0, 1, 0]`
|
|
|
|
|
|
|
|
|
|
| 111 |
""")
|
| 112 |
|
| 113 |
+
elif technique_option == "π¬ Bag of Words (BoW)":
|
| 114 |
st.write("""
|
| 115 |
+
#### π¬ Bag of Words (BoW)
|
| 116 |
+
The Bag of Words model represents text as a collection of words, ignoring grammar and word order. It focuses on the frequency of words.
|
| 117 |
|
| 118 |
- **Example**:
|
| 119 |
- Text: "I love NLP"
|
| 120 |
- BoW: `{"I": 1, "love": 1, "NLP": 1}`
|
|
|
|
|
|
|
| 121 |
""")
|
| 122 |
|
| 123 |
+
elif technique_option == "π TF-IDF":
|
| 124 |
st.write("""
|
| 125 |
+
#### π TF-IDF (Term Frequency-Inverse Document Frequency)
|
| 126 |
+
TF-IDF assigns weights to words based on their frequency in a document relative to all documents, emphasizing rare but meaningful words.
|
| 127 |
|
| 128 |
+
- **Example**:
|
| 129 |
+
- "data" in a data science article has a higher TF-IDF score compared to a general article.
|
| 130 |
""")
|
| 131 |
|
| 132 |
+
elif technique_option == "π Word Embeddings":
|
| 133 |
st.write("""
|
| 134 |
+
#### π Word Embeddings
|
| 135 |
+
Word embeddings map words to dense vectors, capturing semantic relationships. Similar words have similar vector representations.
|
| 136 |
|
| 137 |
+
- **Example**:
|
| 138 |
+
- "King" and "Queen" are closer in vector space due to their semantic relationship.
|
| 139 |
""")
|
| 140 |
|
| 141 |
+
elif technique_option == "π·οΈ Named Entity Recognition (NER)":
|
| 142 |
st.write("""
|
| 143 |
+
#### π·οΈ Named Entity Recognition (NER)
|
| 144 |
+
NER identifies entities such as people, organizations, dates, and locations in text, aiding in information extraction.
|
| 145 |
|
| 146 |
- **Example**:
|
| 147 |
+
- Text: "Elon Musk is the CEO of Tesla."
|
| 148 |
+
- Entities: `["Elon Musk" (Person), "Tesla" (Organization)]`
|
| 149 |
""")
|
| 150 |
|
| 151 |
+
elif technique_option == "π Part-of-Speech (POS) Tagging":
|
| 152 |
st.write("""
|
| 153 |
+
#### π Part-of-Speech (POS) Tagging
|
| 154 |
+
POS tagging assigns grammatical labels (noun, verb, adjective, etc.) to words in a sentence, aiding in understanding the sentence structure.
|
| 155 |
|
| 156 |
- **Example**:
|
| 157 |
+
- Sentence: "The dog barks loudly."
|
| 158 |
+
- POS Tags: `[("The", "DT"), ("dog", "NN"), ("barks", "VBZ"), ("loudly", "RB")]`
|
| 159 |
""")
|
| 160 |
|
| 161 |
+
elif technique_option == "π Sentiment Analysis":
|
| 162 |
st.write("""
|
| 163 |
+
#### π Sentiment Analysis
|
| 164 |
+
Sentiment analysis determines the sentiment expressed in text, often classifying it as positive, negative, or neutral.
|
| 165 |
|
| 166 |
- **Example**:
|
| 167 |
+
- "I absolutely love this!" β Positive sentiment
|
| 168 |
+
""")
|
| 169 |
+
|
| 170 |
+
# Custom CSS for general styles
|
| 171 |
+
st.markdown("""
|
| 172 |
+
<style>
|
| 173 |
+
body {
|
| 174 |
+
background-color: #F0F8FF;
|
| 175 |
+
color: #2C3E50;
|
| 176 |
+
}
|
| 177 |
+
.css-1v0mbd0 {
|
| 178 |
+
color: #16A085;
|
| 179 |
+
font-family: 'Arial', sans-serif;
|
| 180 |
+
}
|
| 181 |
+
.stText {
|
| 182 |
+
color: #34495E;
|
| 183 |
+
}
|
| 184 |
+
.stMarkdown p {
|
| 185 |
+
font-size: 1.2em;
|
| 186 |
+
color: #34495E;
|
| 187 |
+
font-family: 'Arial', sans-serif;
|
| 188 |
+
}
|
| 189 |
+
.stRadio button {
|
| 190 |
+
background-color: #16A085;
|
| 191 |
+
color: white;
|
| 192 |
+
}
|
| 193 |
+
.css-1d391kg {
|
| 194 |
+
background-color: #2C3E50;
|
| 195 |
+
color: white;
|
| 196 |
+
}
|
| 197 |
+
.css-1v0mbd0 {
|
| 198 |
+
font-size: 1.5em;
|
| 199 |
+
color: #16A085;
|
| 200 |
+
}
|
| 201 |
+
</style>
|
| 202 |
+
""", unsafe_allow_html=True)
|
| 203 |
+
|
| 204 |
+
# Custom Sidebar Styles
|
| 205 |
+
sidebar = st.sidebar
|
| 206 |
+
sidebar.title("π NLP Techniques")
|
| 207 |
+
st.session_state.selected_page = sidebar.radio(
|
| 208 |
+
"Select a page:",
|
| 209 |
+
["π§ NLP Techniques", "π Text Preprocessing", "π Model Evaluation"],
|
| 210 |
+
index=0
|
| 211 |
+
)
|