pyrit.prompt_converter.WordSelectionStrategy#

class WordSelectionStrategy[source]#

Bases: TextSelectionStrategy

Base class for word-level selection strategies.

Word selection strategies work by splitting text into words and selecting specific word indices. They provide a select_words() method and implement select_range() by converting word selections to character ranges.

__init__()#

Methods

__init__()

select_range(*, text[, word_separator])

Selects a character range by first selecting words, then converting to character positions.

select_words(*, words)

Selects word indices to be converted.

select_range(*, text: str, word_separator: str = ' ') tuple[int, int][source]#

Selects a character range by first selecting words, then converting to character positions.

This implementation splits the text by word_separator, gets selected word indices, then calculates the character range that spans those words.

Parameters:
  • text (str) – The input text to select from.

  • word_separator (str) – The separator used to split words. Defaults to “ “.

Returns:

A tuple of (start_index, end_index) representing the character range

that encompasses all selected words.

Return type:

tuple[int, int]

abstract select_words(*, words: List[str]) List[int][source]#

Selects word indices to be converted.

Parameters:

words (List[str]) – The list of words to select from.

Returns:

A list of indices representing which words should be converted.

Return type:

List[int]