# Q6a · Text classification pipeline

Subject: Natural Language Processing

Exam: TEE

Reader: https://deep-learning-study-room.vercel.app/nlp/#tee/papers/legacy-f23/q6a

Canonical: https://deep-learning-study-room.vercel.app/read/nlp/tee/papers/legacy-f23--q6a/

Previous examination · complete worked answers

**Question. Explain the text-classification pipeline in detail with a suitable block diagram.** [10 marks]

### Answer

**Text classification** assigns a document to one or more predefined categories. Spam detection usually uses two classes; news classification may use several topics. In single-label multiclass classification, choose one of several categories. In multilabel classification, several labels may apply to the same document.

Define labels

→

Prepare labelled examples

→

Train representation + model

→

Evaluate on unseen text

→

Predict new labels

### An exam-ready pipeline

| Stage | What you do | Why it matters |
| --- | --- | --- |
| Task definition | Decide what counts as spam or which news topics exist. | Unclear labels create inconsistent training data. |
| Data collection and annotation | Collect representative examples and assign correct labels. | The model learns from these examples. |
| Split data | Separate training, validation and test data. | Test examples must not influence training choices. |
| Preprocess | Tokenize and normalise where suitable; retain meaningful negation. | Reduce irrelevant variation without deleting signal. |
| Represent | Use BoW, TF-IDF or learned embeddings. | Models need numerical features. |
| Train | Fit Naive Bayes, logistic regression or another appropriate model. | Learn how features predict categories. |
| Evaluate | Measure relevant precision, recall and F1 on held-out examples. | Accuracy alone may hide poor minority-class performance. |
| Deploy and monitor | Apply the same preprocessing and monitor changing language. | Incoming data can differ from old examples. |

Naive Bayes is especially useful for hand calculations. With a bag-of-words representation, its class score is the prior multiplied by the conditional probability of each token; repeated tokens repeat the factor. The “naive” assumption is conditional independence of features given the class, not that words are independent in real language.



$$
s(c)=P(c)\prod_{w\in V}P(w\mid c)^{C(w,d)}
$$





$$
P(w\mid c)=\frac{C(w,c)+1}{N_c+|V|}
$$



C(w,d) counts word w in document d. C(w,c) counts it in training documents belonging to class c. Nc is the total token count for class c. Add-one smoothing adds one to every vocabulary count and therefore adds |V| to the denominator. This is the same arithmetic taught for WSD, with document classes instead of senses.

### Worked classification · two-token message

A spam classifier has vocabulary {win, prize, meeting}. Priors are P(spam)=P(ham)=0.5. Spam token counts are [3,2,0]; ham counts are [0,0,5]. With add-one smoothing, classify “win prize”.

**Given**

|V|=3; N(spam)=5; N(ham)=5; message has win once and prize once.

**To find**

Class scores and predicted label.

### Formula and why it applies



$$
s(c)=P(c)P(\text{win}\mid c)P(\text{prize}\mid c)
$$





$$
P(w\mid c)=\frac{C(w,c)+1}{N_c+|V|}
$$



The model is multinomial Naive Bayes; multiply the two token likelihoods and the class prior.

### Solution

#### Step 1. Compute the smoothed denominators



$$
5+3=8
$$



#### Step 2. Compute spam likelihoods



$$
P(\text{win}\mid S)=\frac{3+1}{8}=\frac12
$$





$$
P(\text{prize}\mid S)=\frac{2+1}{8}=\frac38
$$



#### Step 3. Compute ham likelihoods



$$
P(\text{win}\mid H)=P(\text{prize}\mid H)=\frac{0+1}{8}=\frac18
$$



#### Step 4. Multiply the spam score



$$
s(S)=\frac12\times\frac12\times\frac38=\frac{3}{32}=0.09375
$$



#### Step 5. Multiply the ham score



$$
s(H)=\frac12\times\frac18\times\frac18=\frac{1}{128}=0.0078125
$$



#### Step 6. Compare

0.09375 > 0.0078125. These are unnormalised scores; the label is chosen by the larger score.

Predicted label: spam.

**Yaad rakho:** Class prior × har word ki class-likelihood; sabse bada score wins.
