Home Topics NLP

Natural Language Processing

Teaching machines to understand human language

8 hours 18 examples Intermediate
Basics

What is NLP?

Natural Language Processing (NLP) is a field of AI that helps computers understand, interpret, and generate human language. It powers everything from Google Search to ChatGPT!

Chatbots

ChatGPT, Siri, Alexa

Translation

Google Translate, DeepL

Sentiment Analysis

Review analysis, social media

Spam Detection

Email filtering

Basics

Tokenization

Tokenization splits text into smaller units called tokens (words, subwords, or characters). It's the first step in any NLP pipeline!

tokenization.py
# Simple tokenization with Python
text = "Natural Language Processing is amazing!"

# Method 1: Basic split
tokens_basic = text.split()
print("Basic split:", tokens_basic)

# Method 2: Using NLTK (Natural Language Toolkit)
import nltk
# nltk.download('punkt')  # Run once to download
from nltk.tokenize import word_tokenize

tokens_nltk = word_tokenize(text)
print("NLTK tokens:", tokens_nltk)

# Sentence tokenization
paragraph = "NLP is cool. It powers ChatGPT. Let's learn more!"
from nltk.tokenize import sent_tokenize
sentences = sent_tokenize(paragraph)
print("Sentences:", sentences)
Output
Basic split: ['Natural', 'Language', 'Processing', 'is', 'amazing!']
NLTK tokens: ['Natural', 'Language', 'Processing', 'is', 'amazing', '!']
Sentences: ['NLP is cool.', 'It powers ChatGPT.', "Let's learn more!"]

Notice the Difference!

NLTK separates punctuation ("!" becomes its own token), while basic split keeps "amazing!" together. This matters for analysis!

Techniques

Text Preprocessing

Raw text is messy! Clean it up with these common preprocessing steps:

text_preprocessing.py
import re
from nltk.corpus import stopwords
from nltk.stem import PorterStemmer, WordNetLemmatizer

text = "The QUICK brown foxes are JUMPING over the lazy dogs!!!"

# Step 1: Lowercase
text_lower = text.lower()
print("1. Lowercase:", text_lower)

# Step 2: Remove punctuation
text_clean = re.sub(r'[^\w\s]', '', text_lower)
print("2. No punctuation:", text_clean)

# Step 3: Remove stopwords (the, is, are...)
# nltk.download('stopwords')  # Run once
stop_words = set(stopwords.words('english'))
words = text_clean.split()
filtered = [w for w in words if w not in stop_words]
print("3. No stopwords:", filtered)

# Step 4: Stemming (reduce to root form)
stemmer = PorterStemmer()
stemmed = [stemmer.stem(w) for w in filtered]
print("4. Stemmed:", stemmed)

# Step 5: Lemmatization (smarter root form)
# nltk.download('wordnet')  # Run once
lemmatizer = WordNetLemmatizer()
lemmatized = [lemmatizer.lemmatize(w) for w in filtered]
print("5. Lemmatized:", lemmatized)
Output
1. Lowercase: the quick brown foxes are jumping over the lazy dogs!!!
2. No punctuation: the quick brown foxes are jumping over the lazy dogs
3. No stopwords: ['quick', 'brown', 'foxes', 'jumping', 'lazy', 'dogs']
4. Stemmed: ['quick', 'brown', 'fox', 'jump', 'lazi', 'dog']
5. Lemmatized: ['quick', 'brown', 'fox', 'jumping', 'lazy', 'dog']
Techniques

Sentiment Analysis

Sentiment Analysis determines if text is positive, negative, or neutral. Essential for analyzing reviews, social media, and customer feedback!

sentiment_analysis.py
from textblob import TextBlob

# Analyze sentiment of reviews
reviews = [
    "This product is absolutely amazing! Best purchase ever!",
    "Terrible quality. Broke after one day. Never buying again.",
    "It's okay, nothing special but works fine."
]

for review in reviews:
    blob = TextBlob(review)
    sentiment = blob.sentiment
    
    # Polarity: -1 (negative) to 1 (positive)
    if sentiment.polarity > 0.1:
        label = "😊 Positive"
    elif sentiment.polarity < -0.1:
        label = "😞 Negative"
    else:
        label = "😐 Neutral"
    
    print(f"Review: '{review[:40]}...'")
    print(f"  Polarity: {sentiment.polarity:.2f} | {label}\n")
Output
Review: 'This product is absolutely amazing! Best ...'
  Polarity: 0.85 | 😊 Positive

Review: 'Terrible quality. Broke after one day. Ne...'
  Polarity: -0.65 | 😞 Negative

Review: 'It's okay, nothing special but works fine...'
  Polarity: 0.10 | 😐 Neutral
Advanced

Word Embeddings

Word Embeddings represent words as dense vectors where similar words have similar vectors. This is how modern NLP understands meaning!

The Famous Example

King - Man + Woman = Queen

Word vectors capture semantic relationships!

Word2Vec

Google's 2013 model. Fast, efficient. 300d vectors.

GloVe

Stanford's model. Better for rare words. Pre-trained on Wikipedia.

FastText

Facebook's model. Handles misspellings and rare words.

BERT Embeddings

Context-aware! Same word gets different vectors based on sentence.

🎉 You've Learned NLP Basics!

Ready to use state-of-the-art models? Learn Hugging Face next!