Created
November 16, 2025 00:01
-
-
Save jweinst1/31baae6f9c04884fdb4b0b181719a8df to your computer and use it in GitHub Desktop.
forward and back contexts of a word
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
| 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