The idea: give every rule a probability, then keep the best

The plain CYK algorithm answers "can the grammar build this sentence?" and, for an ambiguous sentence, finds several trees. It cannot say which tree is right. A probabilistic context-free grammar (PCFG) adds a number to every rule: how often that rule is used. A tree's probability is the product of its rules. Probabilistic CYK fills the same chart, but each cell keeps only the best probability for each nonterminal, so the top cell gives the most likely tree directly, without listing all trees.

Left: the twelve rules of the grammar with their probabilities; S → NP VP 1, VP → V NP 0.7, VP → VP PP 0.3, PP → P NP 1, NP → NP PP 0.4, NP → astronomers 0.1, ears 0.18, saw 0.04, stars 0.18, telescopes 0.1, V → saw 1, P → with 1. VP rules sum to 0.7 + 0.3 = 1, NP rules to 1. Right: the tree of astronomers saw stars with ears with the PP under the noun, each node marked with its rule probability; the product is 0.0009072.
The rules of each left side share a probability of 1; a tree's probability is the product of the rules it uses.

A PCFG in Chomsky normal form

The page uses the classic example from Manning and Schütze. Every rule has the form A → B C or A → word (Chomsky normal form, as on the CYK page). The number P(A → β) is the probability that an A is expanded with this rule, so the rules with the same left side sum to 1: VP → V NP (0.7) and VP → VP PP (0.3); the six NP rules 0.4 + 0.1 + 0.18 + 0.04 + 0.18 + 0.1. The grammar panel prints both sums. Where do the numbers come from? Usually from a treebank: count how often each rule appears and divide by how often its left side appears.

Note that "saw" is both a verb (V → saw, 1) and a noun (NP → saw, 0.04). The bottom cell of "saw" holds both, each with its own number: NP 0.04 · V 1.

One cell keeps the maximum

Write π[l, s][A] for the highest probability of any tree of A over the l words starting at word s. For one word, π[1, s][A] = P(A → word). For a longer span, every split k and every rule A → B C gives a candidate:

P(A → B C) × π[k, s][B] × π[l − k, s + k][C]

The cell keeps the largest candidate for A and a back pointer (the split and the rule that produced it). In "astronomers saw stars with ears" the cell over "saw stars with ears" sees two VP candidates: split 1 gives VP → V NP, 0.7 × 1 × 0.01296 = 0.009072; split 2 gives VP → VP PP, 0.3 × 0.126 × 0.18 = 0.006804. The second one is thrown away right there. (Demo: each cell keeps the max)

The cell π[4,2] over saw stars with ears gets two VP candidates: split k = 1, VP → V NP, 0.7 × 1 × 0.01296 = 0.009072, and split k = 2, VP → VP PP, 0.3 × 0.126 × 0.18 = 0.006804. Max mode keeps 0.009072 with back pointer k = 1 and drops 0.006804. Sum mode adds them to 0.015876 and keeps no back pointer.
The two attachments meet in one cell: max keeps 0.009072 and its back pointer, sum adds both to 0.015876.

Why throwing candidates away is safe

Suppose the best tree of the whole sentence used a VP over "saw stars with ears" that was not the best VP for those words. Swap in the best one: the rest of the tree is unchanged, and the product only gets bigger. So the best tree is made of best sub-trees (optimal substructure), and keeping one number per nonterminal per cell loses nothing. This is the same argument as in the Viterbi algorithm for hidden Markov models; probabilistic CYK is "Viterbi on a tree" instead of on a chain. The work is the same as plain CYK: O(n³ · |G|).

Reading back the best tree

When the chart is full, π[n, 1][S] is the probability of the best tree: 0.0009072 for the example. Follow the back pointer of S to its two children, then theirs, down to the words. The page draws the tree one depth per Step and lights the cell of every node. The tree's product, 0.1 × 0.7 × 0.4 × 0.18 × 0.18 (the rules of probability 1 left out), equals the top cell, because each cell on the way kept exactly this tree's pieces. (Demo: short sentence gives one tree, 0.1 × 0.7 × 0.18 = 0.0126.)

PP attachment as two numbers

"with ears" can belong to the stars (stars that have ears) or to the seeing (seeing by means of ears). The grammar allows both, which is the ambiguity the CYK page counts. With probabilities, the two trees just compare:

Tree 1 attaches the PP to the noun with NP → NP PP: 0.1 × 0.7 × 0.4 × 0.18 × 0.18 = 0.0009072, the best. Tree 2 attaches the PP to the verb phrase with VP → VP PP: 0.1 × 0.3 × 0.7 × 0.18 × 0.18 = 0.0006804. Only one node differs; all word rules are shared.
The noun attachment wins, 0.0009072 to 0.0006804: the two trees differ in one rule, NP → NP PP (0.4) against VP → VP PP (0.3).
grammarsentencePP on the nounPP on the verbwinner
A, P(VP → VP PP) = 0.3… with ears0.00090720.0006804noun
A, P(VP → VP PP) = 0.3… with telescopes0.0005040.000378noun
B, P(VP → VP PP) = 0.5… with ears0.0006480.00081verb

(Demo: both trees compared, Demo: P(VP → VP PP) = 0.5 flips)

What the words cannot change

"astronomers saw stars with telescopes" should attach the PP to the seeing. The PCFG still picks the noun, with the same ratio 0.4 : 0.3. Both trees contain the same word rules (NP → telescopes is in each of them), so the words cancel out, and only NP → NP PP against VP → VP PP decides. A plain PCFG is blind to which words are involved. Lexicalized parsers fix this by putting the head word into each nonterminal (VP(saw), NP(stars)), so that "saw … with telescopes" can get its own probability. (Demo: with telescopes)

Max or sum: the inside probability

Replace max with sum and the same chart computes the inside probability: the total probability of all trees of A over a span. The top cell is then P(sentence) = 0.0009072 + 0.0006804 = 0.0015876, and the best tree takes 0.0009072 / 0.0015876 ≈ 57% of it. The sum is what a language model needs, and what training a PCFG from raw text (the inside-outside algorithm) is built on. It is the same switch as between the Viterbi and forward algorithms on an HMM; see also POS tagging with an HMM. (Demo: sum instead of max)

combinea cell keepstop cellgives
maxthe best candidate + back pointer0.0009072the most likely tree
sumthe total of all candidates0.0015876P(sentence)

Probabilistic parsing also works with other chart parsers: see the Earley parser, which does not need Chomsky normal form.

What the page leaves out

The grammar and the three sentences are fixed presets; the page does not convert a grammar to CNF or estimate probabilities from a treebank. It leaves out unary rules and their closure inside a cell, working with log probabilities to avoid underflow on long sentences, beam pruning, the outside probabilities and the inside-outside training algorithm, lexicalized and latent-variable PCFGs, and neural constituency parsers.

On the mind map

This page on the Dynamic Programming mind map · 36 pages in 8 branches · open it to see the whole map and test yourself in recall mode.