All problems
Backoff Probability
Problem
Compute a Katz-style backoff probability for a trigram. context is [w1, w2] and token is w3.
If the key 'w1 w2 w3' is in tri_probs with a positive value, return it. Otherwise, if 'w2 w3' is in bi_probs with a positive value, return alpha times that value. Otherwise, if w3 is in uni_probs with a positive value, return alpha squared times that value. Return 0.0 when nothing matches.
Hint
Only back off when the higher-order probability is absent or not positive.
Test cases
case 1
in: [["a","b"],"c",{"a b c":0.2},{},{},0.4]
expected: 0.2
case 2
in: [["a","b"],"c",{},{"b c":0.5},{},0.4]
expected: 0.2
case 3
in: [["a","b"],"c",{},{},{"c":0.1},0.5]
expected: 0.025
case 4
in: [["a","b"],"c",{},{},{},0.5]
expected: 0
case 5
in: [["a","b"],"c",{"a b c":0},{"b c":0.5},{"c":0.9},0.5]
expected: 0.25
Starter codePython
def backoff_prob(context, token, tri_probs, bi_probs, uni_probs, alpha):
# Your code here
pass5 test cases · run your code against them in the editor