5. Natural Language Processing (NLP) Essentials

5. Natural Language Processing (NLP) Essentials

Welcome to Module 5! We’re shifting gears to focus on Natural Language Processing (NLP), a fascinating field of AI concerned with enabling computers to understand, interpret, and generate human language. From chatbots to translation services, NLP powers many applications. This module covers foundational techniques for preparing and representing text data for machine learning models.

Learning Objectives

After this module, you will be able to:

  • Apply common text preprocessing steps like lowercasing, removing punctuation, and stop words.
  • Understand and implement tokenization (splitting text into words or subwords).
  • Explain different text vectorization techniques (Bag-of-Words, TF-IDF).
  • Describe the task of Sentiment Analysis and its common approaches.
  • Understand the goal of Named Entity Recognition (NER).
  • Grasp the basic concept of Word Embeddings as dense vector representations of words.
ℹ️
Interactive Practice: Copy the code examples into your Jupyter Notebook environment (Google Colab or local). For text processing, libraries like NLTK (pip install nltk) and scikit-learn are commonly used. For more advanced tasks, spaCy (pip install spacy && python -m spacy download en_core_web_sm) is very popular.

Text Preprocessing Techniques

Raw text data is often messy and needs cleaning before it can be effectively used by ML models. Preprocessing aims to standardize the text and reduce noise.

Common Steps:

  • Lowercasing: Converting all text to lowercase ensures that words like “Apple” and “apple” are treated as the same.
  • Removing Punctuation: Punctuation marks (,, ., !, ?, etc.) often don’t add significant meaning for basic analyses and can be removed.
  • Removing Stop Words: Stop words are common words (like “the”, “a”, “is”, “in”) that appear frequently but usually carry little specific meaning for tasks like topic classification. Libraries provide standard lists of stop words.
  • Stemming/Lemmatization: (More advanced) Reducing words to their root form.
    • Stemming: A cruder process, often chopping off word endings (e.g., “running” -> “run”, “studies” -> “studi”). Fast but can produce non-words.
    • Lemmatization: Uses vocabulary and morphological analysis to return the base dictionary form (lemma) of a word (e.g., “running” -> “run”, “studies” -> “study”). More accurate but slower.

Conceptual Example:

import re # Regular expressions for pattern matching
# Basic example without external libraries for illustration
# In practice, use libraries like NLTK or spaCy for robust processing

text = "Hello World! This is an Example sentence, with punctuation & numbers 123."
stop_words = {'is', 'an', 'this', 'with', 'the', 'a'} # Very basic example list

# 1. Lowercasing
text_lower = text.lower()
print(f"Lowercased: {text_lower}")

# 2. Removing Punctuation and Numbers (using regex)
text_no_punct = re.sub(r'[^\w\s]', '', text_lower) # Remove non-alphanumeric (keep whitespace)
text_no_num = re.sub(r'\d+', '', text_no_punct)   # Remove digits
print(f"No Punct/Nums: {text_no_num}")

# 3. Tokenization (Simple split on whitespace)
tokens = text_no_num.split()
print(f"Tokens: {tokens}")

# 4. Removing Stop Words
filtered_tokens = [word for word in tokens if word not in stop_words]
print(f"Filtered Tokens: {filtered_tokens}")

# Combining steps (typical pipeline)
processed_text = ' '.join(filtered_tokens) # Join back into string if needed
print(f"Processed Text: {processed_text}")
The specific preprocessing steps depend heavily on the task. For sentiment analysis, punctuation like “!” might be important. Always consider the trade-offs and tailor the pipeline accordingly. Libraries like NLTK and spaCy provide efficient functions for these steps.

Tokenization and Vectorization

Machine learning models work with numbers, not raw text. We need to convert processed text into numerical representations (vectors).

