All problems
Term Frequency
Problem
Compute relative term frequencies for a document.
Return a dict mapping each unique token to count(token) / len(tokens). An empty token list returns an empty dict.
Hint
Counting first and dividing once is simpler than dividing per token.
Test cases
case 1
in: [["a","a","b"]]
expected: {"a":0.6666666666666666,"b":0.3333333333333333}
case 2
in: [[]]
expected: {}
case 3
in: [["x"]]
expected: {"x":1}
case 4
in: [["a","b","a","b"]]
expected: {"a":0.5,"b":0.5}
Starter codePython
def term_frequency(tokens):
# Your code here
pass4 test cases · run your code against them in the editor