Skip to content

Instantly share code, notes, and snippets.

@jweinst1
Created November 16, 2025 00:01
Show Gist options
  • Select an option

  • Save jweinst1/31baae6f9c04884fdb4b0b181719a8df to your computer and use it in GitHub Desktop.

Select an option

Save jweinst1/31baae6f9c04884fdb4b0b181719a8df to your computer and use it in GitHub Desktop.
forward and back contexts of a word
class WordContext(object):
def __init__(self, direction, terms):
self.direction = direction
self.terms = terms
def __repr__(self):
return f"{self.direction} - {self.terms}"
def make_context_list(seq, target):
place = seq.index(target)
conts = []
# back conts
back_i = place
back_d = 1
while back_i:
conts.append(WordContext(-back_d, seq[0:back_i]))
back_d += 1
back_i -= 1
forw_begin = place + 1
forw_d = 1
while (forw_begin + forw_d) <= len(seq):
conts.append(WordContext(forw_d, seq[forw_begin: forw_begin + forw_d]))
forw_d += 1
return conts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment