Word2Vec Skip-gram
Implement skip-gram training pair generation for Word2Vec.
In the skip-gram model, given a center word, we predict the context words within a window. Generate all (center_word, context_word) training pairs.
Write skipgram_pairs(words, window).
Input:
- words: a list of words (the text already split on spaces)
- window: window size W (integer)
For each word at position i, context words are at positions max(0, i-W) to min(n-1, i+W), excluding position i itself.
Output: A list of tuples (center_word, context_word) in order of center word position, and for each center word, context words from left to right.
Example
I love NLP deeply 2
[('I', 'love'), ('I', 'NLP'), ('love', 'I'), ('love', 'NLP'), ('love', 'deeply'), ('NLP', 'I'), ('NLP', 'love'), ('NLP', 'deeply'), ('deeply', 'love'), ('deeply', 'NLP')]- Inputs:
wordsis the text split into tokens: "I" (position 0), "love" (1), "NLP" (2), "deeply" (3).windowis neighbours on each side. - Each word's window: position sees positions through , minus itself. "I" gets 1, 2 (nothing to its left); "love" gets 0, 2, 3; "NLP" gets 0, 1, 3; "deeply" gets 1, 2 (the text ends on its right).
- Pairs: each center word is paired with its context words, left to right, giving
[('I', 'love'), ('I', 'NLP'), ('love', 'I'), ('love', 'NLP'), ('love', 'deeply'), ('NLP', 'I'), ('NLP', 'love'), ('NLP', 'deeply'), ('deeply', 'love'), ('deeply', 'NLP')]. - Count check: pairs. Away from the edges a word would get neighbours (16 pairs in total); the boundaries clip that.
- Notice: pairs come in mirrored couples, e.g. (I, NLP) and (NLP, I), because "within positions" is symmetric. Casing is preserved, so "NLP" stays upper case.
Constraints
- Window size W means W words to the left and W words to the right
- Handle boundary positions (beginning/end of text)
- Preserve original word casing
- Output pairs in positional order