Skip to content
Tutorials Center
My learning

Topics

Lesson 61 of 63 ยท Practice and Review

Practice Exercises with Solutions

15 graded NLP exercises with runnable solutions: tokenizing, normalizing, n-grams, TF-IDF, cosine similarity, edit distance, Naive Bayes and metrics.

You learn natural language processing by pushing real strings through real code, not by reading about it. This page collects 15 exercises that cover the whole course, from counting words to training a classifier. They are ordered from easy to harder, and each one has a short task, an editor with starter code and a hidden solution.

Here is how to get the most out of them:

  • Try first, peek later. Give each exercise an honest attempt before opening the solution. Being stuck for a few minutes is where the learning happens.
  • Run often. Each starter prints test calls, and the expected output is written in the comments. Each starter has a stub that returns a placeholder (marked # replace this), so it runs before you change anything but prints the wrong answer.
  • Compare, don't copy. Your answer may look different from ours and still be right. Read ours for ideas, then improve yours.

Everything runs in your browser with the standard library plus scikit-learn, so there is nothing to install. The cheat sheet is handy to keep open in another tab.

Fifteen exercises, four groups

Text and tokens Exercises 1 to 5.
Counting and vectors Exercises 6 to 10.
Edit distance Exercise 11.
Classification Exercises 12 to 15.

Each exercise links to the lesson it practices.

1 / 2

Text and tokens

These practice text basics, cleaning, tokenization and stop words.

1. Most common words (Easy)

Write top_words(text, n) that returns the n most frequent words, lowercased, ignoring punctuation. Treat an apostrophe as part of a word.

Python Exercise 1: Most common words

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Show solution
Python
import re
from collections import Counter

def top_words(text, n=3):
    words = re.findall(r"[a-z']+", text.lower())
    return Counter(words).most_common(n)

print(top_words("The cat and the hat. The CAT sat; the end."))

most_common keeps insertion order for ties, so and (the first of several words seen once) comes third.

2. A regex tokenizer (Easy)

Write tokenize(text) that splits text into words (keeping contractions such as it's whole), numbers (keeping a decimal point) and single punctuation marks.

Python Exercise 2: Tokenizer

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Show solution
Python
import re

def tokenize(text):
    return re.findall(r"[A-Za-z]+(?:'[a-z]+)?|\d+(?:\.\d+)?|[^\w\s]", text)

print(tokenize("Don't pay $4.50 for it, it's a rip-off!"))

The pattern has three alternatives, tried left to right: a word with an optional 'xx ending, a number with an optional decimal part, or any single character that is neither a word character nor whitespace. Splitting rip-off into three tokens is a design choice; a stricter pattern could keep hyphenated words together.

3. Strip accents (Easy)

Write strip_accents(text) that removes accents so that Crรจme brรปlรฉe becomes Creme brulee. Use unicodedata.

Python Exercise 3: Strip accents

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Show solution
Python
import unicodedata

def strip_accents(text):
    decomposed = unicodedata.normalize("NFD", text)
    return "".join(c for c in decomposed if not unicodedata.combining(c))

print(strip_accents("Crรจme brรปlรฉe ร  la faรงon de Sรฃo Paulo"))

NFD splits รจ into e plus a combining grave accent. Dropping the combining characters leaves the base letters. This works for Latin scripts; it is the wrong tool for languages where the mark changes the meaning of a word.

4. Stop words with a safety net (Medium)

Write remove_stop(tokens) that drops stop words but keeps negations. not and no are in the stop list on purpose, because removing them would flip the meaning of a sentence.

Python Exercise 4: Remove stop words

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Show solution
Python
STOP = {"the", "a", "an", "is", "was", "this", "it", "and", "of", "to", "not", "no", "very"}
KEEP = {"not", "no"}

def remove_stop(tokens):
    return [t for t in tokens if t not in STOP or t in KEEP]

print(remove_stop("this movie was not a very good one".split()))

A token survives if it is not a stop word, or if it is protected. Computing STOP - KEEP once and testing membership in that set is an equivalent, slightly cleaner version.

5. Type-token ratio (Easy)

The type-token ratio is the number of distinct words divided by the total number of words. Write type_token_ratio(tokens) and round the result to three decimals when printing.

Python Exercise 5: Type-token ratio

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Show solution
Python
def type_token_ratio(tokens):
    return len(set(tokens)) / len(tokens)

t = "the quick brown fox jumps over the lazy dog the end".split()
print(round(type_token_ratio(t), 3))

There are 11 tokens and 9 distinct ones (the appears three times), so the ratio is 9 / 11. The ratio falls as texts get longer, which is why it is only fair to compare texts of similar length (see Zipf's law).

Counting and vectors

These practice n-grams, bag of words, TF-IDF and document similarity.

6. Count bigrams (Easy)

Write bigram_counts(tokens) that returns a Counter of adjacent word pairs.

Python Exercise 6: Bigram counts

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Show solution
Python
from collections import Counter

def bigram_counts(tokens):
    return Counter(zip(tokens, tokens[1:]))

print(bigram_counts("to be or not to be".split()).most_common(2))

Zipping a list with itself shifted by one gives every adjacent pair. The same trick extends to trigrams with zip(t, t[1:], t[2:]).

7. Jaccard similarity (Easy)

Write jaccard(a, b) that compares two sentences as sets of words: the size of the intersection divided by the size of the union.

Python Exercise 7: Jaccard similarity

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Show solution
Python
def jaccard(a, b):
    sa, sb = set(a.split()), set(b.split())
    return len(sa & sb) / len(sa | sb)

print(round(jaccard("the cat sat on the mat", "the cat lay on a mat"), 3))

The shared words are the, cat, on and mat (4), and the union has 7 words, so the score is 4 / 7. Sets ignore counts, so this measure does not care how many times a word repeats.

8. A document-term matrix by hand (Medium)

Write bow_vectors(docs) that returns a sorted vocabulary and one count vector per document, with no library.

Python Exercise 8: Bag-of-words vectors

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Show solution
Python
def bow_vectors(docs):
    vocab = sorted({w for d in docs for w in d.split()})
    vectors = [[d.split().count(w) for w in vocab] for d in docs]
    return vocab, vectors

vocab, matrix = bow_vectors(["red red blue", "blue green"])
print(vocab)
print(matrix)

Sorting the vocabulary makes the column order deterministic. Calling .count for every word and document is fine for a toy, but it is quadratic; real code (CountVectorizer) counts each document once and stores a sparse matrix.

9. Find the most distinctive word (Medium)

Write tfidf_scores(docs, i) that returns a dictionary of TF-IDF scores for document i. Use tf = count / length and the textbook idf = log(N / df). Then print the two highest-scoring words, breaking ties alphabetically.

Python Exercise 9: TF-IDF scores

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Show solution
Python
import math
from collections import Counter

def tfidf_scores(docs, i):
    tokens = [d.split() for d in docs]
    n = len(docs)
    df = Counter(w for t in tokens for w in set(t))
    doc = tokens[i]
    return {w: doc.count(w) / len(doc) * math.log(n / df[w]) for w in set(doc)}

docs = ["the cat sat on the mat", "the dog sat on the log",
        "the cat chased the dog", "a bird sang"]
for i in (0, 1):
    scores = tfidf_scores(docs, i)
    print(i, sorted(scores.items(), key=lambda kv: (-kv[1], kv[0]))[:2])

mat and log each appear in exactly one document, so their IDF is log(4), the highest possible here. the occurs most often but is in three of four documents, so its weight is small. Note the sort key: negating the score sorts descending, and the word itself breaks ties.

10. Nearest document by cosine (Medium)

Write cosine(u, v) for two count vectors (return 0.0 if either is all zeros). Then use it to find the corpus document closest to a query.

Python Exercise 10: Nearest document

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Show solution
Python
import math

def cosine(u, v):
    dot = sum(a * b for a, b in zip(u, v))
    nu = math.sqrt(sum(a * a for a in u))
    nv = math.sqrt(sum(b * b for b in v))
    return dot / (nu * nv) if nu and nv else 0.0

corpus = ["cheap pills online", "meeting moved to monday",
          "buy cheap watches online", "lunch on monday?"]
vocab = sorted({w for d in corpus for w in d.split()})
vec = lambda d: [d.split().count(w) for w in vocab]

query = "cheap online pills"
best = max(range(len(corpus)), key=lambda i: cosine(vec(query), vec(corpus[i])))
print(best, corpus[best], round(cosine(vec(query), vec(corpus[best])), 3))

The query uses the same words as document 0 in a different order. A bag of words ignores order, so the two vectors are identical and the cosine is exactly 1.0. The guard against a zero norm avoids dividing by zero for queries made only of unknown words.

Edit distance

This practices spelling correction.

11. Levenshtein distance (Hard)

Write edit_distance(a, b): the minimum number of insertions, deletions and substitutions needed to turn a into b. Use dynamic programming with two rows instead of a full table.

Python Exercise 11: Edit distance

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Show solution
Python
def edit_distance(a, b):
    prev = list(range(len(b) + 1))
    for i, ca in enumerate(a, 1):
        cur = [i]
        for j, cb in enumerate(b, 1):
            cur.append(min(prev[j] + 1,                # delete from a
                           cur[j - 1] + 1,             # insert into a
                           prev[j - 1] + (ca != cb)))  # substitute (free if equal)
        prev = cur
    return prev[-1]

print(edit_distance("kitten", "sitting"))
print(edit_distance("flaw", "lawn"))
print(edit_distance("", "abc"))

prev[j] is the distance between the first i - 1 characters of a and the first j of b. Each cell is the cheapest of three moves, and a match costs nothing because ca != cb is False, which Python treats as 0. Only the previous row is needed, so memory is proportional to the length of b.

Classification

These practice Naive Bayes, logistic regression, evaluation and rule-based extraction.

12. Train a tiny spam filter (Medium)

Build a scikit-learn pipeline of CountVectorizer and MultinomialNB, fit it on the six messages, and predict the three new ones.

Python Exercise 12: Spam filter

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Show solution
Python
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.pipeline import make_pipeline

train = ["win free money now", "free prize claim now", "cheap pills free",
         "meeting at noon", "lunch tomorrow with team", "project report attached"]
y = ["spam", "spam", "spam", "ham", "ham", "ham"]

clf = make_pipeline(CountVectorizer(), MultinomialNB()).fit(train, y)
print(clf.predict(["free money prize", "team meeting tomorrow", "report on pills"]))
print(clf.predict_proba(["free money prize"]).round(3), clf.classes_)

The second print shows the probabilities: about 0.065 for ham and 0.935 for spam, in the order of clf.classes_. With six training messages these numbers are fragile, so treat them as a demonstration, not as evidence the filter works.

13. Precision, recall and F1 by hand (Medium)

Write prf(y_true, y_pred) that returns precision, recall and F1 for the positive class 1, without scikit-learn. Return 0.0 when a denominator is zero.

Python Exercise 13: Precision, recall, F1

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Show solution
Python
def prf(y_true, y_pred, pos=1):
    tp = sum(t == pos and p == pos for t, p in zip(y_true, y_pred))
    fp = sum(t != pos and p == pos for t, p in zip(y_true, y_pred))
    fn = sum(t == pos and p != pos for t, p in zip(y_true, y_pred))
    p = tp / (tp + fp) if tp + fp else 0.0
    r = tp / (tp + fn) if tp + fn else 0.0
    f = 2 * p * r / (p + r) if p + r else 0.0
    return p, r, f

yt = [1, 1, 1, 1, 0, 0, 0, 0, 0, 0]
yp = [1, 1, 0, 0, 1, 0, 0, 0, 0, 0]
print([round(x, 3) for x in prf(yt, yp)])

Here there are 2 true positives, 1 false positive and 2 false negatives. Precision is 2/3, recall is 2/4 and F1, their harmonic mean, is 4/7. Accuracy would look better: 7 of 10 predictions are correct, yet the model finds only half of the positives.

14. Extract emails and dates (Medium)

Write extract(text) that returns a tuple of two lists: the email addresses and the dates written as YYYY-MM-DD.

Python Exercise 14: Extract emails and dates

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Show solution
Python
import re

def extract(text):
    emails = re.findall(r"[\w.+-]+@[\w-]+(?:\.[\w-]+)+", text)
    dates = re.findall(r"\b\d{4}-\d{2}-\d{2}\b", text)
    return emails, dates

print(extract("Write to [email protected] or [email protected] before 2026-03-15; the event was on 2025-12-01."))

The email pattern is deliberately simple: it accepts what you meet in everyday text but is not a full implementation of the email standard. The date pattern checks the shape only, so 2026-99-99 would also match; validate with datetime.date.fromisoformat if that matters.

15. Read the model's mind (Hard)

Fit a TfidfVectorizer and a LogisticRegression (random_state=0) on the same six messages, with labels 1 for spam and 0 for ham. Print the three words with the largest positive weights and the three with the most negative weights.

Python Exercise 15: Top weights

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Show solution
Python
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression

train = ["win free money now", "free prize claim now", "cheap pills free",
         "meeting at noon", "lunch tomorrow with team", "project report attached"]
labels = [1, 1, 1, 0, 0, 0]

vec = TfidfVectorizer()
X = vec.fit_transform(train)
model = LogisticRegression(random_state=0).fit(X, labels)

names = vec.get_feature_names_out()
order = model.coef_[0].argsort()      # indices from most negative to most positive
print([names[i] for i in order[-3:][::-1]])
print([names[i] for i in order[:3]])

coef_[0] holds one weight per vocabulary word. Positive weights push toward class 1 and negative ones toward class 0. The strongest negative words are close in weight, so their exact order can change with the regularization strength; the top spam word free is robust because it appears in three of the three spam messages.

Where to go next

If some exercises felt hard, revisit the lesson linked above them. If they felt easy, try the projects, for example the spam filter project, and then test yourself with the NLP interview questions.