langchain/libs/experimental/langchain_experimental/data_anonymizer/base.py
maks-operlejn-ds 4d62def9ff
Better deanonymizer matching strategy (#11557)
@baskaryan, @hwchase17
2023-10-09 11:10:29 -07:00

50 lines
1.5 KiB
Python

from abc import ABC, abstractmethod
from typing import Callable, 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) -> str:
"""Anonymize text"""
return self._anonymize(text, language)
@abstractmethod
def _anonymize(self, text: str, language: Optional[str]) -> 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"""