All problems
PCFG Rule Probability Normalization
Problem
Normalize rule counts into PCFG probabilities per left-hand side. Keys are rules formatted as 'LHS -> RHS' and values are their counts.
For each rule return count divided by the total count of all rules sharing the same LHS, so every LHS distribution sums to 1.
Hint
The left-hand side is the first space-separated token of the rule string.
Test cases
case 1
in: [{"S -> A B":3,"S -> C":1,"A -> a":2}]
expected: {"S -> A B":0.75,"S -> C":0.25,"A -> a":1}
case 2
in: [{}]
expected: {}
case 3
in: [{"X -> y":5}]
expected: {"X -> y":1}
case 4
in: [{"S -> A":2,"S -> B":2,"T -> c":1}]
expected: {"S -> A":0.5,"S -> B":0.5,"T -> c":1}
Starter codePython
def pcfg_normalize(counts):
# Your code here
pass4 test cases · run your code against them in the editor