Natural Language Processing · TEE · papers

Q7b · Naive Bayes sentiment

2024-02-12 · re-exam · recovered AI paper

Previous examination · complete worked answers

Question. Train Naive Bayes on: positive “amaze wonderful great”; “happy happy great”; “amaze great happy disgust”; “great happy wonderful”. Negative “disgust sad terrible great”; “sad terrible sad happy”. Predict sentence 7: “great happy disgust great”. [10 marks]

Answer

Given

Training textClass
amaze wonderful greatpositive
happy happy greatpositive
amaze great happy disgustpositive
great happy wonderfulpositive
disgust sad terrible greatnegative
sad terrible sad happynegative

Test: great happy disgust great

To find

Classify the test using multinomial Naive Bayes.

Formula: which one and why

Assume add-one smoothing unless the question supplies probabilities. Keep repeated words as repeated factors. Use training vocabulary only; ignore out-of-vocabulary words unless an unknown-token model is defined.

\mathrm{score}(c)=P(c)\prod_{w\in V}\left(\frac{c(w,c)+1}{N_c+|V|}\right)^{c(w,d)}LaTeX: \mathrm{score}(c)=P(c)\prod_{w\in V}\left(\frac{c(w,c)+1}{N_c+|V|}\right)^{c(w,d)}

Step 1. Vocabulary and class priors

V = {amaze, disgust, great, happy, sad, terrible, wonderful}; |V|=7.

ClassDocumentsPrior
negative21/3
positive42/3

Step 2. Count every training word within each class

ClassTotal tokensamazedisgustgreathappysadterriblewonderful
negative80111320
positive132144002

Step 3. Count the test words

WordCount
great2
happy1
disgust1

OOV words: none

Step 4. Calculate likelihoods and multiply with the prior

ClassPriorIndividual factorsExact scoreDecimal score
negative1/3great: (1+1)/(8+1×7) = 2/15, used 2 time(s); happy: (1+1)/(8+1×7) = 2/15, used 1 time(s); disgust: (1+1)/(8+1×7) = 2/15, used 1 time(s)16/1518750.000105349794239
positive2/3great: (4+1)/(13+1×7) = 1/4, used 2 time(s); happy: (4+1)/(13+1×7) = 1/4, used 1 time(s); disgust: (1+1)/(13+1×7) = 1/10, used 1 time(s)1/9600.00104166666667

Step 5. Compare and optionally normalize

Normalized scores: negative = 0.091846802, positive = 0.908153198

Division by the same total does not change the winning class.

Final answer

Predicted class: positive.

Yaad rakho: Count → prior → smoothed word chances → repeated factors → compare.