What is NLP?
Theme
Palette
Download
Caption (tap to copy)
📖 Deep dive (full written explanation)
This fourth post is the hands-on core of the series, and the cover promises something the reader can run immediately. The emphasis on 'right now' and 'a few lines of Python' is a deliberate antidote to the assumption that NLP requires a GPU, a dataset, or a research background.
The post covers four tasks — sentiment, NER, zero-shot classification, and semantic similarity — chosen to span the understand-transform-generate landscape from the first post, so the code reinforces the conceptual map rather than wandering off into one narrow demo.
The setup slide is intentionally boring and complete, because a snippet that won't import is useless. It pins the exact packages a reader needs — transformers, sentence-transformers, and torch — and imports the two entry points the rest of the post uses.
Keeping install and import in one explicit slide follows the gold-standard pattern of front-loading environment setup. A reader can copy this, run it once, and then every subsequent snippet just works without hunting for a missing dependency.
This slide does two classic NLP tasks in a handful of lines. The sentiment pipeline scores tone, and the example ('painful but worth it') is again deliberately mixed to remind the reader that a single label compresses nuance. The NER pipeline with grouped entities extracts and merges multi-token entities into clean spans.
The value is showing how little code separates a beginner from a working classifier and entity extractor. Both pipelines download a fine-tuned model on first run, so the reader gets production-grade results without training anything — the practical proof of the previous post's 'pretrain then reuse' claim.
Zero-shot classification is the most surprising trick in the post and worth dwelling on. Normally classification requires training data with your specific labels. Zero-shot skips that entirely: you pass arbitrary candidate labels at call time and the model scores them, choosing 'technical' for a downed-internet complaint with no training on support tickets at all.
This works because the underlying model was trained on natural language inference — judging whether one sentence entails another. Reframing classification as entailment ('this text is about technical') lets one model handle label sets it has never seen, which is enormously practical for prototyping.
Here the post pivots from classification to representation by encoding sentences into vectors with sentence-transformers. The output shape (3, 384) makes the abstraction concrete: three sentences become three 384-dimensional vectors, each a point in meaning space.
This is the embeddings concept from the How It Works post, now executed. The 'all-MiniLM-L6-v2' model is chosen because it's small, fast, and good enough for most semantic-search tasks — a sensible default a reader can keep using in real projects.
Semantic search is the payoff of the embedding step and the most useful single pattern in the post. The query 'can't sign in' shares no keywords with 'I forgot my login', yet cosine similarity in vector space ranks it as the best match — because the meanings are close even though the words differ.
This is the core mechanism behind modern search, retrieval-augmented generation, and recommendation. Keyword search would miss this match entirely; semantic search catches it. Demonstrating it in five lines shows the reader they can build meaning-based retrieval without any specialized infrastructure.
The flow diagram steps back to summarize what the code accomplished: raw text enters, a pretrained pipeline turns it into a vector or label, and that machine-readable output drives a decision — routing a ticket, ranking a result, or generating a reply.
This closes the loop with the first post's definition: turning unstructured words into something a machine can act on. The reader has now done exactly that, four different ways, and the diagram ties the snippets back to the big picture.
This slide answers the question a thoughtful reader is asking: how can this possibly work with no training on my data? The answer is that each pipeline downloads a model already pretrained on enormous corpora and, for sentiment and NER, already fine-tuned for the task. Zero-shot leans on an entailment-trained model.
The framing 'you inherit millions of dollars of training for the cost of a download' is the honest economics of modern NLP. Understanding that the heavy lifting happened during someone else's pretraining is what lets the reader trust — and correctly bound — these one-line results.
The first mistake slide previews the next post by surfacing the gotcha that bites everyone: input length. Models have a maximum token window, often 512 for BERT-family models. Paste a long document into a single pipeline call and it silently truncates, analyzing only the opening chunk with no warning.
This failure is insidious precisely because nothing errors. The demo looks fine on short inputs and quietly degrades on real documents. Flagging it here, in the code post, plants the seed that the next post harvests in full.
The length-check code makes the gotcha defensible rather than theoretical. Pulling the tokenizer off the pipeline and counting input IDs lets you detect over-length text before sending it, so you can chunk it deliberately instead of losing the tail silently.
The key teaching point embedded here is that token count is not word count — sub-word tokenization means a 400-word document can exceed 512 tokens. This connects directly back to the tokenization slide in the How It Works post, reinforcing why that detail matters in practice.
The closing checklist turns the post's demos into durable working habits: prototype with pipeline(), pin model names so results are reproducible, batch inputs for throughput, watch the token limit, and cache models so you don't redownload gigabytes on every run.
These are the small operational disciplines that separate a notebook demo from something you can actually ship. They're deliberately practical, the kind of advice a reader will appreciate the first time a forgotten model pin breaks a teammate's run.
The CTA hands off to the final post on common mistakes, framing it as the failure modes that quietly wreck projects. Having shown how easy NLP is to start, the natural and responsible next move is to show where that ease becomes a trap.
The transition is honest: the library makes the easy 80% trivial, but the project-killing problems live in the other 20%, and the last post is dedicated to naming and fixing them.