CAS logo CAS NLP LABInteractive Tutorial Series ← Series

⌗  TEXT REPRESENTATION — How Words Become Math

From Tokens to Vectors

Every NLP model you have met so far — the RNN of Lecture 02, the LSTM of Lecture 03 — silently assumed its input was already numbers. This lecture fills in that assumption: the eight rungs of the ladder that turn raw text into geometry a network can learn from. Each rung fixes exactly one wound of the rung before it — and keeps one of its own.

8Representations1960s → 2016, each fixing the last
12Sectionsevery one interactive
10+Live Demostype, drag, click — nothing is a picture
8Quiz Checksone per rung of the ladder
THE JOURNEY, IN ONE SENTENCE

We start with a string of characters and end with dense vectors where direction means meaning — passing through integers, one-hot islands, count vectors, weighted counts, and finally learned representations where king − man + woman ≈ queen is just arithmetic. Transformers (Lecture 05) begin exactly where this page ends.

ONE SENTENCE, EIGHT LIVES

“The keys to the cabinet are on the table.” — the same probe sentence from Lecture 03. This time we won't feed it to a memory cell; we'll watch it get encoded, rung by rung, and feel what each encoding preserves and what it throws away.

Nothing here is a screenshot. Every table, grid and map on this page is computed live from the text in front of you — edit any input and the math follows.

RUNG 1 · WORD TOKENS ↓

THE LADDER'S LOGIC

text → ids → sparse → counts → weighted → dense

The whole history of pre-transformer NLP is these four pairs. Keep this map in your head all the way down.

Before any math, a decision that seems trivial and isn't: what is one "unit" of language? Letters? Words? Syllables? Rung 1 makes the call that everything else inherits.

02 RUNG 1 · WORD TOKENS

Break language into units

A model never sees a sentence — it sees a list. Tokenization is the act of chopping raw text into pieces called tokens, and the simplest useful chop is by words. Type below; the chips update live.

TOKEN STREAM · 0 tokens

0characters in
tokens out (see counter above)
0unique types
type/token ratio — lexical variety

Notice three things: “The” and “the” — case would double-count them, so real pipelines lowercase (this demo does). Punctuation — sometimes a token, sometimes noise; toggle it and watch the count change. Words like “don't” — the apostrophe rule here keeps it whole, but there is no single correct answer. Tokenization is a design choice, and modern subword tokenizers (Lecture 05) revisit it entirely.

Tokens are still strings — and strings can't be multiplied by weights. Next: give every word a number. Any number. It will matter that it doesn't matter which.

03 RUNG 2 · TOKEN IDS

Words become numbers — meaningless ones

Build a vocabulary: every unique word gets an integer ID. The IDs are arbitrary labels — shuffle them and nothing about the language changes. That arbitrariness is the whole point of this rung.

VOCABULARY · built from the probe sentence

THE SENTENCE, ENCODED

Shuffle the IDs: the encoding changes completely, yet carries exactly the same information. Conclusion: the integer itself holds no meaning — it is a name tag, not a measurement.

type a word…

The wound: a network fed raw IDs learns that word 7 is “between” word 6 and word 8 — an ordering that does not exist. “cat” (ID 3) is not closer to “cabinet” (ID 4) than to “table” (ID 9). We must encode IDs so that no fake geometry is implied.

The cure for fake ordering is brutally democratic: give every word its own axis, equally far from all others. Democratic — and ruinously expensive.

04 RUNG 3 · ONE-HOT ENCODING

Every word an island

A one-hot vector is as long as the vocabulary, all zeros except a single 1 at the word's ID. No word is “between” two others anymore — every pair is exactly the same distance apart. Pick a word and see its vector.

VOCABULARY AXES · one column per word

VOCABULARY SIZE — slide from a sentence to a language100 words
100dimensions per word vector
99.0%of every vector is zeros
400 KBone 1,000-token document, float32

DISTANCE LAB · how similar are two one-hot words?

vs

The wound: “cat” and “dog” are exactly as far apart as “cat” and “democracy” — dot product 0, distance √2, always. One-hot removed the fake geometry by refusing to have any geometry. Add the size problem (50,000-dim vectors that are 99.998% zeros) and this can't be the final answer.

Words are vectors now — but documents are what we actually classify, search and compare. The cheapest way to vectorize a document: pour its words into a bag and count.

05 RUNG 4 · BAG OF WORDS

Documents as counts — order dies here

Bag-of-Words represents a document as the sum of its words' one-hot vectors — one vocabulary-length row of counts. The classic demo of the price: two sentences, opposite meanings, identical vectors. Edit them and watch.

COUNT MATRIX · shared vocabulary

And yet — BoW powered decades of spam filters, search engines and topic classifiers, and still wins benchmarks as a baseline. Counts carry topic even when they lose meaning. Never skip the baseline.

Order matters — at least locally. “not good” ≠ “good”. The patch: tokenize into short word sequences instead of single words. It works. It also blows up.

06 RUNG 5 · N-GRAMS

Stealing back local order

An n-gram is a window of n consecutive tokens. Bigrams catch “New York” and “not good”; trigrams catch “United States of”. Slide n and watch the units change — then look at what n does to the vocabulary.

WINDOW SIZE n1 (unigrams)

N-GRAM STREAM · 0

VOCABULARY EXPLOSION · unique n-grams in a 22-word corpus

On real corpora a 50k-word vocabulary holds ~2.5 billion possible bigrams — almost all rare or unseen. More features, less data per feature: the curse of dimensionality in its natural habitat.

The wound: n-grams capture local order only — “the movie was not particularly… good” still defeats a bigram. And “good movie” vs “great film” share zero n-grams: still no notion that two different units can mean similar things.

