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.
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)
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:
| grammar | sentence | PP on the noun | PP on the verb | winner |
|---|---|---|---|---|
| A, P(VP → VP PP) = 0.3 | … with ears | 0.0009072 | 0.0006804 | noun |
| A, P(VP → VP PP) = 0.3 | … with telescopes | 0.000504 | 0.000378 | noun |
| B, P(VP → VP PP) = 0.5 | … with ears | 0.000648 | 0.00081 | verb |
(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)
| combine | a cell keeps | top cell | gives |
|---|---|---|---|
| max | the best candidate + back pointer | 0.0009072 | the most likely tree |
| sum | the total of all candidates | 0.0015876 | P(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.