def test_given_several_anonymizers_then_we_use_the_correct_one(): analyzer_result = AnalyzerResult.from_json({ "score": 0.5, "entity_type": "PHONE_NUMBER", "start": 8, "end": 18 }) anonymizer_config = AnonymizerConfig("replace", {}) anonymizer_config.anonymizer_class = MockAnonymizer text = AnonymizerEngine().anonymize("Number: 0554555556", [analyzer_result], {"PHONE_NUMBER": anonymizer_config}) assert text == "Number: I am your new text!"
def __extract_anonymizer_and_anonymize( self, entity_type: str, anonymizer_config: AnonymizerConfig, text_to_anonymize: str, ) -> str: self.logger.debug(f"getting anonymizer for {entity_type}") anonymizer = anonymizer_config.anonymizer_class() self.logger.debug( f"validating anonymizer {anonymizer} for {entity_type}") anonymizer.validate(params=anonymizer_config.params) params = anonymizer_config.params params["entity_type"] = entity_type self.logger.debug(f"anonymizing {entity_type} with {anonymizer}") anonymized_text = anonymizer.anonymize(params=params, text=text_to_anonymize) return anonymized_text