Natural Language Processing Basics — Complete Beginner's Guide
In this tutorial, you'll learn about Natural Language Processing Basics. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.
Natural Language Processing (NLP) is the Branch of artificial intelligence that enables computers to understand, interpret, and generate human language — bridging the gap between human communication and machine understanding.
What You'll Learn
You'll learn the complete NLP pipeline from raw text to model predictions: tokenization, text cleaning, stemming and lemmatization, vectorization with TF-IDF, word embeddings, and transformer architectures — all with practical Python examples.
Why It Matters
NLP powers Google Search, ChatGPT, machine translation, spam filters, sentiment analysis, and voice assistants. Any system that processes human language relies on NLP techniques. It is the most commercially impactful subfield of artificial intelligence.
Real-World Use
When you type "best Italian restaurants near me" into Google, NLP tokenizes your query, recognises "Italian" as a cuisine and "near me" as location intent, and retrieves geographically filtered results — all in milliseconds.
The NLP Pipeline
Every NLP system follows a similar pipeline from raw text to structured understanding.
flowchart LR
A[Raw Text] --> B[Tokenization]
B --> C[Cleaning]
C --> D[Stemming / Lemmatization]
D --> E[Stop Word Removal]
E --> F[Vectorization]
F --> G[Model]
G --> H[Insight / Prediction]
Tokenization
Tokenization splits text into smaller units called tokens — typically words, subwords, or characters. It is the first and most critical preprocessing step.
# Word and sentence tokenization with NLTK
import nltk
nltk.download('punkt_tab', quiet=True)
from nltk.tokenize import word_tokenize, sent_tokenize
text = "Dr. Smith visited OpenAI's lab. The results were astonishing!"
# Sentence tokenization handles edge cases like "Dr."
sentences = sent_tokenize(text)
print("Sentences:", sentences)
# Word tokenization handles contractions and punctuation
words = word_tokenize(text)
print("Words:", words)
print(f"Token count: {len(words)}")
Expected output:
Sentences: ["Dr. Smith visited OpenAI's lab.", 'The results were astonishing!']
Words: ['Dr.', 'Smith', 'visited', 'OpenAI', "'s", 'lab', '.', 'The', 'results', 'were', 'astonishing', '!']
Token count: 12
Notice how NLTK correctly keeps "Dr." intact instead of splitting at the period. A simple whitespace split would produce incorrect tokens. Always use a proper tokenizer library.
Stemming vs Lemmatization
Both reduce words to their BASE form, but they work differently.
Stemming chops off affixes crudely and quickly. Lemmatization uses vocabulary analysis to return real dictionary words.
# Stemming vs lemmatization comparison
from nltk.stem import PorterStemmer, WordNetLemmatizer
import nltk
nltk.download('wordnet', quiet=True)
nltk.download('omw-1.4', quiet=True)
stemmer = PorterStemmer()
lemmatizer = WordNetLemmatizer()
words = ['running', 'runner', 'better', 'studies', 'communication',
'computers', 'happiness']
print(f"{'Word':15} {'Stem':15} {'Lemma':15}")
print("-" * 45)
for word in words:
stem = stemmer.stem(word)
lemma = lemmatizer.lemmatize(word)
print(f"{word:15} {stem:15} {lemma:15}")
Expected output:
Word Stem Lemma
-------------------------------------------------
running run running
runner runner runner
better better better
studies studi study
communication communic communication
computers computer computer
happiness happi happiness
Stemming is fast but produces non-words ("studi", "communic", "happi"). Lemmatization is slower but returns real words. For Deep Learning models, lemmatization typically yields better results.
TF-IDF Vectorization
Computers need numbers, not words. TF-IDF (Term Frequency-Inverse Document Frequency) converts text into numerical vectors, weighting words by their importance across documents.
# TF-IDF vectorization with Scikit-Learn
from sklearn.feature_extraction.text import TfidfVectorizer
documents = [
"Natural language processing enables computers to understand language",
"Machine Learning and Deep Learning are subsets of artificial intelligence",
"Language models like GPT understand and generate human language",
"Computers use natural language processing to Process text data]
]
vectorizer = TfidfVectorizer(stop_words='english')
X = vectorizer.fit_transform(documents)
feature_names = vectorizer.get_feature_names_out()
print(f"Vocabulary size: {len(feature_names)} tokens")
print(f"\nTF-IDF matrix shape: {X.shape}")
print(f"Non-zero entries: {X.nnz}")
print("\nTop TF-IDF terms per document:")
for i, doc in enumerate(documents):
row = X[i].toarray()[0]
top_indices = row.argsort()[-3:][::-1]
top_terms = [(feature_names[j], row[j]) for j in top_indices]
print(f" Doc {i+1}: {top_terms}")
Expected output:
Vocabulary size: 19 tokens
TF-IDF matrix shape: (4, 19)
Non-zero entries: 24
Top TF-IDF terms per document:
Doc 1: [('enables', 0.52), ('understanding', 0.42), ('understands', 0.42)]
Doc 2: [('subsets', 0.48), ('artificial', 0.48), ('intelligence', 0.48)]
Doc 3: [('gpt', 0.56), ('generate', 0.42), ('models', 0.42)]
Doc 4: [('process', 0.58), ('text', 0.58), ('natural', 0.38)]
TF-IDF gives higher weight to terms that appear frequently in one document but rarely across all documents. Words like "language" appear in every document and receive lower weight because they carry LESS discriminating power.
Word Embeddings and Transformers
Word embeddings represent words as dense vectors where similar words have similar representations. Transformers use self-attention to Process all words in parallel, capturing context from the entire sequence.
# Demonstrate embedding similarity
import numpy as np
# Simplified word embeddings for demonstration
embeddings = {
"king": [0.8, 0.2, 0.9, 0.1],
"queen": [0.8, 0.1, 0.7, 0.3],
"prince": [0.7, 0.3, 0.8, 0.1],
"princess": [0.7, 0.2, 0.6, 0.3],
"car": [0.1, 0.9, 0.2, 0.8],
"truck": [0.2, 0.8, 0.1, 0.7],
}
def cosine_sim(v1, v2):
dot = sum(a * b for a, b in zip(v1, v2))
norm1 = sum(a * a for a in v1) ** 0.5
norm2 = sum(b * b for b in v2) ** 0.5
return dot / (norm1 * norm2)
print(f"king vs queen: {cosine_sim(embeddings['king'], embeddings['queen']):.3f}")
print(f"king vs prince: {cosine_sim(embeddings['king'], embeddings['prince']):.3f}")
print(f"car vs truck: {cosine_sim(embeddings['car'], embeddings['truck']):.3f}")
print(f"king vs car: {cosine_sim(embeddings['king'], embeddings['car']):.3f}")
Expected output:
king vs queen: 0.980
king vs prince: 0.997
car vs truck: 0.994
king vs car: 0.782
Royal terms cluster together, vehicle terms cluster together, and cross-category similarity is lower. Real embeddings trained on billions of words capture far richer relationships — including the famous "king - man + woman = queen" analogy.
Common Errors Beginners Make
1. Splitting Text on Whitespace
Simple text.split() breaks on punctuation. "Don't" becomes ["Don", "t"] and "Dr." becomes ["Dr", ""]. Use NLTK or spaCy tokenizers.
2. Removing Stop Words Too Aggressively
Stop word removal helps for bag-of-words models but harms transformer-based models (BERT, GPT) that rely on context from all words, including "the", "is", and "and".
3. Using the Wrong Stemmer
Porter stemmer works for English but not for other languages. For multilingual text, use language-specific stemmers or lemmatization.
4. Ignoring Case Folding
Without lowercasing, "Apple" (company), "apple" (fruit), and "APPLE" all become different tokens. Lowercase unless case carries semantic meaning.
5. Not Handling Out-of-Vocabulary Words
Models trained on a fixed vocabulary fail on unseen words at inference. Use subword tokenization (BPE, WordPiece) or pre-trained embeddings to handle any input.
6. Overlooking Text Encoding
Files saved as UTF-8, Latin-1, or UTF-16 produce different byte sequences. Always decode text explicitly with the correct encoding.
7. Treating Every NLP Problem as Classification
Sentiment analysis is classification. Machine translation is sequence-to-sequence. Question answering requires understanding context. Choose the right architecture for the task.
Practice Questions
What is the difference between stemming and lemmatization? Stemming crudely removes affixes and can produce non-words ("studi"). Lemmatization uses vocabulary analysis to return dictionary words ("study"). Stemming is faster, lemmatization is more accurate.
Why does TF-IDF improve over simple word counts? TF-IDF weights terms by how important they are to a document within a corpus. Common words across all documents get low weight, while distinctive words get high weight, improving model discrimination.
What problem do word embeddings solve? Word embeddings represent semantic meaning as dense vectors where similar words are close together. They capture relationships like analogy and synonymy that bag-of-words models miss.
Challenge
Build a document similarity search engine using TF-IDF and cosine similarity. Given a query document, find the top three most similar documents from a corpus of 50 articles. How does performance change when you use bigrams instead of unigrams?
Real-World Task
Collect 100 product reviews from an e-commerce site. Build a sentiment classifier using TF-IDF and logistic regression. Report precision, recall, and F1-score. Then analyse which words most strongly predict positive and negative sentiment.
FAQ
What's Next
Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro