03-pseudonymize-data-in-power-bi-python_1.py 976 B

12345678910111213141516171819202122232425262728
  1. def anonymizeEmail(text_to_anonymize, country):
  2. # Initialize Faker
  3. fake = Faker(faker_locales_dict[country])
  4. fake.add_provider(internet)
  5. analyzer_results = analyzer.analyze(text=text_to_anonymize, entities=["EMAIL_ADDRESS"], language='en')
  6. matched_emails = {}
  7. for match in analyzer_results:
  8. email = text_to_anonymize[match.start:match.end]
  9. if email not in emails_dict:
  10. fake_email = fake.safe_email()
  11. while (fake_email in emails_dict.values()) or (fake_email in emails_dict):
  12. fake_email = fake.safe_email()
  13. emails_dict[email] = fake_email
  14. matched_emails[email] = fake_email
  15. else:
  16. fake_email = emails_dict[email]
  17. matched_emails[email] = fake_email
  18. anonymized_result = text_to_anonymize
  19. for email in matched_emails:
  20. anonymized_result = anonymized_result.replace(email, matched_emails[email])
  21. return anonymized_result