Learn / NLP & Practical LLMs / Module 01
NLP & Practical LLMs · Module 01
From Raw Text to Tokens and Embeddings
Understand how NLP pipelines break text into tokens, clean noise, and convert sentences into sparse TF-IDF or dense vector representations.
<!-- Thompson Axiomata LLC — Brent Alexander Thompson -->
Computers do not read sentences—they read IDs and vectors
Before any search engine, classifier, or large language model can work on text, it runs two foundational steps:
- Tokenization: split a raw string into discrete pieces (tokens).
- Representation: map those tokens into numbers that preserve frequency or meaning.
Step 1: Tokenization & normalization
Splitting on whitespace (text.split()) breaks immediately on punctuation, contractions, URLs, and domain codes like ISO/IEC-21838-2.
| Strategy | How it splits "unbelievable" | Where it is used |
|---|---|---|
| Rule / linguistic tokenizer (e.g., spaCy) | ["unbelievable"] plus POS tags & lemmas | Classical NLP, NER, deterministic parsing |
| Subword tokenizer (e.g., BPE / WordPiece) | ["un", "believ", "able"] | Modern Transformers and LLMs |
Subword tokenization (like Byte-Pair Encoding) solves two problems at once: vocabulary stays compact (often 32k–128k tokens), and rare or brand-new technical terms never collapse into an unknown <UNK> token.
Step 2: Sparse vs. dense representations
Once text is tokenized, there are two classic ways to turn a document into a vector:
- Sparse vectors (Bag-of-Words & TF-IDF): one dimension per vocabulary word. TF-IDF boosts words that appear often in this document but rarely across the rest of the corpus. It is fast, exact on IDs and part numbers, and needs no GPU.
- Dense embeddings: a neural encoder maps a whole sentence or chunk into a fixed-length vector (for example, 384 or 768 floats) where semantically similar phrases land near each other—even if they share zero exact words.
import math
def cosine_similarity(a: list[float], b: list[float]) -> float:
dot = sum(x * y for x, y in zip(a, b))
norm_a = math.sqrt(sum(x * x for x in a))
norm_b = math.sqrt(sum(y * y for y in b))
return dot / (norm_a * norm_b)
# Conceptual toy 3D vectors: [ontology, solar, cooking]
doc_kg = [0.92, 0.35, 0.02]
query_owl = [0.89, 0.28, 0.01]
doc_pasta = [0.01, 0.04, 0.98]
print("KG vs OWL:", round(cosine_similarity(doc_kg, query_owl), 3))
print("KG vs Pasta:", round(cosine_similarity(doc_kg, doc_pasta), 3))<!-- Thompson Axiomata LLC — Brent Alexander Thompson -->
Finished this module?
Mark it complete to update your browser-local progress bar for NLP & Practical LLMs.