1. Tokenization: The process of breaking down text into smaller units called tokens. Tokens can be words, subwords, or characters.

  • Word Tokenization: Splitting text by spaces or punctuation (as seen in the example above).
  • Subword Tokenization: (More advanced, used in models like BERT) Breaks words into smaller, meaningful units (e.g., “tokenization” -> “token”, “##ization”). Handles rare words better.

2. Vectorization: Converting tokens into numerical vectors.

  • Bag-of-Words (BoW):

    • Creates a vocabulary of all unique words in the entire dataset (corpus).
    • Represents each document as a vector where each element corresponds to a word in the vocabulary.
    • The value of each element is typically the count of that word in the document (Count Vectorization) or a binary indicator (present/absent).
    • Limitation: Ignores word order and grammar, only considers presence/frequency.
  • TF-IDF (Term Frequency-Inverse Document Frequency):

    • Builds on BoW but weights words based on their importance.
    • Term Frequency (TF): How often a word appears in a specific document.
    • Inverse Document Frequency (IDF): How rare a word is across the entire corpus. Common words get a lower IDF score, rare words get a higher score.
    • TF-IDF Score = TF * IDF. Words that are frequent in a specific document but rare overall get a high score, signifying they are important discriminators for that document.

Scikit-learn Example:

from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer

corpus = [
    'this is the first document',
    'this document is the second document',
    'and this is the third one',
    'is this the first document',
]

# --- Bag-of-Words (Count Vectorizer) ---
count_vectorizer = CountVectorizer()
X_bow = count_vectorizer.fit_transform(corpus)

print("Vocabulary (BoW):", count_vectorizer.get_feature_names_out())
print("\nBag-of-Words Matrix (sparse):\n", X_bow)
print("\nBag-of-Words Matrix (dense):\n", X_bow.toarray()) # Convert sparse to dense for viewing

# --- TF-IDF Vectorizer ---
tfidf_vectorizer = TfidfVectorizer()
X_tfidf = tfidf_vectorizer.fit_transform(corpus)

print("\nVocabulary (TF-IDF):", tfidf_vectorizer.get_feature_names_out())
print("\nTF-IDF Matrix (dense):\n", X_tfidf.toarray())
ℹ️
These vector representations can then be fed into standard machine learning models (like Logistic Regression, Naive Bayes, SVMs) from scikit-learn for tasks like text classification.

Sentiment Analysis

One of the most common NLP tasks.

  • Goal: To determine the emotional tone or sentiment expressed in a piece of text (e.g., positive, negative, neutral).
  • Applications: Analyzing customer reviews, monitoring social media brand perception, gauging public opinion.
  • Common Approaches:
    • Lexicon-based: Using pre-defined dictionaries of words with associated sentiment scores. Simple but can miss context/nuance.
    • Machine Learning: Training a classification model (like Naive Bayes, SVM, or increasingly, deep learning models like LSTMs or Transformers) on labeled text data (e.g., movie reviews labeled positive/negative). This is the dominant approach now.
# Conceptual ML approach (using vectors from previous step)
# Assume X_tfidf (feature matrix) and y_sentiment (labels) are available
# from the vectorization step and your data labeling process.

import numpy as np # Needed for the example label array
from sklearn.linear_model import LogisticRegression

# Example dummy labels (replace with actual data)
y_sentiment = np.array([1, 0, 1, 1]) 

# --- Classifier Training ---
# Make sure X_tfidf and y_sentiment are defined and have compatible shapes
# X_tfidf = ... # Feature matrix from TfidfVectorizer
model = LogisticRegression()
# Ensure X_tfidf and y_sentiment are correctly defined before fitting
# model.fit(X_tfidf, y_sentiment)
# print("Model trained successfully (conceptually).")

# --- Prediction on New Data ---
# Assume 'new_text_vectorized' is the TF-IDF vector for new text input
# new_text_vectorized = ... # TF-IDF vector of the text to predict
# Ensure the model is trained and new_text_vectorized is defined
# predictions = model.predict(new_text_vectorized)
# print(f"Predicted sentiment for new text: {predictions}")

Named Entity Recognition (NER)

  • Goal: To identify and categorize named entities (like people, organizations, locations, dates, monetary values) mentioned in text.
  • Applications: Information extraction, knowledge graph creation, content categorization, improving search results.
  • Common Approaches: Often relies on statistical models (like Conditional Random Fields - CRFs) or deep learning models (BiLSTMs, Transformers) trained on annotated datasets. Libraries like spaCy provide excellent pre-trained NER models.

Conceptual spaCy Example:

import spacy

# Load a pre-trained spaCy model (e.g., English core small)
# Make sure you have the model downloaded: python -m spacy download en_core_web_sm
# nlp = spacy.load("en_core_web_sm") 

# Define the text to analyze
text_to_analyze = "Apple Inc. is looking at buying U.K. startup for $1 billion in London."

# Process the text with the loaded spaCy model
# Ensure the 'nlp' model is loaded before processing
# doc = nlp(text_to_analyze)

print("\nNamed Entities (Conceptual - requires model loading):")
# Ensure 'doc' is defined (i.e., model loaded and text processed)
# for ent in doc.ents:
#     print(f"- Entity: {ent.text}, Label: {ent.label_}") 
    # Expected Output might include: Apple Inc. (ORG), U.K. (GPE), $1 billion (MONEY), London (GPE)

Introduction to Word Embeddings

BoW and TF-IDF represent words based on their frequency but ignore semantic meaning (e.g., “king” and “queen” are treated as completely separate entities).

  • Word Embeddings: Represent words as dense, low-dimensional numerical vectors in a way that captures semantic relationships. Words with similar meanings tend to have vectors that are close together in the vector space.
  • How they are learned: Typically learned from large amounts of text data using neural network models. The models learn to predict a word based on its context (or vice-versa), and the learned weights in the network’s hidden layer become the word embeddings.
  • Popular Algorithms: Word2Vec, GloVe, fastText.
  • Pre-trained Embeddings: You can often download embeddings pre-trained on massive datasets (like Wikipedia or Google News) and use them in your own models, saving significant training time.
Word embeddings are a foundational concept in modern NLP and deep learning. They provide much richer input representations for models compared to sparse methods like BoW/TF-IDF, often leading to better performance on tasks like sentiment analysis, text classification, and machine translation. We often use these embeddings as the first layer in deep learning models for NLP.

Practice Exercises (Take-Home Style)

  1. Preprocessing: Take the sentence " Learning NLP is FUN!! 😄 Includes numbers 123. " and apply: lowercasing, removing numbers, removing punctuation (keep spaces), and tokenizing by whitespace.
    • Expected Result: ['learning', 'nlp', 'is', 'fun', 'includes', 'numbers'] (or similar, depending on exact punctuation handling)
  2. Vectorization Concept: Explain the main difference between Bag-of-Words and TF-IDF in how they assign importance to words. Which technique gives higher weight to words that are unique to a specific document within a collection?
    • Expected Result: BoW uses raw counts or presence/absence. TF-IDF weights words by both their frequency in a document (TF) and their rarity across all documents (IDF). TF-IDF gives higher weight to words unique/rare across the collection but frequent in a specific document.
  3. Task Identification: Identify the primary NLP task (e.g., Sentiment Analysis, NER, Text Classification) most relevant to:
    • Extracting all company names from a news article.
    • Determining if a customer support email is positive or negative.
    • Categorizing forum posts into ’technical support’, ‘feature request’, or ‘general discussion’.
    • Expected Result: NER, Sentiment Analysis, Text Classification.
  4. Word Embeddings: Why are word embeddings often preferred over Bag-of-Words for input to deep learning models?
    • Expected Result: Embeddings capture semantic meaning and relationships between words, providing a richer representation. They are dense vectors, which are often more suitable for neural networks than high-dimensional sparse BoW vectors.

Summary

You’ve learned the initial steps for preparing text data (preprocessing, tokenization) and converting it into numerical formats suitable for machine learning (vectorization using BoW and TF-IDF). We introduced key NLP tasks like Sentiment Analysis and Named Entity Recognition. Finally, we discussed the concept of Word Embeddings as dense, meaning-rich representations of words, crucial for modern NLP models.

Additional Resources

Next: Let’s explore how AI can ‘see’. Proceed to Module 6: Computer Vision Essentials.

Last updated on