Natural Language Processing · TEE · papers

Q2a · TF–IDF cosine after a fixed stoplist

2025-02-21 · re-exam · recovered AI paper

Previous examination · complete worked answers

Question. For “The cat sat on the mat.”, “The dog barked at the cat.” and “I love my dog and my cat.” remove stopwords {I,the,on,my,at,and}; construct TF–IDF and calculate cosine similarity between the first two documents. [10 marks]

Answer

Given

DocumentOriginal text
1The cat sat on the mat.
2The dog barked at the cat.
3I love my dog and my cat.

Stopwords: at, my, i, on, the, and

To find

BoW, IDF and TF–IDF plus requested cosine

Formula: which one and why

Use lowercase alphabetic tokens and exactly the stated stoplist. If no stoplist is supplied, retain function words. Adopt raw-count TF and natural-log IDF; the paper does not prescribe another TF/log convention.

\mathrm{TFIDF}(w,d)=c(w,d)\ln\frac{N}{\mathrm{df}(w)}LaTeX: \mathrm{TFIDF}(w,d)=c(w,d)\ln\frac{N}{\mathrm{df}(w)}

A term shared by all N documents has IDF ln(1)=0. Counts and TF–IDF weights are different representations.

Step 1. Tokenize each document

DocumentTokens
1cat, sat, mat
2dog, barked, cat
3love, dog, cat

Step 2. Fix the vocabulary order

barked, cat, dog, love, mat, sat

Step 3. Count occurrences (Bag of Words)

Documentbarkedcatdoglovematsat
1010011
2111000
3011100

Step 4. Calculate document frequency and IDF

TermdfN / dfln(N/df)
barked131.098612289
cat310.000000000
dog23/20.405465108
love131.098612289
mat131.098612289
sat131.098612289

Step 5. Multiply each TF by its IDF

Documentbarkedcatdoglovematsat
10.0000000.0000000.0000000.0000001.0986121.098612
21.0986120.0000000.4054650.0000000.0000000.000000
30.0000000.0000000.4054651.0986120.0000000.000000

Step 6. Calculate cosine from these same TF–IDF rows

\cos(u,v)=\frac{\sum_i u_i v_i}{\sqrt{\sum_i u_i^2}\sqrt{\sum_i v_i^2}}LaTeX: \cos(u,v)=\frac{\sum_i u_i v_i}{\sqrt{\sum_i u_i^2}\sqrt{\sum_i v_i^2}}

Coordinate products: 0.000000000, 0.000000000, 0.000000000, 0.000000000, 0.000000000, 0.000000000

Dot product=0.000000000; squared norms=2.413897922, 1.371350915. Cosine=0.000000000

Final answer

The final table is the TF–IDF matrix in the declared vocabulary order. A different explicitly stated log base rescales all IDFs; length-normalized TF changes each document scale.

Cosine is 0.0. Shared words whose IDF is zero contribute nothing to the TF–IDF dot product.

Which terms are prominent?

D1: sat and mat have IDF ln3; D2: barked has ln3 and dog has ln(3/2); D3: love has ln3 and dog has ln(3/2). Cat occurs in all three documents and has zero unsmoothed IDF. Thus shared cat does not make D1/D2 TF–IDF cosine positive.