Last active
June 10, 2019 00:46
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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