Natural Language Processing

Natural Language Processing (NLP) is the computational study of human language, blending linguistics, statistics, and machine learning to enable machines to understand, generate, and manipulate text and speech. Key result in words: language can be modeled probabilistically as sequences of tokens, and modern neural architectures (especially the Transformer) learn distributed representations that capture syntax and semantics from raw data. Where it applies: machine translation, search, dialogue systems, information extraction, summarization, sentiment analysis. Who/when: roots in 1950s machine translation and Shannon's information theory; revolutionized by statistical methods (1990s) and deep learning / Transformers (2013–present).

Natural Language Processing is the subfield of artificial intelligence and computational linguistics concerned with algorithmic processing of natural (human) language, encompassing both understanding (parsing, semantics, inference) and generation. Its central mathematical object is a probability distribution over sequences of discrete symbols (tokens), from which nearly all tasks can be cast as conditional inference or estimation problems. NLP matters because language is humanity's primary information medium, and automating its processing unlocks search, translation, knowledge extraction, and human–computer interaction at scale. A surprising fact is that purely self-supervised objectives — predicting the next or a masked token from massive unlabeled corpora — yield representations that transfer to virtually every downstream linguistic task, suggesting that much of "meaning" is recoverable from distributional statistics alone (the distributional hypothesis). The field has progressively shed handcrafted rules in favor of learned, high-dimensional vector representations.

The Core Insight

Language is modeled as a probability distribution over token sequences. By the chain rule of probability, this factorizes into a product of conditional next-token distributions. The entire enterprise of modern NLP reduces to estimating these conditionals well — from -gram counts to recurrent networks to self-attention — and then using the learned distribution either to score, generate, or extract structured information. The distributional hypothesis ("you shall know a word by the company it keeps," Firth 1957) provides the semantic justification: words appearing in similar contexts have similar meanings, so context-prediction objectives implicitly learn semantics.

Formal Statement

Language Model

A language model is a probability distribution over sequences of tokens drawn from a finite vocabulary . Given a sequence with each , the joint probability factorizes by the chain rule:

1
⟳Repeatedly apply the definition of conditional probability along the sequence.

Here is the context (history), is the next-token conditional, and the product runs left to right. The vocabulary may consist of words, characters, or subword units (e.g., Byte-Pair Encoding tokens).

n-gram Approximation

Under the Markov assumption of order , the full history is truncated to the previous tokens:

2
⟳Assume conditional independence of from all but the most recent tokens; the maximum-likelihood estimate is the ratio of co-occurrence counts.

The maximum-likelihood estimate for a bigram () is

3
⟳Maximize the multinomial log-likelihood subject to ; the Lagrangian yields normalized counts.

where denotes corpus counts.

Word Embeddings

A word embedding is a map assigning each token a dense vector. The skip-gram objective (word2vec) maximizes the log-probability of context words given a center word over a window of size :

4
⟳Model context prediction as a softmax over the vocabulary using separate input () and output () embeddings; the softmax is intractable, motivating negative sampling or hierarchical softmax.

Self-Attention as Weighted Information Retrieval

The Transformer's scaled dot-product attention computes, for query matrix , key matrix , and value matrix :

5
⟳The softmax row-normalizes pairwise compatibility scores into a convex combination over value vectors; the divisor keeps the dot-product variance to avoid saturated softmax gradients.

This allows every position to aggregate information from all other positions in time with sequential dependency, enabling full parallelization across the sequence — the key advantage over recurrent models.

Why It Holds / Derivation

From Cross-Entropy to Perplexity

We show that minimizing the standard training loss (cross-entropy) is equivalent to maximizing data likelihood, and that perplexity is its natural exponential form.

Step 1. Training maximizes the log-likelihood of the corpus under the model parameters :
6
⟳Take the log of the chain-rule factorization in Equation [1] and apply it across the corpus.
Step 2. Equivalently, minimize the average negative log-likelihood, the cross-entropy :
7
⟳Negate and normalize Equation [6]; this approximates the cross-entropy between the true data distribution and the model.
Step 3. Define perplexity as the exponentiated cross-entropy:
8
⟳Substitute Equation [7] into and use ; the geometric mean of inverse probabilities emerges.
Step 4. Interpretation: perplexity is the effective branching factor — the average number of equally likely next tokens. A perfect model on a deterministic sequence has ; a uniform model over tokens has . Lower perplexity means better predictive compression of the language.

Claude Shannon | 1916–2001

Though an electrical engineer and mathematician rather than a linguist, Shannon laid NLP's quantitative foundation. In A Mathematical Theory of Communication (1948) and his 1951 paper Prediction and Entropy of Printed English, he treated English as a stochastic source, estimated its entropy via human prediction experiments (~1.1 bits/character), and introduced the very notions of entropy, redundancy, and -gram models of text. His framing of language as an information source emitting symbols with measurable uncertainty is the conceptual seed from which perplexity, cross-entropy training, and the entire probabilistic-language-modeling program grew.

Building Intuition

Imagine reading a sentence and covering the last word: "I drove my car to the ___." You don't assign equal probability to every English word; "garage," "store," or "office" feel far more likely than "elephant." A language model formalizes precisely this intuition as a probability distribution. Early models (-grams) only peeked at the last few words — like predicting from a tiny window — and were brittle: unseen combinations got zero probability. Word embeddings then realized that "car" and "automobile" should share statistical strength because they appear in similar contexts, packing meaning into geometry where direction and distance encode semantic relationships. The Transformer's attention is best imagined as each word querying every other word and asking "how relevant are you to my meaning here?" — then blending in their information weighted by that relevance. "Bank" attends to "river" or "money" depending on context, resolving ambiguity dynamically. The deep stacking of these attention layers builds, layer by layer, from surface tokens to abstract meaning.

Transformer self-attention: each query attends over all keys/values to form a context-weighted output

Scaled Dot-Product Attention the river bank Linear Q, K, V QKᵀ / √dₖ softmax Σ wᵢ Vᵢ context vector attention weights for query "bank" the river bank

Worked Examples

Bigram Probability and Perplexity

Given a tiny corpus (with sentence boundaries <s> and </s>): <s> I am Sam </s> <s> Sam I am </s> <s> I do not like green eggs </s> Task: Estimate and the perplexity of the test sentence <s> I am Sam </s>. Step 1 — Counts. (positions: "I am", "I am", "I do"). . Step 2 — Bigram MLE (Equation [3]):
Step 3 — Other needed bigrams (approximate from corpus): , , . Step 4 — Test sentence probability:
Step 5 — Perplexity over transitions (Equation [8]):

Add-One (Laplace) Smoothing

Given: Same corpus, vocabulary size (I, am, Sam, do, not, like, green, eggs). We need but want to avoid zero probabilities for unseen bigrams. Step 1 — Raw counts. , . Step 2 — Laplace-smoothed estimate:
9
⟳Add a pseudocount of 1 to every possible bigram; equivalent to a uniform Dirichlet prior with concentration 1 on the multinomial.
Step 3 — Plug in:
Step 4 — Compare to unsmoothed . Smoothing redistributes probability mass to unseen events, lowering the spike on observed bigrams.

Common Misconceptions

"A language model understands meaning." A language model estimates — it is a statistical sequence predictor, not a grounded reasoner. High fluency does not imply truthfulness, comprehension, or symbolic reasoning. Large models exhibit emergent abilities, but they can also "hallucinate" plausible-sounding falsehoods because the training objective rewards likely continuations, not factual correctness. Distinguish fluency (well-formed output) from faithfulness (correctness relative to a source or the world). "More in -grams is always better." As grows, the number of possible contexts grows as , so most are never observed — the data sparsity problem. Without smoothing or backoff, large- models assign zero probability to most test sequences (infinite perplexity). The bias–variance trade-off applies: higher reduces bias but explodes variance. Neural models circumvent this by sharing parameters across contexts via continuous representations. The softmax does not preserve relative attention under length. One might assume attention weights for a fixed query stay stable as context grows. Consider a query with uniform compatibility scores over keys: each weight is . Adding more keys dilutes every existing weight, so a salient token's relative contribution can vanish in very long sequences — a known weakness ("attention dilution") that motivates sparse, local, or retrieval-augmented attention variants. Thus the convex-combination structure of Equation [5] does not guarantee scale-invariant focus.

Connections

