win :: int corresponding to the size of the window given a list of indexes composing a sentence l :: array containing the word indexes it will return a list of list of indexes corresponding to context windows surrounding each word in the sentence
(l, win)
| 43 | |
| 44 | # start-snippet-1 |
| 45 | def contextwin(l, win): |
| 46 | ''' |
| 47 | win :: int corresponding to the size of the window |
| 48 | given a list of indexes composing a sentence |
| 49 | |
| 50 | l :: array containing the word indexes |
| 51 | |
| 52 | it will return a list of list of indexes corresponding |
| 53 | to context windows surrounding each word in the sentence |
| 54 | ''' |
| 55 | assert (win % 2) == 1 |
| 56 | assert win >= 1 |
| 57 | l = list(l) |
| 58 | |
| 59 | lpadded = win // 2 * [-1] + l + win // 2 * [-1] |
| 60 | out = [lpadded[i:(i + win)] for i in range(len(l))] |
| 61 | |
| 62 | assert len(out) == len(l) |
| 63 | return out |
| 64 | # end-snippet-1 |
| 65 | |
| 66 |