NLP Exercises Hands-on Practice
Beginner to Intermediate Interview Ready

NLP Exercises – Topic-wise Coding Practice

Strengthen your Natural Language Processing skills with short, topic-wise coding exercises on preprocessing, text representation, classification, sequence labeling, transformers and real-world applications in Python.

1. Text Preprocessing Exercises

Exercise 1.1 – Implement a reusable preprocessing function
Topic: Preprocessing Level: Easy

Write a function that takes a raw text string and returns a cleaned version with lowercase letters, removed HTML tags, normalized whitespace and optional stopword removal. Test it on at least five noisy example sentences and show before/after pairs.

Exercise 1.2 – Compare stemming and lemmatization
Topic: Preprocessing Level: Easy

Given a list of words, print their stemmed and lemmatized forms side-by-side to see how the algorithms differ on English text. Summarize which words are over‑stemmed or incorrectly lemmatized and discuss when this might hurt downstream tasks.

Exercise 1.3 – Design a preprocessing pipeline for tweets
Topic: Social Media Text Level: Intermediate

Define a preprocessing pipeline for Twitter or social media data that handles mentions, hashtags, emojis, URLs and repeated characters. Specify the exact steps and justify which elements you normalize, which you keep and why.

Exercise 1.4 – Unicode normalization and punctuation
Topic: Normalization Level: Intermediate

Collect examples of visually similar but distinct Unicode characters (different quotes, dashes, accented characters). Describe or implement a normalization strategy that maps them to a canonical form and explain its impact on tokenization.

2. Text Representation & Embeddings Exercises

Exercise 2.1 – Bag-of-words vs TF-IDF
Topic: Text Representation Level: Easy

Using a small corpus of 5–10 sentences, build both bag-of-words and TF-IDF matrices and print them to compare how frequent and rare words are weighted.

Exercise 2.2 – Load pre-trained word embeddings
Topic: Embeddings Level: Intermediate

Load pre-trained GloVe or Word2Vec embeddings and write a function that returns the top-5 most similar words to a query token using cosine similarity. Run it on at least three different query words and interpret the neighbours you get.

Exercise 2.3 – Document embeddings with averaging
Topic: Embeddings Level: Intermediate

Given pre-trained word embeddings, build simple document embeddings by averaging word vectors. Compare cosine similarity between document pairs from similar and different topics.

Exercise 2.4 – Handling out-of-vocabulary words
Topic: Embeddings Level: Intermediate

Design and implement at least two strategies for handling words that are not present in your embedding vocabulary (e.g., <UNK> token, subword composition). Compare their effect on a simple downstream task.

3. Text Classification Exercises

Exercise 3.1 – Train a baseline sentiment classifier
Topic: Classification Level: Beginner

Using any small labeled sentiment dataset, train a logistic regression classifier with TF-IDF features and print confusion matrix and F1-score.

Exercise 3.2 – Evaluate model on out-of-domain data
Topic: Generalization Level: Intermediate

Train on one domain (e.g., movie reviews) and evaluate on another (e.g., product reviews). Analyze performance drop and write a short explanation of why it happens.

Exercise 3.3 – Error analysis for misclassified examples
Topic: Evaluation Level: Intermediate

Collect at least 20 misclassified texts from your classifier and categorize error types (sarcasm, domain shift, negation, label noise). Propose at least three concrete changes to reduce these errors.

Exercise 3.4 – Dealing with class imbalance
Topic: Imbalanced Data Level: Intermediate

Take a classification dataset with imbalanced labels and experiment with at least two techniques such as class weights, undersampling or oversampling. Compare macro F1 before and after applying each technique.

4. Sequence Labeling & NER Exercises

Exercise 4.1 – Visualize NER output
Topic: NER Level: Beginner

Use spaCy or a Hugging Face NER model to detect entities in a paragraph and print the text with entities wrapped in color-coded brackets such as <ORG>Apple</ORG>.

Exercise 4.2 – Implement BIO tagging conversion
Topic: Sequence Labeling Level: Intermediate

Given a list of entity spans (start, end, label) and the tokenized text, write a function that converts them into BIO tags and verify your tags on a few examples.

Exercise 4.3 – Compare NER across domains
Topic: NER Level: Intermediate

Run the same off‑the‑shelf NER model on news text, scientific abstracts and social media posts. Manually inspect outputs and list at least five typical failure cases per domain.

Exercise 4.4 – Custom entity schema design
Topic: Schema Design Level: Intermediate

For a chosen domain (e.g., resumes, finance, healthcare), design a custom NER label set and write guidelines describing when to assign each label. Create at least 10 manually annotated example sentences with your schema.

5. Transformers & Fine-tuning Exercises

Exercise 5.1 – Inspect attention weights
Topic: Transformers Level: Intermediate

Run a small sentence through a BERT model and extract attention weights for a chosen head. Visualize them as a matrix to see which words attend to which tokens.

Exercise 5.2 – Fine-tune a transformer on a small dataset
Topic: Fine-tuning Level: Intermediate

Fine-tune a small transformer (e.g., DistilBERT) on a sentiment or classification dataset with 1–2 epochs and track training/validation curves. Experiment with different learning rates and batch sizes and compare results.

Exercise 5.3 – Prompt design for generative models
Topic: LLMs Level: Intermediate

Design at least five different prompts to solve the same NLP task (e.g., sentiment, extraction) with a generative model. Compare outputs and discuss which prompt patterns work best and why.

Exercise 5.4 – Measuring inference latency
Topic: Deployment Level: Intermediate

Measure average inference time for a transformer model on short vs long inputs. Plot latency against sequence length and discuss trade‑offs for real‑time applications.

6. Applications & Mini Project Exercises

Exercise 6.1 – Build a CLI sentiment tool
Topic: Applications Level: Beginner

Create a small command-line script that reads a sentence from the user and prints predicted sentiment using a pre-trained model.

Exercise 6.2 – Simple QA over a single article
Topic: Question Answering Level: Intermediate

Load one article as context and let users ask questions from the terminal. Use a QA pipeline and print both the answer and confidence score.

Exercise 6.3 – Evaluation dashboard sketch
Topic: MLOps Level: Intermediate

Design a simple evaluation dashboard (wireframe or written description) to monitor an NLP system in production. List the key metrics, error slices and logs you would track over time.

Exercise 6.4 – Ethical considerations checklist
Topic: Ethics Level: Intermediate

Create a checklist of at least 10 questions to evaluate the ethical impact of an NLP application (bias, privacy, misuse). Apply it to one of your own projects and summarize the main risks you discovered.