NLP sits at the intersection of several mathematical fields. The probabilistic backbone connects to Information Theory (entropy, cross-entropy, perplexity) and Bayesian Inference (smoothing as priors, latent-variable topic models). Sequence labeling relies on Hidden Markov Models and Conditional Random Fields. Word embeddings and attention are grounded in Linear Algebra (dot products, matrix factorization — word2vec implicitly factorizes a shifted PMI matrix). Training rests on Stochastic Gradient Descent and Backpropagation. Parsing and grammar formalisms link to Formal Language Theory and Context-Free Grammars. The Transformer's representational power connects to Universal Approximation and the broader theory of Deep Learning.

Real-World Applications

  1. Google Translate / DeepL — neural machine translation using encoder–decoder Transformers translates between 100+ languages, casting translation as conditional generation .
  2. GPT-4 / ChatGPT, Claude, Gemini — large language models trained on next-token prediction power conversational assistants, code generation, and reasoning tasks.
  3. Spam filtering (Gmail) — Naive Bayes and neural classifiers estimate at billions-of-messages scale.
  4. Voice assistants (Siri, Alexa) — pipeline of automatic speech recognition, intent classification, and natural-language generation.
  5. Search engines (Google BERT integration, 2019) — contextual embeddings improve query–document matching beyond keyword overlap.
  6. Clinical NLP (cTAKES, BioBERT) — extracting diagnoses, medications, and outcomes from unstructured electronic health records.
  7. Code completion (GitHub Copilot) — language models trained on source code predict and synthesize program text.

Historical Development

  • 1948–1951 — Claude Shannon models English as a stochastic source, introduces -gram entropy estimation, founding probabilistic language modeling.
  • 1954 — Georgetown–IBM experiment demonstrates Russian–English machine translation (60 sentences), igniting MT optimism (and later disappointment via the 1966 ALPAC report).
  • 1957 — Chomsky's Syntactic Structures establishes generative grammar and formal language hierarchies, dominating symbolic NLP for decades; Firth articulates the distributional hypothesis.
  • 1970s–1980s — Rule-based and symbolic systems (SHRDLU, expert systems); Hidden Markov Models adopted for speech recognition.
  • 1990s — Statistical revolution: IBM alignment models for MT, statistical parsing, the rise of corpus-driven, probabilistic methods over hand-written rules.
  • 2003 — Bengio et al. introduce the neural probabilistic language model, learning distributed word representations.
  • 2013 — Mikolov et al. release word2vec; efficient embeddings make distributed semantics mainstream. GloVe follows in 2014.
  • 2014–2015 — Sequence-to-sequence learning (Sutskever et al.) and the attention mechanism (Bahdanau et al.) transform neural MT.
  • 2017–2019 — "Attention Is All You Need" (Vaswani et al., 2017) introduces the Transformer; BERT (2018) and GPT (2018) usher in the pretraining/fine-tuning paradigm and the era of large language models.

Subtleties & Limits

Grounding, reasoning, and scaling limits remain open. Despite remarkable fluency, current models struggle with (1) factual grounding — distinguishing learned associations from truth, leading to hallucination; (2) compositional generalization — systematically combining known parts in novel ways (the systematicity critique from Fodor & Pylyshyn); (3) long-range and quadratic cost — self-attention's scaling limits context length, spurring linear-attention and state-space alternatives; (4) interpretability — we lack a theory mapping the geometry of learned representations to formal semantic structure; and (5) data and energy efficiency — humans acquire language from orders of magnitude less data than LLMs. Whether scaling alone yields robust reasoning, or whether explicit symbolic structure, world models, or grounding in perception/action are necessary, is a central and contested open question.

See Also

Information Theory · Hidden Markov Models · Conditional Random Fields · Word Embeddings · Transformer Architectures · Large Language Models · Context-Free Grammars · Bayesian Inference · Deep Learning

Before this

  • Probability Theory — language models are distributions over token sequences; the chain rule is foundational.
  • Linear Algebra — embeddings, attention, and all neural computation operate on vectors and matrices.
  • Information Theory — entropy, cross-entropy, and perplexity define training objectives and evaluation.
  • Calculus and Optimization — gradient-based training requires differentiation and convex/non-convex optimization intuition.
  • Formal Language Theory — grammars and automata frame syntax and the limits of sequence models.

Where this leads