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