All problems
Term Frequency
Problem
Return the term frequency of token in doc. With log_tf=False return the raw count as a float; with log_tf=True return 1 + math.log(count).
A token that never appears yields 0.0.
Hint
The log variant dampens repeated counts with 1 + log(count).
Test cases
case 1
in: ["cat",["the","cat","sat","on","the","cat"],false]
expected: 2
case 2
in: ["dog",["the","cat"],false]
expected: 0
case 3
in: ["cat",["cat","cat","cat"],true]
expected: 2.09861228866811
case 4
in: ["the",["the","cat","the"],true]
expected: 1.6931471805599454
case 5
in: ["a",[],false]
expected: 0
Starter codePython
import math
def tf(token, doc, log_tf=False):
# Your code here
pass5 test cases · run your code against them in the editor