Skip to content

Instantly share code, notes, and snippets.

@ashunigion
Last active June 10, 2019 00:46
Show Gist options
  • Select an option

  • Save ashunigion/fc44ffeae093733ca9dd5145edf22ca8 to your computer and use it in GitHub Desktop.

Select an option

Save ashunigion/fc44ffeae093733ca9dd5145edf22ca8 to your computer and use it in GitHub Desktop.
Code to return context for word on a current index, from a list of words
def get_target(words, idx, window_size=5):
''' Get a list of words in a window around an index.
words -- list of words in text
idx -- index of the center word
window_size -- the window size to define context
'''
# implement this function
R = np.random.randint(1, window_size+1)
start = idx - R if (idx - R) > 0 else 0
stop = idx + R
target_words = words[start:idx] + words[idx+1:stop+1]
return list(target_words)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment