Natural Language Processing · TEE · papers

Q5B · Sentence-window vectors and cosine

Solved re-exam · February 2026

Actual re-examination · 9 February 2026 · 100 marks · 3 hours

Question. Consider the paragraph: The farmer planted seeds in the soil. The rain watered the soil. The sun helped the plants grow. The farmer harvested the plants. The soil became rich and fertile.
Target words: farmer, soil, sun, plant.
Context words: plant, water, grow, harvest.
i. Construct a co-occurrence matrix for the target and context words. Take the window size as one whole sentence.
ii. Evaluate the similarity between the vectors of the target words “farmer-soil”, “farmer-sun”, and “farmer-plant” using the cosine similarity.
iii. Plot the target word vectors by considering only two dimensions: “planted” and “harvested”. [10 marks]

Answer

Given

Five sentence windows. For alignment with the supplied matrix, target plant means the noun plants, occurring in sentences 3 and 4. Context plant covers planted and plants; watered → water; harvested → harvest. Count a target–context pair once per sentence in which both are present, including the plant–plant sentence-presence diagonal.

To find

The target–context matrix, three cosine similarities and a two-coordinate plot.

Formula: which one and why

M_{ij}=\sum_s\mathbf1[\text{target}_i\text{ and context}_j\text{ present in }s]LaTeX: M_{ij}=\sum_s\mathbf1[\text{target}_i\text{ and context}_j\text{ present in }s]
\cos(\mathbf a,\mathbf b)=\frac{\sum_j a_jb_j}{\sqrt{\sum_j a_j^2}\sqrt{\sum_j b_j^2}}LaTeX: \cos(\mathbf a,\mathbf b)=\frac{\sum_j a_jb_j}{\sqrt{\sum_j a_j^2}\sqrt{\sum_j b_j^2}}

This is a sentence-presence matrix, not a position-based sliding window that excludes a token from its own context. Stating the convention is necessary because those alternatives give a different diagonal.

Solution

Step 1. Normalize and identify each sentence’s contribution

SentenceTargets presentContext lemmas present
1farmer, soilplant
2soilwater
3sun, plantplant, grow
4farmer, plantplant, harvest
5soilnone

Step 2. Fill all matrix rows

Targetplantwatergrowharvest
farmer2001
soil1100
sun1010
plant2011

Farmer meets context plant in sentences 1 and 4. Soil meets plant in sentence 1 and water in sentence 2. Sun meets plant and grow in sentence 3. The noun plant occurs in sentences 3 and 4, producing its last row.

Step 3. Compute farmer–soil

\mathbf f\cdot\mathbf s=(2)(1)+(0)(1)+(0)(0)+(1)(0)=2LaTeX: \mathbf f\cdot\mathbf s=(2)(1)+(0)(1)+(0)(0)+(1)(0)=2
\|\mathbf f\|=\sqrt{2^2+0^2+0^2+1^2}=\sqrt5,\quad\|\mathbf s\|=\sqrt2LaTeX: \|\mathbf f\|=\sqrt{2^2+0^2+0^2+1^2}=\sqrt5,\quad\|\mathbf s\|=\sqrt2
\cos(\mathbf f,\mathbf s)=\frac2{\sqrt{10}}\approx0.632456LaTeX: \cos(\mathbf f,\mathbf s)=\frac2{\sqrt{10}}\approx0.632456

Step 4. Compute farmer–sun

\mathbf f\cdot\mathbf u=(2)(1)+(0)(0)+(0)(1)+(1)(0)=2LaTeX: \mathbf f\cdot\mathbf u=(2)(1)+(0)(0)+(0)(1)+(1)(0)=2
\|\mathbf u\|=\sqrt2,\quad\cos(\mathbf f,\mathbf u)=\frac2{\sqrt{10}}\approx0.632456LaTeX: \|\mathbf u\|=\sqrt2,\quad\cos(\mathbf f,\mathbf u)=\frac2{\sqrt{10}}\approx0.632456

Step 5. Compute farmer–plant

\mathbf f\cdot\mathbf p=(2)(2)+(0)(0)+(0)(1)+(1)(1)=5LaTeX: \mathbf f\cdot\mathbf p=(2)(2)+(0)(0)+(0)(1)+(1)(1)=5
\|\mathbf p\|=\sqrt{2^2+0^2+1^2+1^2}=\sqrt6LaTeX: \|\mathbf p\|=\sqrt{2^2+0^2+1^2+1^2}=\sqrt6
\cos(\mathbf f,\mathbf p)=\frac5{\sqrt{30}}\approx0.912871LaTeX: \cos(\mathbf f,\mathbf p)=\frac5{\sqrt{30}}\approx0.912871

Step 6. Plot the requested two coordinates

Interpret the axis names planted and harvested as their normalized context features plant and harvest. Farmer=(2,1), soil=(1,0), sun=(1,0), plant=(2,1). Farmer and plant overlap; soil and sun overlap.

001122farmer, plantsoil, sunplant (planted)harvest (harvested)
The plot uses only the requested two coordinates; cosine uses all context columns.

Final answer

Using the stated noun-target convention: farmer–soil = 0.632456, farmer–sun = 0.632456, farmer–plant = 0.912871.

Yaad rakho: Plant noun aur plant verb ko same maana ya separate? Pehle convention likho, phir count karo.

Two details that prevent an incorrect answer

Arithmetic: 5/√30 = 0.912871; the source key’s printed approximation 0.9444 does not equal its own fraction. Keep the exact fraction until the last step.

Normalization: If the question instead requires all noun and verb forms of plant to be merged as targets, sentence 1 also contains the plant target. Its row becomes [3,0,1,1], and farmer–plant becomes 7/√55 ≈ 0.943880. The paper leaves this choice unstated; the main worked solution explicitly uses the noun-target interpretation that reproduces its supplied matrix.