Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding conditional probability of trigram in python nltk

I have started learning NLTK and I am following a tutorial from here, where they find conditional probability using bigrams like this.

import nltk
from nltk.corpus import brown
cfreq_brown_2gram = nltk.ConditionalFreqDist(nltk.bigrams(brown.words()))

However I want to find conditional probability using trigrams. When I try to change nltk.bigrams to nltk.trigrams I get the following error.

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "home/env/local/lib/python2.7/site-packages/nltk/probability.py", line 1705, in __init__
    for (cond, sample) in cond_samples:
ValueError: too many values to unpack (expected 2)

How can I calculate the conditional probability using trigrams?

like image 634
Riken Shah Avatar asked Jun 28 '16 06:06

Riken Shah


1 Answers

nltk.ConditionalFreqDist expects its data as a sequence of (condition, item) tuples. nltk.trigrams returns tuples of length 3, which causes the exact error you posted.

From your post it's not exactly clear what you want to use as conditions, but the convention when doing language modeling is to condition the last word on its predecessors. The following code demonstrates how you'd implement that.

brown_trigrams = nltk.trigrams(brown.words())
condition_pairs = (((w0, w1), w2) for w0, w1, w2 in brown_trigrams)
cfd_brown = nltk.ConditionalFreqDist(condition_pairs)
like image 177
Ilia Kurenkov Avatar answered Oct 13 '22 02:10

Ilia Kurenkov