Bigram probability

Viewed 1077

I have a Moby Dick Corpus and I need to calculate the probability of the bigram "ivory leg." I know that this command gives me the list of all bigrams

bigrams = [w1+" "+w2 for w1,w2 in zip(words[:-1], words[1:])]

But how do I get the probability of just the two words?

1 Answers

You can count all the bigrams and count the specific bigram you are looking for. The probability of the bigram occurring P(bigram) is jut the quotient of those. The conditional probability of word[1] give word[0] P(w[1] | w[0]) is the quotient of the number of occurrence of the bigram over the count of w[0]. For example looking at the bigram ('some', 'text'):

s = 'this is some text about some text but not some other stuff'.split()

bigrams = [(s1, s2) for s1, s2 in zip(s, s[1:])]

# [('this', 'is'),
#  ('is', 'some'),
# ('some', 'text'),
# ('text', 'about'),
# ...

number_of_bigrams = len(bigrams)
# 11

# how many times 'some' occurs 
some_count = s.count('some')
# 3

# how many times bigram occurs
bg_count = bigrams.count(('some', 'text'))
# 2

# probabily of 'text' given 'some' P(bigram | some)
# i.e. you found `some`, what's the probability that its' makes the bigram:
bg_count/some_count
# 0.666

# probabilty of bigram in text P(some text)
# i.e. pick a bigram at random, what's the probability it's your bigram:
bg_count/number_of_bigrams
# 0.181818
Related