Skip to content
DeepForgeby svx
All problems
EasyNLPnlp-279

CRF Emission and Transition Score

Problem

Score a label sequence with a linear-chain CRF. tags is the label list, emissions is a list of per-position dictionaries mapping each label to its emission score, and transitions maps 'from to' keys to transition scores.

Return the sum of the emission score of each chosen tag plus the transition score of every adjacent tag pair. Missing entries contribute 0.0.

Hint

Walk the tags once, adding emissions at every position and transitions between positions.

Test cases

case 1
in: [["B","I"],[{"B":0.5,"I":-0.1},{"B":-0.2,"I":0.3}],{"B I":0.4}]
expected: 1.2000000000000002
case 2
in: [[],[],{}]
expected: 0
case 3
in: [["O"],[{"O":1.25}],{}]
expected: 1.25
case 4
in: [["A","B","A"],[{"A":0.1,"B":0.2},{"A":0.3,"B":0.4},{"A":0.5,"B":0.6}],{"A A":0.5,"A B":1,"B A":2,"B B":0.5}]
expected: 4
case 5
in: [["A"],[{}],{}]
expected: 0
Starter codePython
def crf_sequence_score(tags, emissions, transitions):
    # Your code here
    pass

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