03-pseudonymize-data-in-power-bi-python_2.py 897 B

123456789101112131415161718192021222324252627
  1. def anonymizeName(text_to_anonymize, country):
  2. # Initialize Faker
  3. fake = Faker(faker_locales_dict[country])
  4. analyzer_results = analyzer.analyze(text=text_to_anonymize, entities=["PERSON"], language='en')
  5. matched_names = {}
  6. for match in analyzer_results:
  7. name = text_to_anonymize[match.start:match.end]
  8. if name not in names_dict:
  9. fake_name = fake.name()
  10. while (fake_name in names_dict.values()) or (fake_name in names_dict):
  11. fake_name = fake.name()
  12. names_dict[name] = fake_name
  13. matched_names[name] = fake_name
  14. else:
  15. fake_name = names_dict[name]
  16. matched_names[name] = fake_name
  17. anonymized_result = text_to_anonymize
  18. for name in matched_names:
  19. anonymized_result = anonymized_result.replace(name, matched_names[name])
  20. return anonymized_result