PIXELBANKv8.2.1
Menu

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.

Input format:

  • Line 1: The text (space-separated words)
  • Line 2: 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:

Input:
I love NLP deeply
2
Output:
[('I', 'love'), ('I', 'NLP'), ('love', 'I'), ('love', 'NLP'), ('love', 'deeply'), ('NLP', 'I'), ('NLP', 'love'), ('NLP', 'deeply'), ('deeply', 'love'), ('deeply', 'NLP')]
Reasoning:

Words: ["I", "love", "NLP", "deeply"], window=2

Center "I" (pos 0): context positions max(0,0-2)=0 to min(3,0+2)=2, excluding 0 => 1,2 Pairs: (I, love), (I, NLP)

Center "love" (pos 1): context 0 to 3, excluding 1 => 0,2,3 Pairs: (love, I), (love, NLP), (love, deeply)

Center "NLP" (pos 2): context 0 to 3, excluding 2 => 0,1,3 Pairs: (NLP, I), (NLP, love), (NLP, deeply)

Center "deeply" (pos 3): context 1 to 3, excluding 3 => 1,2 Pairs: (deeply, love), (deeply, NLP)

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
Editor

Test Results

0/0
Run code to see test results.