File size: 1,811 Bytes
ed4d993
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from abc import ABC, abstractmethod
from typing import Callable, List, Optional

from langchain_experimental.data_anonymizer.deanonymizer_mapping import MappingDataType
from langchain_experimental.data_anonymizer.deanonymizer_matching_strategies import (
    exact_matching_strategy,
)

DEFAULT_DEANONYMIZER_MATCHING_STRATEGY = exact_matching_strategy


class AnonymizerBase(ABC):
    """Base abstract class for anonymizers.

    It is public and non-virtual because it allows
        wrapping the behavior for all methods in a base class.
    """

    def anonymize(
        self,
        text: str,
        language: Optional[str] = None,
        allow_list: Optional[List[str]] = None,
    ) -> str:
        """Anonymize text."""

        return self._anonymize(text, language, allow_list)

    @abstractmethod
    def _anonymize(
        self, text: str, language: Optional[str], allow_list: Optional[List[str]] = None
    ) -> str:
        """Abstract method to anonymize text"""


class ReversibleAnonymizerBase(AnonymizerBase):
    """
    Base abstract class for reversible anonymizers.
    """

    def deanonymize(
        self,
        text_to_deanonymize: str,
        deanonymizer_matching_strategy: Callable[
            [str, MappingDataType], str
        ] = DEFAULT_DEANONYMIZER_MATCHING_STRATEGY,
    ) -> str:
        """Deanonymize text"""
        return self._deanonymize(text_to_deanonymize, deanonymizer_matching_strategy)

    @abstractmethod
    def _deanonymize(
        self,
        text_to_deanonymize: str,
        deanonymizer_matching_strategy: Callable[[str, MappingDataType], str],
    ) -> str:
        """Abstract method to deanonymize text"""

    @abstractmethod
    def reset_deanonymizer_mapping(self) -> None:
        """Abstract method to reset deanonymizer mapping"""