Natural Language Processing · TEE · papers

Q4b · Three-document TF–IDF

2023-12-01 · final · recovered AI paper

Previous examination · complete worked answers

Question. Calculate TF–IDF for: (1) Inflation increased unemployment (2) Company increased sales (3) Fear increased pulse. [10 marks]

Answer

Given

DocumentOriginal text
1Inflation increased unemployment
2Company increased sales
3Fear increased pulse

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
1inflation, increased, unemployment
2company, increased, sales
3fear, increased, pulse

Step 2. Fix the vocabulary order

company, fear, increased, inflation, pulse, sales, unemployment

Step 3. Count occurrences (Bag of Words)

Documentcompanyfearincreasedinflationpulsesalesunemployment
10011001
21010010
30110100

Step 4. Calculate document frequency and IDF

TermdfN / dfln(N/df)
company131.098612289
fear131.098612289
increased310.000000000
inflation131.098612289
pulse131.098612289
sales131.098612289
unemployment131.098612289

Step 5. Multiply each TF by its IDF

Documentcompanyfearincreasedinflationpulsesalesunemployment
10.0000000.0000000.0000001.0986120.0000000.0000001.098612
21.0986120.0000000.0000000.0000000.0000001.0986120.000000
30.0000001.0986120.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.