|
| 1 | +"""Utility methods for working with PII data. |
| 2 | +
|
| 3 | +Python port of UiPath.SemanticProxy.Client.PiiUtilities (C#). |
| 4 | +""" |
| 5 | + |
| 6 | +import re |
| 7 | +from typing import Callable, Iterable |
| 8 | + |
| 9 | +from .pii_detection import PiiDetectionResponse, PiiEntity |
| 10 | + |
| 11 | + |
| 12 | +def rehydrate_from_pii_entities( |
| 13 | + masked_text: str, pii_entities: Iterable[PiiEntity] |
| 14 | +) -> str: |
| 15 | + """Rehydrate masked text by replacing PII placeholders with original values. |
| 16 | +
|
| 17 | + Placeholders (e.g. ``[Person-1]``) are matched case-insensitively and replaced |
| 18 | + with the corresponding original PII text. The function also replaces variants |
| 19 | + without the surrounding brackets (e.g. ``Person-1``) in case the LLM stripped |
| 20 | + them in its output. |
| 21 | +
|
| 22 | + Args: |
| 23 | + masked_text: The masked text with PII placeholders. |
| 24 | + pii_entities: The PII entities containing the original values. |
| 25 | +
|
| 26 | + Returns: |
| 27 | + The rehydrated text with original PII values. |
| 28 | + """ |
| 29 | + if not masked_text: |
| 30 | + return masked_text |
| 31 | + |
| 32 | + entities = [e for e in pii_entities if e.replacement_text] |
| 33 | + if not entities: |
| 34 | + return masked_text |
| 35 | + |
| 36 | + # Sort by replacement text length descending to avoid substring collisions |
| 37 | + # (e.g. "[Person-10]" must be replaced before "[Person-1]"). |
| 38 | + entities.sort(key=lambda e: len(e.replacement_text), reverse=True) |
| 39 | + |
| 40 | + rehydrated = masked_text |
| 41 | + for entity in entities: |
| 42 | + if not entity.replacement_text or not entity.pii_text: |
| 43 | + continue |
| 44 | + # Replace the full placeholder (with brackets) case-insensitively. |
| 45 | + # ``_literal_replacer`` bypasses regex backreference interpretation in the |
| 46 | + # replacement string. |
| 47 | + rehydrated = re.sub( |
| 48 | + re.escape(entity.replacement_text), |
| 49 | + _literal_replacer(entity.pii_text), |
| 50 | + rehydrated, |
| 51 | + flags=re.IGNORECASE, |
| 52 | + ) |
| 53 | + # Also replace the content without brackets (in case the LLM dropped them). |
| 54 | + if entity.replacement_text.startswith("[") and entity.replacement_text.endswith( |
| 55 | + "]" |
| 56 | + ): |
| 57 | + no_brackets = entity.replacement_text[1:-1] |
| 58 | + rehydrated = re.sub( |
| 59 | + re.escape(no_brackets), |
| 60 | + _literal_replacer(entity.pii_text), |
| 61 | + rehydrated, |
| 62 | + flags=re.IGNORECASE, |
| 63 | + ) |
| 64 | + |
| 65 | + return rehydrated |
| 66 | + |
| 67 | + |
| 68 | +def _literal_replacer(replacement: str) -> Callable[[re.Match[str]], str]: |
| 69 | + """Return a replacement function that ignores regex backreference syntax.""" |
| 70 | + |
| 71 | + def replace(_match: re.Match[str]) -> str: |
| 72 | + return replacement |
| 73 | + |
| 74 | + return replace |
| 75 | + |
| 76 | + |
| 77 | +def rehydrate_from_pii_response( |
| 78 | + masked_text: str, response: PiiDetectionResponse |
| 79 | +) -> str: |
| 80 | + """Rehydrate masked text using all PII entities from a detection response. |
| 81 | +
|
| 82 | + Merges entities from both ``response.response`` (detected in documents/prompts) |
| 83 | + and ``response.files`` (detected in files), so placeholders originating from |
| 84 | + either source are rehydrated. |
| 85 | +
|
| 86 | + Args: |
| 87 | + masked_text: The masked text with PII placeholders. |
| 88 | + response: The PII detection response containing entities to rehydrate. |
| 89 | +
|
| 90 | + Returns: |
| 91 | + The rehydrated text with original PII values. |
| 92 | + """ |
| 93 | + entities: list[PiiEntity] = [] |
| 94 | + for doc in response.response: |
| 95 | + entities.extend(doc.pii_entities) |
| 96 | + for file in response.files: |
| 97 | + entities.extend(file.pii_entities) |
| 98 | + return rehydrate_from_pii_entities(masked_text, entities) |
0 commit comments