def anonymizeEmail(text_to_anonymize, country): # Initialize Faker fake = Faker(faker_locales_dict[country]) fake.add_provider(internet) analyzer_results = analyzer.analyze(text=text_to_anonymize, entities=["EMAIL_ADDRESS"], language='en') matched_emails = {} for match in analyzer_results: email = text_to_anonymize[match.start:match.end] if email not in emails_dict: fake_email = fake.safe_email() while (fake_email in emails_dict.values()) or (fake_email in emails_dict): fake_email = fake.safe_email() emails_dict[email] = fake_email matched_emails[email] = fake_email else: fake_email = emails_dict[email] matched_emails[email] = fake_email anonymized_result = text_to_anonymize for email in matched_emails: anonymized_result = anonymized_result.replace(email, matched_emails[email]) return anonymized_result