MCPcopy
hub / github.com/QData/TextAttack / text_window_around_index

Method text_window_around_index

textattack/shared/attacked_text.py:108–124  ·  view source on GitHub ↗

The text window of ``window_size`` words centered around ``index``.

(self, index: int, window_size: int)

Source from the content-addressed store, hash-verified

106 self.attack_attrs.pop(key, None)
107
108 def text_window_around_index(self, index: int, window_size: int) -> str:
109 """The text window of ``window_size`` words centered around
110 ``index``."""
111 length = self.num_words
112 half_size = (window_size - 1) / 2.0
113 if index - half_size < 0:
114 start = 0
115 end = min(window_size - 1, length - 1)
116 elif index + half_size >= length:
117 start = max(0, length - window_size)
118 end = length - 1
119 else:
120 start = index - math.ceil(half_size)
121 end = index + math.floor(half_size)
122 text_idx_start = self._text_index_of_word_index(start)
123 text_idx_end = self._text_index_of_word_index(end) + len(self.words[end])
124 return self.text[text_idx_start:text_idx_end]
125
126 def pos_of_word_index(self, desired_word_idx: int) -> str:
127 """Returns the part-of-speech of the word at index `word_idx`.

Callers 9

_sim_scoreMethod · 0.80
_score_listMethod · 0.80
_get_merged_wordsMethod · 0.80
_get_new_wordsMethod · 0.80

Calls 1