Counting treats every word as equally informative. But “the” appears everywhere and says nothing, while “saturn” appears twice and says everything. Time to weigh the counts.

07 RUNG 6 · TF-IDF

Which words matter?

Term Frequency rewards words used often in this document; Inverse Document Frequency punishes words used in every document. Their product finds the words that characterize a text. Three mini-documents below — every number is hand-checkable.

tfidf(w, d) = count(w, d) / len(d)  ×  ln( N / df(w) ) TF: how often here · IDF: how rare everywhere · N = number of documents · df = documents containing w

The wound: TF-IDF knows importance, not meaning. “car” and “automobile” are different features with different weights; synonyms never meet, and “not good” still scores “good” positively. Counting — however cleverly weighted — has run out of road.

Every method so far counts. The revolution of 2013: stop counting and learn — let a small network discover that words used in similar contexts should live at similar coordinates.

08 RUNG 7 · WORD2VEC & GLOVE

Meaning as geometry

“You shall know a word by the company it keeps” (Firth, 1957). Word2Vec (Mikolov et al., 2013) trains a tiny network to predict context words — and the learned weights turn out to place similar words at similar coordinates. GloVe (2014) reaches the same geometry by factorizing global co-occurrence counts. Below: a hand-placed 2D map that behaves like the real 300-D space.

THE EMBEDDING MAP · hover or tap any word — its 3 nearest neighbors light up

Hover a word to inspect its neighborhood…

HOW SKIP-GRAM LEARNS · slide the center word — pairs (center, context) are the training data

CENTER WORDfox

The network adjusts the center word's vector to better predict each context word. Words that predict the same contexts — “cat” and “dog” both sit near “pet”, “feed”, “barked” — get pulled to the same region. The task is an excuse; the vectors are the treasure.

THE FAMOUS ARITHMETIC · relationships are directions

The solid arrow and the dashed arrow are the same displacement — gender, royalty, capital-of all become consistent directions. This is the property no counting method ever had: structure you can do algebra on.

The wound: one vector per word, frozen after training. “bank” gets a single point whether the sentence is about rivers or money. And a word never seen in training — a typo, a new coinage, “unhappiness” — gets nothing at all: out-of-vocabulary is fatal.

Word2Vec treats each word as an indivisible atom. But words aren't atoms — they're built from pieces, and the pieces carry meaning. FastText (2016) splits the atom.

09 RUNG 8 · FASTTEXT

Words are made of pieces

FastText (Bojanowski et al., 2016) represents each word as the sum of its character n-grams — “playing” becomes <pla, play, layi, ying, ing>, …. A word's vector is built from its subwords, so unseen, rare and misspelled words still get meaningful vectors. Type two words; shared subwords highlight.

SUBWORDS OF WORD 1 · character n-grams, n = 3…5, with < > boundaries

SUBWORDS OF WORD 2 · highlighted = shared with word 1

The < and > boundary markers let the model distinguish the prefix “un-” in “<un” from the “un” inside “bundle”. Morphology — prefixes, suffixes, roots — becomes shared structure: “playing” and “playful” borrow each other's vectors, and even “kingdoms” (never seen in training) inherits from “kingdom” + “-s”.

The wound: still one static vector per word — “bank” remains one point. The fix requires vectors that change with the sentence: contextual embeddings. That is the transformer story, and it is Lecture 05.

10 SUMMARY

The eight-step ladder, whole

Every rung fixed exactly one wound and kept one of its own. Read the table top to bottom — it is the entire pre-transformer history of text representation in eight rows.

#RepresentationWhat it addsWhat it still misses
1Word tokensBreak language into unitsStill text — no math possible
2Token IDsConvert words to numbersIDs have no meaning — fake ordering
3One-hotRemove artificial orderingHuge + sparse — no similarity at all
4BoWRepresent documentsLoses order
5N-gramsCapture local orderVocabulary explosion
6TF-IDFIdentify important wordsWeak semantics — synonyms never meet
7Word2Vec / GloVeLearn semantic vectorsOne vector per word — no polysemy, OOV fatal
8FastTextLearn subword informationStill mostly static — context comes next
01 · The pattern

Each representation is a repair of the previous one. When you meet a new encoding, ask two questions: what did it fix, and what did it keep broken?

02 · Counting vs learning

Rungs 1–6 count; rungs 7–8 learn. Counting is transparent and free; learning discovers structure no one counted — at the price of opacity and training data.

03 · Sparse vs dense

One-hot/BoW/TF-IDF vectors are vocabulary-long and mostly zeros. Embeddings are 100–1000 dense floats where every dimension works. Dense is what neural networks were built to eat.

04 · Geometry is meaning

In a trained embedding space, similarity is distance and relationships are directions. king − man + woman ≈ queen is not a party trick — it is evidence the space has structure.

05 · Subwords win

FastText's character n-grams handle rare words, typos and morphology. Modern tokenizers (BPE, WordPiece, SentencePiece — Lecture 05) are this idea industrialized.

06 · What comes next

All eight rungs produce static vectors. The transformer makes each vector a function of its sentence — “bank” by a river ≠ “bank” in a vault. The ladder continues in Lecture 05.

11 CODE LAB

Every rung, in runnable Python

Three tabs: the whole ladder in twelve lines of standard library, TF-IDF the way it's done at work, and Word2Vec + FastText training on a toy corpus — including the OOV trick Word2Vec can't do.

TEXT REPRESENTATION · THREE WAYS

12 MASTERY CHECK

Eight questions — one per rung.

YOUR SCORE

0 / 8