Skip to content
DeepForgeby svx
All problems
MediumNLPnlp-025

Bigram Probabilities with Laplace Smoothing

Problem

Build add-one smoothed bigram probabilities from a token list. For every observed bigram (a, b) compute P(b | a) = (count(a, b) + 1) / (count(a) + V), where V is the number of distinct tokens in the corpus.

Return a dictionary keyed by 'a b'. A corpus shorter than two tokens yields an empty dictionary.

Hint

The denominator uses the count of the first token plus the smoothed vocabulary size.

Test cases

case 1
in: [["a","b","a"]]
expected: {"a b":0.5,"b a":0.6666666666666666}
case 2
in: [["the","cat","the","dog"]]
expected: {"the cat":0.4,"cat the":0.5,"the dog":0.4}
case 3
in: [["a","a","a"]]
expected: {"a a":0.75}
case 4
in: [["x"]]
expected: {}
Starter codePython
def bigram_probs_laplace(tokens):
    # Your code here
    pass

4 test cases · run your code against them in the editor