| 348 | |
| 349 | |
| 350 | def get_context(pos, sentence, window_size): |
| 351 | # input: |
| 352 | # a sentence of the form: x x x x c c c pos c c c x x x x |
| 353 | # output: |
| 354 | # the context word indices: c c c c c c |
| 355 | |
| 356 | start = max(0, pos - window_size) |
| 357 | end_ = min(len(sentence), pos + window_size) |
| 358 | |
| 359 | context = [] |
| 360 | for ctx_pos, ctx_word_idx in enumerate(sentence[start:end_], start=start): |
| 361 | if ctx_pos != pos: |
| 362 | # don't include the input word itself as a target |
| 363 | context.append(ctx_word_idx) |
| 364 | return context |
| 365 | |
| 366 | |
| 367 | |