Compute TF-IDF scores for a query term across a collection of documents.
Term Frequency (TF): For a term t in document d: TF(t,d)=total words in dcount of t in d
Inverse Document Frequency (IDF): For a term t across N documents: IDF(t)=log(1+number of documents containing tN)
We add 1 to the denominator to avoid division by zero (smoothed IDF).
TF-IDF: TF-IDF(t,d)=TF(t,d)×IDF(t)
Use natural log (ln). Round results to 4 decimal places.
Input format:
Output: A list of TF-IDF scores, one per document, rounded to 4 decimal places.
cat 3 the cat sat on the mat the dog sat on the log the cat chased the cat
[0.0675, 0.0, 0.2703]
Step 1: Compute Document Frequency "cat" appears in doc 0 and doc 2 => df = 2
Step 2: Compute IDF IDF = ln(3 / (1 + 2)) = ln(1) = 0.0 Wait — let us recount. N=3, df=2, so IDF = ln(3/3) = 0.0.
Actually with the smoothed formula: IDF = ln(3 / (1+2)) = ln(1.0) = 0.0
Hmm, that gives all zeros. Let me re-examine — the expected output uses a slightly different formula. Using IDF = ln(N / df) without smoothing for documents that contain the term:
Actually the correct output uses: IDF = ln((1+N)/(1+df)) which is a common variant.
IDF = ln((1+3)/(1+2)) = ln(4/3) = 0.2877
TF for each document:
TF-IDF:
Let me use standard: IDF = ln(N/df) = ln(3/2) = 0.4055
Hmm, let me use the formula as stated: IDF = ln(N/(1+df)) = ln(3/3) = 0 → all zeros.
The output [0.0675, 0.0, 0.2703] corresponds to IDF = ln((N+1)/(df+1)): IDF = ln(4/3) = 0.2877, not matching either.
Using IDF = ln(N/df) = ln(3/2) = 0.4055: Doc 0: 0.4055/6 = 0.0676, Doc 2: 0.4055*2/5 = 0.1622. Still not matching.
The output matches IDF = ln(N/df+1)+1 ... The actual calculation will use the code's formula directly.