LiteralDetector#

class scikitplot.cleanprompt.LiteralDetector(terms, kind='CUSTOM', name=None, priority=75, word_boundary=False, ignore_case=False)[source]#

Detector for exact strings the caller names.

Parameters:
termsiterable of str

Strings to hide. Empty and whitespace-only terms are rejected.

kindstr, default=’CUSTOM’

Category for every match.

namestr, optional

Detector name. Defaults to "literal:<kind>".

priorityint, default=75

Arbitration weight. Above the structural patterns by default, because a caller naming a term explicitly has expressed stronger intent than a generic pattern.

word_boundarybool, default=False

When True, a term matches only at word boundaries.

ignore_casebool, default=False

When True, terms match regardless of case. Normally supplied from case_insensitive so that one setting governs both halves of “case is not significant”: which surfaces match, and which surfaces share a label. Implementing only the second half would silently ignore --hide acme against the text Acme.

Raises:
PolicyError

If a term is empty or whitespace-only.

Parameters:

Notes

User notes. This is the “additional words to hide” feature. Terms are matched literally, not as regular expressions, so a.b matches exactly a.b and not axb.

Developer notes. Two properties matter here, and both were absent upstream.

Longest-first alternation. Terms are sorted by descending length inside one alternation, so scanning "Ann met Anna" for ["Ann", "Anna"] matches Anna at its position rather than Ann followed by a stranded a. Upstream rewrote the string once per term with str.replace and produced "[ADDITIONAL-1] met [ADDITIONAL-1]a" — defect CP-001, which both corrupts the text and leaks the final a of the secret.

One pass. All terms are scanned in a single alternation, so cost is linear in the text rather than linear in len(text) * len(terms).

Examples

>>> from ._policy import DEFAULT_POLICY
>>> detector = LiteralDetector(["Ann", "Anna"])
>>> [(s.start, s.text) for s in detector.detect("Ann met Anna", DEFAULT_POLICY)]
[(0, 'Ann'), (8, 'Anna')]
confidence#

!! processed by numpydoc !!

detect(text, policy)[source]#

Yield one span per literal occurrence.

Parameters:
textstr

The original text.

policyRedactionPolicy

The active policy. Unused; present for protocol conformance.

Yields:
Span

Matches in ascending start order, longest alternative first at any given position.

Parameters:
Return type:

Iterator[Span]

ignore_case#

!! processed by numpydoc !!

kind#

!! processed by numpydoc !!

name#

!! processed by numpydoc !!

priority#

!! processed by numpydoc !!

reads_view = True#

may read the detection view.

Type:

Offsets index the text given to detect

terms: tuple[str, ...]#

!! processed by numpydoc !!

word_boundary#

!! processed by numpydoc !!