Skip to content
DeepForgeby svx
All problems
MediumNLPnlp-027

Bigram Log-Likelihood of a Sentence

Problem

Return the bigram log-likelihood of a token sequence. The first token is conditioned on the start symbol '<s>' using the key '<s> token', and each later token uses the key 'previous token'.

Sum math.log of the looked-up probabilities. Treat a missing entry as probability 0 and return float('-inf'). Return 0.0 for an empty sequence.

Hint

Update the context to the token you just scored before the next lookup.

Test cases

case 1
in: [["a","b"],{"<s> a":1,"a b":0.5}]
expected: -0.6931471805599453
case 2
in: [["the","cat"],{"<s> the":0.5,"the cat":0.1}]
expected: -2.995732273553991
case 3
in: [[],{}]
expected: 0
case 4
in: [["x"],{"<s> x":1}]
expected: 0
Starter codePython
import math
def log_likelihood(tokens, probs):
    # Your code here
    pass

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