Natural Language Processing · TEE · papers

Q6b · BoW and TF–IDF movie corpus

2022-11-22 · final · recovered AI paper

Previous examination · complete worked answers

Question. Find Bag of Words and TF–IDF for: “This movie is very scary and long”; “This movie is not scary and is slow”; “This movie is spooky and good”. [10 marks]

Answer

Given

DocumentOriginal text
1This movie is very scary and long
2This movie is not scary and is slow
3This movie is spooky and good

Stopwords: none supplied

To find

BoW, IDF and TF–IDF

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
1this, movie, is, very, scary, and, long
2this, movie, is, not, scary, and, is, slow
3this, movie, is, spooky, and, good

Step 2. Fix the vocabulary order

and, good, is, long, movie, not, scary, slow, spooky, this, very

Step 3. Count occurrences (Bag of Words)

Documentandgoodislongmovienotscaryslowspookythisvery
110111010011
210201111010
311101000110

Step 4. Calculate document frequency and IDF

TermdfN / dfln(N/df)
and310.000000000
good131.098612289
is310.000000000
long131.098612289
movie310.000000000
not131.098612289
scary23/20.405465108
slow131.098612289
spooky131.098612289
this310.000000000
very131.098612289

Step 5. Multiply each TF by its IDF

Documentandgoodislongmovienotscaryslowspookythisvery
10.0000000.0000000.0000001.0986120.0000000.0000000.4054650.0000000.0000000.0000001.098612
20.0000000.0000000.0000000.0000000.0000001.0986120.4054651.0986120.0000000.0000000.000000
30.0000001.0986120.0000000.0000000.0000000.0000000.0000000.0000001.0986120.0000000.000000

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.