Teaching NLP with Python
Explore how text moves from raw language to tokens, vectors, search results, and a simple classifier. Each interaction is paired with readable Python so the method stays visible rather than disappearing behind the output.
No setup required. The demonstrations run in this page and mirror the computational steps shown in the accompanying Python examples. Visitors can explore the concepts without installing Python or opening a notebook.
The pipeline students build
The next three tabs walk through the middle of this pipeline. Each one runs in your browser as a JavaScript re-creation of what the Python code does, so you can change inputs and see results without installing anything.
Libraries by job
NLTK
The classic teaching library: tokenizers, stemmers, stopword lists and corpora. Good for showing each step by hand.
spaCy
Production-grade pipelines for tokens, lemmas, part-of-speech tags and named entities in a few lines.
scikit-learn
CountVectorizer, TfidfVectorizer and classifiers with one consistent fit/predict interface.
Hugging Face transformers
Pretrained models through pipeline("sentiment-analysis"). Shows students what modern NLP builds on.
gensim
Word embeddings and topic models for exploring meaning as geometry.
Jupyter and Colab
Notebooks mix code, output and notes, and Colab removes install problems for the whole class.
A four-week sample unit
Week 1: Text as data
Strings, regular expressions, re.findall, word counts with collections.Counter. Goal: count words in a short text by hand.
Week 2: Vectors and similarity
Bag of words, TF-IDF, cosine similarity. Goal: build a tiny search engine over ten documents.
Week 3: Classification
Naive Bayes and logistic regression on labeled reviews, with train/test splits and accuracy. Goal: explain why a model made a prediction.
Week 4: Pretrained models and ethics
Run a transformer pipeline, compare it with the week 3 model, and examine bias in training data.
Teaching tip: make students predict which words will matter before they see the model's weights. Wrong guesses make the lesson stick.
Preprocessing playground
Edit the text, toggle each step, and see which tokens survive. Struck-through chips were removed by a step.
Try this: turn stemming on. "running" and "runs" merge into one token, which helps counting but can blur meaning ("university" and "universe" can collide in real stemmers). Here a simple suffix rule stands in for NLTK's Porter stemmer.
A tiny search engine
Six documents are turned into TF-IDF vectors. Type a query and the documents are ranked by cosine similarity, exactly what scikit-learn computes.
Show the corpus
Discuss: search for "football pasta". Why does the engine return results at all, and why are the scores low? Words that appear in many documents carry little weight, and shared words are the only bridge.
Sentiment classifier (Naive Bayes)
A model trained on 16 short reviews. Type a review to see the probability it is positive, and which words pushed the decision.
Show the training data
Discuss: the model has never seen "mediocre". What does it do with unseen words, and why does the smoothing setting matter for them? Also try "not good": a bag of words ignores word order, so negation is a known weakness.
Quick check
Four questions with instant feedback, suitable for a notebook warm-up.
Score: 0 / 4
Interactive lesson + applied capstone · 5–10 minutes
Context Before Count
Learning objective: Use keyword-in-context (KWIC) evidence to explain why a word count alone cannot establish a speaker’s attitude.
Choose a term, inspect the highlighted words, and compare the surrounding language. These three passages are invented for instruction. The exercise uses JavaScript to demonstrate the KWIC step of my Python Corpus Explorer; it does not execute Python, spaCy, TF-IDF, or sentiment analysis.
Apply it: write an evidence-based interpretation
Using the examples above, identify the source IDs, quote the words that shape your interpretation, and state one limit of the count. For the full capstone, repeat this process with three short, rights-cleared passages: preserve source IDs, record each occurrence and its context, consider a competing interpretation, and explain what additional evidence your claim would need.
Your response stays in this page’s memory until you leave or reload. This exercise does not transmit or save it. Download a copy to keep it. Use the synthetic passages here; do not enter personal or confidential information.
Capstone assessment rubric
Score each criterion from 0 to 2 (8 points total). Use the descriptors to revise; the page does not automatically grade written work.
Traceable evidence
2: Each quoted context has a matching source ID. 1: Some IDs or quotations are incomplete. 0: Evidence cannot be traced.
Contextual interpretation
2: Explains how contrasting language or negation affects meaning. 1: Notices a difference without explaining it. 0: Treats occurrences as equivalent.
Competing explanation
2: Tests a plausible alternative against the passages. 1: Names an alternative without testing it. 0: Gives no alternative.
Limits of the claim
2: Distinguishes count from interpretation and identifies needed evidence. 1: Gives a general caution. 0: Claims more than the passages support.
Designed by Laura Elizabeth Hand. Adapted from the KWIC routine in 1_corpus_explorer.py (Python Corpus Explorer teaching artifact). Synthetic examples and this assessment are new instructional materials.