# Lexical & Vector Semantics: TF-IDF, BM25, Word2Vec (Skip-Gram / CBOW) & GloVe
Exam: GATE Data Science & Artificial Intelligence (DA) (GATE DA)
Subject: Natural Language Processing (NLP) & LLMs
Canonical URL: https://gateandnet.com/exams/gate-da/natural-language-processing/tfidf-bm25-word2vec-and-distributional-embeddings

> Sparse Bag-of-Words vs TF-IDF & Okapi BM25 saturation, Cosine Similarity invariance, Word2Vec CBOW vs Skip-Gram with Negative Sampling (SGNS), and vector analogy arithmetic.

## 1. Sparse Representations: BoW, TF-IDF & Okapi BM25

- **Bag-of-Words (BoW):** Document vector in $\mathbb{R}^{|V|}$; loses word order, high-dimensional ($|V| \sim 10^5$), orthogonal one-hot word vectors ($\cos(w_i, w_j) = 0$ even for *"hotel"* and *"motel"*!).
- **TF-IDF:** Multiplies local **Term Frequency** (often sublinear $1 + \log_{10} f_{t,d}$) by global **Inverse Document Frequency** $\log(N / \text{df}_t)$ so rare discriminative terms outweigh ubiquitous words.
- **Okapi BM25 (Modern IR & RAG Lexical Baseline):** Adds **term-frequency saturation** ($k_1 \approx 1.2$: seeing a query word $10$ times isn't $10\times$ more relevant than $3$ times) and **document-length normalization** ($b \approx 0.75$).

## 2. Dense Static Word Embeddings: Word2Vec & GloVe

Grounded in the **Distributional Hypothesis** (*"You shall know a word by the company it keeps"* — Firth, 1957), Word2Vec learns dense low-dimensional embeddings $d \in [100, 300]$ via self-supervised sliding windows:

| Model Variant | Prediction Task | Speed & Strengths |
| :--- | :--- | :--- |
| **CBOW (Continuous Bag-of-Words)** | Predicts center word $w_t$ from averaged surrounding window $(w_{t-m}, \dots, w_{t+m})$ | **Faster** to train; slightly better on frequent words |
| **Skip-Gram** | Predicts each context word $w_{t+j}$ ($-m \le j \le m, j \neq 0$) given center word $w_t$ | Superior representations for **rare words** & morphological analogies |
| **FastText** | Represents each word as sum of **character $n$-grams** (e.g. `<wh`, `whe`, `her`, `ere`, `re>`) | Handles **Out-of-Vocabulary (OOV)** words & rich morphology! |
| **GloVe** | Factorizes log global word-word co-occurrence matrix $w_i^T \tilde{w}_j + b_i + \tilde{b}_j \approx \log X_{ij}$ | Combines global matrix statistics + local context geometry |

> [!TRAP]
> **Static vs Contextual Embeddings (Polysemy Limitation):** Word2Vec and GloVe assign **one single static vector** per word type — *"river **bank**"* and *"savings **bank**"* receive the exact same vector! Contextual encoders (**ELMo, BERT, GPT**) compute dynamic token vectors $h_t = f(w_1, \dots, w_n)$ conditioned on the entire sentence.

## Solved Practice Questions & PYQs

### [GATE DA NLP | MCQ]
In a collection of $N = 10,000$ documents, the word `"the"` appears at least once in **every single document** ($\text{df} = 10,000$). Using standard $\operatorname{idf}(t) = \log_{10}(N / \text{df}_t)$ without smoothing, what is the **TF-IDF weight** of `"the"` in document $d$?
**Correct Answer:** A
**Solution:** $\operatorname{idf}(\text{the}) = \log_{10}(10000 / 10000) = \log_{10}(1) = 0$. Hence $\operatorname{tfidf} = \operatorname{tf} \times 0 = \mathbf{0}$, automatically zeroing out non-discriminative ubiquitous stopwords!

### [GATE DA NLP | MCQ]
Which embedding architecture represents each word as a **bag of character $n$-grams**, enabling it to generate meaningful vectors for unseen **Out-of-Vocabulary (OOV)** words at inference time?
**Correct Answer:** C
**Solution:** **FastText** (Bojanowski et al.) sums subword character $n$-gram embeddings (e.g., `<ne`, `neu`, `eur`, `ura`, `ral`, `al>`), allowing OOV or misspelled words to share representations via shared morphological roots/affixes!
