pyrit.prompt_converter.ProportionSelectionStrategy#

class ProportionSelectionStrategy(*, proportion: float, anchor: str = 'start', seed: int | None = None)[source]#

Bases: TextSelectionStrategy

Selects a proportion of text anchored to a specific position (start, end, middle, or random).

__init__(*, proportion: float, anchor: str = 'start', seed: int | None = None) None[source]#

Initializes the proportion selection strategy.

Parameters:
  • proportion (float) – The proportion of text to select (0.0 to 1.0).

  • anchor (str) – Where to anchor the selection. Valid values: - ‘start’: Select from the beginning - ‘end’: Select from the end - ‘middle’: Select from the middle - ‘random’: Select from a random position

  • seed (Optional[int]) – Random seed for reproducible random selections. Defaults to None.

Raises:

ValueError – If proportion is not between 0.0 and 1.0, or anchor is invalid.

Methods

__init__(*, proportion[, anchor, seed])

Initializes the proportion selection strategy.

select_range(*, text)

Selects a proportion of text based on the anchor position.

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

Selects a proportion of text based on the anchor position.

Parameters:

text (str) – The input text to select from.

Returns:

A tuple of (start_index, end_index).

Return type:

tuple[int, int]