Natural Language Processing · TEE · mocks

Q3B · Co-occurrence matrix and cosine

Timed practice · Solved final paper · December 2025

TEE · actual previous paper · 100 marks · 180 minutes

Question. Consider the given paragraph: The teacher wrote on the board. The students listened to the teacher. The board was full of notes. The student asked a question. The teacher answered from the book.
Target words: teacher, student, board, book.
Context words: wrote, listened, notes, question.
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 “teacher-student” and “teacher-board” using the cosine similarity.
iii. Plot the target word vectors by considering only two dimensions: “wrote” and “notes”. [10 marks]

Answer

Given

Five sentences, four target rows and four context columns. Treat students and student as the same target lemma, and count each target–context pair once when both occur in a sentence.

To find

The 4 × 4 sentence-window co-occurrence matrix, two cosine similarities and the requested 2-D plot.

Formula: which one and why

M_{ij}=\sum_{s=1}^{5}\mathbf{1}[\text{target}_i\text{ and context}_j\text{ occur in sentence }s]LaTeX: M_{ij}=\sum_{s=1}^{5}\mathbf{1}[\text{target}_i\text{ and context}_j\text{ occur in sentence }s]

The indicator is 1 when both words appear in that sentence and 0 otherwise. Each matrix row is the target word’s vector: its list of four context counts.

\cos(\mathbf a,\mathbf b)=\frac{\mathbf a\cdot\mathbf b}{\|\mathbf a\|\|\mathbf b\|}LaTeX: \cos(\mathbf a,\mathbf b)=\frac{\mathbf a\cdot\mathbf b}{\|\mathbf a\|\|\mathbf b\|}

The dot product multiplies corresponding coordinates and adds them. The length is the square root of the sum of squared coordinates. Cosine compares directions while allowing different vector lengths.

Solution

Step 1. Record the contribution of each sentence

SentenceTargets presentRelevant contextCounts added
1teacher, boardwroteteacher–wrote +1; board–wrote +1
2student, teacherlistenedstudent–listened +1; teacher–listened +1
3boardnotesboard–notes +1
4studentquestionstudent–question +1
5teacher, booknoneNo requested context count

Step 2. Construct every requested column

Targetwrotelistenednotesquestion
teacher1100
student0101
board1010
book0000

Step 3. Calculate teacher–student

\mathbf t\cdot\mathbf s=(1)(0)+(1)(1)+(0)(0)+(0)(1)=1LaTeX: \mathbf t\cdot\mathbf s=(1)(0)+(1)(1)+(0)(0)+(0)(1)=1
\|\mathbf t\|=\sqrt{1^2+1^2+0^2+0^2}=\sqrt2LaTeX: \|\mathbf t\|=\sqrt{1^2+1^2+0^2+0^2}=\sqrt2
\|\mathbf s\|=\sqrt{0^2+1^2+0^2+1^2}=\sqrt2LaTeX: \|\mathbf s\|=\sqrt{0^2+1^2+0^2+1^2}=\sqrt2
\cos(\mathbf t,\mathbf s)=\frac1{\sqrt2\sqrt2}=\frac12=0.5LaTeX: \cos(\mathbf t,\mathbf s)=\frac1{\sqrt2\sqrt2}=\frac12=0.5

Step 4. Calculate teacher–board

\mathbf t\cdot\mathbf b=(1)(1)+(1)(0)+(0)(1)+(0)(0)=1LaTeX: \mathbf t\cdot\mathbf b=(1)(1)+(1)(0)+(0)(1)+(0)(0)=1
\|\mathbf b\|=\sqrt{1^2+0^2+1^2+0^2}=\sqrt2LaTeX: \|\mathbf b\|=\sqrt{1^2+0^2+1^2+0^2}=\sqrt2
\cos(\mathbf t,\mathbf b)=\frac1{\sqrt2\sqrt2}=0.5LaTeX: \cos(\mathbf t,\mathbf b)=\frac1{\sqrt2\sqrt2}=0.5

Step 5. Select only the two requested plot coordinates

Wordx = wrotey = notes
teacher10
student00
board11
book00
001122teacherstudent, bookboardwrotenotes
The plot uses only the requested two coordinates; cosine uses all context columns.

Final answer

Both requested cosine similarities are 0.5. Student and book overlap at the origin of the 2-D projection, but their complete four-dimensional vectors differ. The book row is zero, so a cosine involving book would be undefined without an explicit convention.

Yaad rakho: Har sentence mein target aur context saath? Ek count. Cosine ke liye poori row lo, plot ke liye sirf do columns.