NLP & Text Classification

Teaching Machines to Read

Unstructured text—reviews, emails, support tickets—contains some of the most valuable signals in business. For **Netflix**, NLP is how they analyze millions of reviews to understand user sentiment. For **Visa**, it's how they automatically route fraud complaints to the right team. This chapter introduces the core of **Natural Language Processing (NLP)**.

We cover **Tokenization**, **TF-IDF** (Term Frequency–Inverse Document Frequency), **Word Embeddings**, and the **Transformer Architecture** that powers GPT, BERT, and every modern language model.

Tokenization & Text Preprocessing

Before a machine can "read," text must be broken into **Tokens** (words or subwords). For **Netflix**, this means taking a review like "Absolutely loved Stranger Things!" and converting it into numerical tokens: [1042, 856, 2391, 5]. Stop words like "the" and "a" are removed, and words are **lowercased** and **stemmed**.

PythonRuns entirely in your browser — nothing is sent to a server.

TF-IDF: Finding Important Words

**TF-IDF** measures how important a word is to a document relative to an entire collection. For **Visa**, TF-IDF is used to analyze fraud complaint text—words like "unauthorized" and "foreign" score high because they're rare in normal complaints but common in fraud reports. This allows automatic ticket routing.

PythonRuns entirely in your browser — nothing is sent to a server.

The Transformer: Attention Is All You Need

The **Transformer Architecture** revolutionized NLP. Unlike older models that read text sequentially, Transformers use **Self-Attention** to consider ALL words simultaneously, understanding context and relationships. GPT, BERT, and every modern AI language model is built on Transformers. For **Netflix**, Transformers power their multi-language content recommendation engine.

PythonRuns entirely in your browser — nothing is sent to a server.

Practice Questions

Question 1

Why is TF-IDF more useful than simple word count for Visa's complaint routing?

  • It uses less memory
  • It weighs words by both their frequency in the document AND their rarity across all documents, surfacing truly distinctive terms
  • It only works with English
  • It is faster to compute

Question 2

What is the key innovation of the Transformer's Self-Attention mechanism?

  • It reads text faster by skipping words
  • It allows the model to consider relationships between ALL words in a sentence simultaneously, regardless of distance
  • It only works with short sentences
  • It requires labeled data to function