Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from gensim.models import Word2Vec
|
| 3 |
|
| 4 |
-
# Apply custom styles
|
| 5 |
st.markdown("""
|
| 6 |
<style>
|
| 7 |
.main-title { color: #FF5733; font-size: 40px; font-weight: bold; text-align: center; }
|
|
@@ -17,32 +17,29 @@ st.markdown('<p class="main-title">Introduction to NLP</p>', unsafe_allow_html=T
|
|
| 17 |
# Section: What is NLP?
|
| 18 |
st.markdown('<p class="section-title">What is NLP?</p>', unsafe_allow_html=True)
|
| 19 |
st.markdown("""
|
| 20 |
-
<p class="text">
|
| 21 |
Natural Language Processing (NLP) is a subfield of artificial intelligence that enables computers to process, understand, and generate human language.
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
β
Chatbots & Virtual Assistants (e.g., Siri, Alexa)
|
| 26 |
-
β
Sentiment Analysis (e.g., Product reviews, Social Media monitoring)
|
| 27 |
-
β
Machine Translation (e.g., Google Translate)
|
| 28 |
-
β
Text Summarization (e.g., News article summaries)
|
| 29 |
-
β
Speech Recognition (e.g., Voice commands)
|
| 30 |
-
"""
|
| 31 |
|
| 32 |
# Section: NLP Terminologies
|
| 33 |
st.markdown('<p class="section-title">NLP Terminologies</p>', unsafe_allow_html=True)
|
| 34 |
st.markdown("""
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
</p>
|
| 45 |
-
""", unsafe_allow_html=True)
|
| 46 |
|
| 47 |
# Section: Text Representation Methods
|
| 48 |
st.markdown('<p class="section-title">Text Representation Methods</p>', unsafe_allow_html=True)
|
|
@@ -57,85 +54,81 @@ selected_method = st.radio("Select a text representation method:", methods)
|
|
| 57 |
if selected_method == "Bag of Words":
|
| 58 |
st.markdown('<p class="sub-title">Bag of Words (BoW)</p>', unsafe_allow_html=True)
|
| 59 |
st.markdown("""
|
| 60 |
-
<p class="text">
|
| 61 |
**Definition**: Represents text as a collection of word counts, ignoring grammar and word order.
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
β
Sentiment analysis
|
| 66 |
-
β
Document classification
|
| 67 |
-
β
Information retrieval
|
| 68 |
|
| 69 |
-
|
| 70 |
-
β
Simple and easy to implement
|
| 71 |
-
β
Works well with traditional ML models
|
| 72 |
|
| 73 |
-
|
| 74 |
-
β Ignores word order and context
|
| 75 |
-
β High-dimensionality for large vocabularies
|
| 76 |
-
"""
|
| 77 |
|
| 78 |
elif selected_method == "TF-IDF":
|
| 79 |
st.markdown('<p class="sub-title">Term Frequency-Inverse Document Frequency (TF-IDF)</p>', unsafe_allow_html=True)
|
| 80 |
st.markdown("""
|
| 81 |
-
<p class="text">
|
| 82 |
**Definition**: Weighs words based on their frequency in a document and across all documents.
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
β
Information retrieval (e.g., search engines)
|
| 87 |
-
β
Text classification
|
| 88 |
-
β
Keyword extraction
|
| 89 |
|
| 90 |
-
|
| 91 |
-
β
Reduces the impact of common words
|
| 92 |
-
β
Highlights important words
|
| 93 |
|
| 94 |
-
|
| 95 |
-
β Still ignores word order
|
| 96 |
-
β Does not capture deep semantics
|
| 97 |
-
"""
|
| 98 |
|
| 99 |
elif selected_method == "One-Hot Encoding":
|
| 100 |
st.markdown('<p class="sub-title">One-Hot Encoding</p>', unsafe_allow_html=True)
|
| 101 |
st.markdown("""
|
| 102 |
-
<p class="text">
|
| 103 |
**Definition**: Represents words as binary vectors where each word has a unique position in a vocabulary.
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
β
Simple NLP tasks
|
| 108 |
-
β
Word-level feature engineering
|
| 109 |
|
| 110 |
-
|
| 111 |
-
β
Simple to understand
|
| 112 |
-
β
Works well with small vocabulary sizes
|
| 113 |
|
| 114 |
-
|
| 115 |
-
β Inefficient for large vocabularies
|
| 116 |
-
β No information on word meaning
|
| 117 |
-
"""
|
| 118 |
|
| 119 |
elif selected_method == "Word Embeddings (Word2Vec)":
|
| 120 |
st.markdown('<p class="sub-title">Word Embeddings (Word2Vec)</p>', unsafe_allow_html=True)
|
| 121 |
st.markdown("""
|
| 122 |
-
<p class="text">
|
| 123 |
**Definition**: Converts words into dense numerical vectors capturing semantic relationships.
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
β
Machine translation
|
| 128 |
-
β
Speech recognition
|
| 129 |
-
β
Sentiment analysis
|
| 130 |
|
| 131 |
-
|
| 132 |
-
β
Captures semantic relationships
|
| 133 |
-
β
Works well for deep learning models
|
| 134 |
|
| 135 |
-
|
| 136 |
-
β Requires large datasets to train
|
| 137 |
-
β Computationally expensive
|
| 138 |
-
"""
|
| 139 |
|
| 140 |
# Sample texts
|
| 141 |
texts = [
|
|
@@ -147,10 +140,10 @@ elif selected_method == "Word Embeddings (Word2Vec)":
|
|
| 147 |
word_vectors = model.wv
|
| 148 |
word = 'natural'
|
| 149 |
if word in word_vectors:
|
| 150 |
-
st.markdown(f'
|
| 151 |
st.write(word_vectors[word])
|
| 152 |
else:
|
| 153 |
-
st.markdown(f'
|
| 154 |
|
| 155 |
# Footer
|
| 156 |
st.markdown('<hr>', unsafe_allow_html=True)
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from gensim.models import Word2Vec
|
| 3 |
|
| 4 |
+
# Apply custom styles using Streamlit's markdown
|
| 5 |
st.markdown("""
|
| 6 |
<style>
|
| 7 |
.main-title { color: #FF5733; font-size: 40px; font-weight: bold; text-align: center; }
|
|
|
|
| 17 |
# Section: What is NLP?
|
| 18 |
st.markdown('<p class="section-title">What is NLP?</p>', unsafe_allow_html=True)
|
| 19 |
st.markdown("""
|
|
|
|
| 20 |
Natural Language Processing (NLP) is a subfield of artificial intelligence that enables computers to process, understand, and generate human language.
|
| 21 |
+
""")
|
| 22 |
+
st.markdown("""
|
| 23 |
+
**Applications of NLP:**
|
| 24 |
+
- β
Chatbots & Virtual Assistants (e.g., Siri, Alexa)
|
| 25 |
+
- β
Sentiment Analysis (e.g., Product reviews, Social Media monitoring)
|
| 26 |
+
- β
Machine Translation (e.g., Google Translate)
|
| 27 |
+
- β
Text Summarization (e.g., News article summaries)
|
| 28 |
+
- β
Speech Recognition (e.g., Voice commands)
|
| 29 |
+
""")
|
| 30 |
|
| 31 |
# Section: NLP Terminologies
|
| 32 |
st.markdown('<p class="section-title">NLP Terminologies</p>', unsafe_allow_html=True)
|
| 33 |
st.markdown("""
|
| 34 |
+
**Corpus**: A collection of text documents used for NLP tasks.
|
| 35 |
+
**Tokenization**: Splitting text into individual words or phrases.
|
| 36 |
+
**Stop Words**: Common words (e.g., "the", "is") that are often removed.
|
| 37 |
+
**Stemming**: Reducing words to their base form (e.g., "running" β "run").
|
| 38 |
+
**Lemmatization**: More advanced than stemming; converts words to their dictionary form.
|
| 39 |
+
**NER (Named Entity Recognition)**: Identifies entities like names, dates, and locations.
|
| 40 |
+
**Sentiment Analysis**: Determines the sentiment (positive, negative, neutral) of a text.
|
| 41 |
+
**n-grams**: Sequences of 'n' consecutive words (e.g., "New York" is a bi-gram).
|
| 42 |
+
""")
|
|
|
|
|
|
|
| 43 |
|
| 44 |
# Section: Text Representation Methods
|
| 45 |
st.markdown('<p class="section-title">Text Representation Methods</p>', unsafe_allow_html=True)
|
|
|
|
| 54 |
if selected_method == "Bag of Words":
|
| 55 |
st.markdown('<p class="sub-title">Bag of Words (BoW)</p>', unsafe_allow_html=True)
|
| 56 |
st.markdown("""
|
|
|
|
| 57 |
**Definition**: Represents text as a collection of word counts, ignoring grammar and word order.
|
| 58 |
+
""")
|
| 59 |
+
st.markdown("""
|
| 60 |
+
**Uses:**
|
| 61 |
+
- β
Sentiment analysis
|
| 62 |
+
- β
Document classification
|
| 63 |
+
- β
Information retrieval
|
| 64 |
|
| 65 |
+
**Advantages:**
|
| 66 |
+
- β
Simple and easy to implement
|
| 67 |
+
- β
Works well with traditional ML models
|
| 68 |
|
| 69 |
+
**Disadvantages:**
|
| 70 |
+
- β Ignores word order and context
|
| 71 |
+
- β High-dimensionality for large vocabularies
|
| 72 |
+
""")
|
| 73 |
|
| 74 |
elif selected_method == "TF-IDF":
|
| 75 |
st.markdown('<p class="sub-title">Term Frequency-Inverse Document Frequency (TF-IDF)</p>', unsafe_allow_html=True)
|
| 76 |
st.markdown("""
|
|
|
|
| 77 |
**Definition**: Weighs words based on their frequency in a document and across all documents.
|
| 78 |
+
""")
|
| 79 |
+
st.markdown("""
|
| 80 |
+
**Uses:**
|
| 81 |
+
- β
Information retrieval (e.g., search engines)
|
| 82 |
+
- β
Text classification
|
| 83 |
+
- β
Keyword extraction
|
| 84 |
|
| 85 |
+
**Advantages:**
|
| 86 |
+
- β
Reduces the impact of common words
|
| 87 |
+
- β
Highlights important words
|
| 88 |
|
| 89 |
+
**Disadvantages:**
|
| 90 |
+
- β Still ignores word order
|
| 91 |
+
- β Does not capture deep semantics
|
| 92 |
+
""")
|
| 93 |
|
| 94 |
elif selected_method == "One-Hot Encoding":
|
| 95 |
st.markdown('<p class="sub-title">One-Hot Encoding</p>', unsafe_allow_html=True)
|
| 96 |
st.markdown("""
|
|
|
|
| 97 |
**Definition**: Represents words as binary vectors where each word has a unique position in a vocabulary.
|
| 98 |
+
""")
|
| 99 |
+
st.markdown("""
|
| 100 |
+
**Uses:**
|
| 101 |
+
- β
Simple NLP tasks
|
| 102 |
+
- β
Word-level feature engineering
|
| 103 |
|
| 104 |
+
**Advantages:**
|
| 105 |
+
- β
Simple to understand
|
| 106 |
+
- β
Works well with small vocabulary sizes
|
| 107 |
|
| 108 |
+
**Disadvantages:**
|
| 109 |
+
- β Inefficient for large vocabularies
|
| 110 |
+
- β No information on word meaning
|
| 111 |
+
""")
|
| 112 |
|
| 113 |
elif selected_method == "Word Embeddings (Word2Vec)":
|
| 114 |
st.markdown('<p class="sub-title">Word Embeddings (Word2Vec)</p>', unsafe_allow_html=True)
|
| 115 |
st.markdown("""
|
|
|
|
| 116 |
**Definition**: Converts words into dense numerical vectors capturing semantic relationships.
|
| 117 |
+
""")
|
| 118 |
+
st.markdown("""
|
| 119 |
+
**Uses:**
|
| 120 |
+
- β
Machine translation
|
| 121 |
+
- β
Speech recognition
|
| 122 |
+
- β
Sentiment analysis
|
| 123 |
|
| 124 |
+
**Advantages:**
|
| 125 |
+
- β
Captures semantic relationships
|
| 126 |
+
- β
Works well for deep learning models
|
| 127 |
|
| 128 |
+
**Disadvantages:**
|
| 129 |
+
- β Requires large datasets to train
|
| 130 |
+
- β Computationally expensive
|
| 131 |
+
""")
|
| 132 |
|
| 133 |
# Sample texts
|
| 134 |
texts = [
|
|
|
|
| 140 |
word_vectors = model.wv
|
| 141 |
word = 'natural'
|
| 142 |
if word in word_vectors:
|
| 143 |
+
st.markdown(f'Word2Vec Representation of "{word}":')
|
| 144 |
st.write(word_vectors[word])
|
| 145 |
else:
|
| 146 |
+
st.markdown(f'Word "{word}" not found in the vocabulary.')
|
| 147 |
|
| 148 |
# Footer
|
| 149 |
st.markdown('<hr>', unsafe_allow_html=True)
|