File size: 6,041 Bytes
6fad24e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
from presidio_analyzer import AnalyzerEngine
from presidio_anonymizer import AnonymizerEngine, DeanonymizeEngine, OperatorConfig
from presidio_anonymizer.operators import Operator, OperatorType

from typing import Dict
from pprint import pprint

# text = "Peter gave his book to Heidi which later gave it to Nicole. Peter lives in London and Nicole lives in Tashkent."
# print("original text:")
# pprint(text)
# analyzer = AnalyzerEngine()
# analyzer_results = analyzer.analyze(text=text, language="en")
# # print("analyzer results:")
# # pprint(analyzer_results)

class InstanceCounterAnonymizer(Operator):
    """
    Anonymizer which replaces the entity value
    with an instance counter per entity.
    """

    REPLACING_FORMAT = "<{entity_type}_{index}>"

    def operate(self, text: str, params: Dict = None) -> str:
        """Anonymize the input text."""

        entity_type: str = params["entity_type"]

        # entity_mapping is a dict of dicts containing mappings per entity type
        entity_mapping: Dict[Dict:str] = params["entity_mapping"]

        entity_mapping_for_type = entity_mapping.get(entity_type)
        if not entity_mapping_for_type:
            new_text = self.REPLACING_FORMAT.format(
                entity_type=entity_type, index=0
            )
            entity_mapping[entity_type] = {}

        else:
            if text in entity_mapping_for_type:
                return entity_mapping_for_type[text]

            previous_index = self._get_last_index(entity_mapping_for_type)
            new_text = self.REPLACING_FORMAT.format(
                entity_type=entity_type, index=previous_index + 1
            )

        entity_mapping[entity_type][text] = new_text
        return new_text

    @staticmethod
    def _get_last_index(entity_mapping_for_type: Dict) -> int:
        """Get the last index for a given entity type."""

        def get_index(value: str) -> int:
            return int(value.split("_")[-1][:-1])

        indices = [get_index(v) for v in entity_mapping_for_type.values()]
        return max(indices)

    def validate(self, params: Dict = None) -> None:
        """Validate operator parameters."""

        if "entity_mapping" not in params:
            raise ValueError("An input Dict called `entity_mapping` is required.")
        if "entity_type" not in params:
            raise ValueError("An entity_type param is required.")

    def operator_name(self) -> str:
        return "entity_counter"

    def operator_type(self) -> OperatorType:
        return OperatorType.Anonymize
    

# Create Anonymizer engine and add the custom anonymizer
# anonymizer_engine = AnonymizerEngine()
# anonymizer_engine.add_anonymizer(InstanceCounterAnonymizer)

# # Create a mapping between entity types and counters
# entity_mapping = dict()

# # Anonymize the text

# anonymized_result = anonymizer_engine.anonymize(
#     text,
#     analyzer_results,
#     {
#         "DEFAULT": OperatorConfig(
#             "entity_counter", {"entity_mapping": entity_mapping}
#         )
#     },
# )

# print(anonymized_result.text)
# pprint(entity_mapping, indent=2)


# class InstanceCounterDeanonymizer(Operator):
#     """
#     Deanonymizer which replaces the unique identifier 
#     with the original text.
#     """

#     def operate(self, text: str, params: Dict = None) -> str:
#         """Anonymize the input text."""

#         entity_type: str = params["entity_type"]

#         # entity_mapping is a dict of dicts containing mappings per entity type
#         entity_mapping: Dict[Dict:str] = params["entity_mapping"]

#         if entity_type not in entity_mapping:
#             raise ValueError(f"Entity type {entity_type} not found in entity mapping!")
#         if text not in entity_mapping[entity_type].values():
#             raise ValueError(f"Text {text} not found in entity mapping for entity type {entity_type}!")

#         return self._find_key_by_value(entity_mapping[entity_type], text)

#     @staticmethod
#     def _find_key_by_value(entity_mapping, value):
#         for key, val in entity_mapping.items():
#             if val == value:
#                 return key
#         return None
    
#     def validate(self, params: Dict = None) -> None:
#         """Validate operator parameters."""

#         if "entity_mapping" not in params:
#             raise ValueError("An input Dict called `entity_mapping` is required.")
#         if "entity_type" not in params:
#             raise ValueError("An entity_type param is required.")

#     def operator_name(self) -> str:
#         return "entity_counter_deanonymizer"

#     def operator_type(self) -> OperatorType:
#         return OperatorType.Deanonymize
    

# # Create Anonymizer engine and add the custom anonymizer
# anonymizer_engine = AnonymizerEngine()
# anonymizer_engine.add_anonymizer(InstanceCounterAnonymizer)

# # Create a mapping between entity types and counters
# entity_mapping = dict()

# # Anonymize the text

# text = "Peter gave his book to Heidi which later gave it to Nicole. Peter lives in London and Nicole lives in Tashkent."
# print("original text:")
# pprint(text)
# analyzer = AnalyzerEngine()
# analyzer_results = analyzer.analyze(text=text, language="en")
# print("analyzer results:")
# pprint(analyzer_results)

# anonymized_result = anonymizer_engine.anonymize(
#     text,
#     analyzer_results,
#     {
#         "DEFAULT": OperatorConfig(
#             "entity_counter", {"entity_mapping": entity_mapping}
#         )
#     },
# )

# print(anonymized_result)    
    
# deanonymizer_engine = DeanonymizeEngine()
# deanonymizer_engine.add_deanonymizer(InstanceCounterDeanonymizer)

# deanonymized = deanonymizer_engine.deanonymize(
#     anonymized_result.text, 
#     anonymized_result.items, 
#     {"DEFAULT": OperatorConfig("entity_counter_deanonymizer", 
#                                params={"entity_mapping": entity_mapping})}
# )
# print("anonymized text:")
# pprint(anonymized_result.text)
# print("de-anonymized text:")
# pprint(deanonymized.text)