NLP Unit-Iii
NLP Unit-Iii
Algorithm Steps:
• Input: A sentence to be parsed.
• Generate Parse Trees: Using CFG rules, generate all possible parse trees for
the sentence.
• Compute Probabilities:
• Multiply the probabilities of each rule used in a parse tree.
• The probability of a parse tree T is:
• where P(Ri) is the probability of each production rule applied in the tree.
• Select the Most Probable Parse Tree: The tree with the highest probability is
chosen as the correct parse.
Real-Time Example of PCFG Parsing
Step1: Sentence:
java
Aspect "chased" "slept"
S → NP VP [1.0]
NP → Det N [0.6] Intransitive
Transitive
NP → N [0.4] Action verb (no
verb (needs
Type object
VP → V NP [0.7] an object)
needed)
VP → V [0.3] 60% (More 40% (Less
Probability
Det → "the“ [1.0] frequent) frequent)
N → "dog" [0.5] Requires a
N → "cat" [0.5] subject and an Only needs a
Sentence
V → "chased" [0.6] object subject (The dog
Structure
V → "slept" [0.4]
(The dog chased slept)
the cat)
Generate Possible Parse Trees
Generated Sentence:
✅ "The dog chased cat"
Now, let's compute the probability of generating this sentence:
1.S → NP VP = 1.0
2.NP → Det N = 0.6
3.Det → "the" = 1.0
4.N → "dog" = 0.5
5.VP → V NP = 0.7
6.V → "chased" = 0.6
7.NP → N = 0.4
8.N → "cat" = 0.5
Total probability =1.0 × 0.6 × 1.0 × 0.5 × 0.7 × 0.6 × 0.4 × 0.5 = 0.0252 (or
Total probability = 2.52%)
Final Sentence & Probability
Calculation (Example 2)
Let's generate a different sentence following another probabilistic path.
Step 1: Start with S → NP VP
This is always 1.0.
Step 2: Expand NP
We choose NP → N (40% probability).
•N → "cat" (50% probability).
So, NP = "cat".
Step 3: Expand VP
We choose VP → V (30% probability).
V → "slept" (40% probability).
So, VP = "slept".
Final Sentence
✅ "Cat slept"
Probability Calculation
1.S → NP VP = 1.0
2.NP → N = 0.4
3.N → "cat" = 0.5
4.VP → V = 0.3
5.V → "slept" = 0.4
Total probability =1.0 × 0.4 × 0.5 × 0.3 × 0.4 = 0.024 (or 2.4%)
Comparison of Sentence Probabilities
Comparison of Sentence
Probabilities
Sentence Probability
S [0.9]
Sentence: "The dog barks." / \
Possible Parse Trees and Their Probabilities: NP VP [0.7]
/ \ |
Tree 1 (Correct Structure) Det N V
| | |
The dog barks
Probability Calculation:
• P(S → NP VP) = 0.9
• P(NP → Det N) = 0.6
• P(Det → "The") = 1.0
• P(N → "dog") = 0.8
• P(VP → V) = 0.3
• P(V → "barks") = 1.0
Total Probability = 0.9 × 0.6 × 1.0 × 0.8 × 0.3 × 1.0 = **0.1296**
Example 2: HMM-Based Parsing (Sequence Prediction)
Sentence: "I saw a man with a telescope."
Goal: Predicting part-of-speech (POS) tags.
HMM States (POS tags):
•"I" → Pronoun
•"saw" → Verb
•"a" → Determiner
•"man" → Noun
•"with" → Preposition
•"a" → Determiner
•"telescope" → Noun
HMM predicts the most likely sequence of tags and disambiguates sentence meaning based on
transition probabilities.
Example 3: Neural Generative Parsing (Transformer-based)
A neural sequence-to-sequence model (like GPT) can generate syntactic parse trees.
Input Sentence: "She enjoys playing chess."
Generated Parse Tree (Dependency Format):
enjoys
/ \
She playing
|
chess