Spaces:
Running
Running
File size: 11,605 Bytes
846b4a5 |
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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
from pprint import pprint
import json
from presidio_analyzer import AnalyzerEngine, RecognizerRegistry
from presidio_analyzer.nlp_engine import NlpEngineProvider, NlpArtifacts
from presidio_analyzer import PatternRecognizer
from presidio_analyzer import Pattern, PatternRecognizer
from presidio_analyzer.predefined_recognizers import SpacyRecognizer
from presidio_analyzer.predefined_recognizers import IbanRecognizer, EmailRecognizer, IpRecognizer,\
EmailRecognizer, PhoneRecognizer, UrlRecognizer, DateRecognizer
from presidio_anonymizer import AnonymizerEngine
from presidio_anonymizer.entities import OperatorConfig
import logging
from typing import Optional, List, Tuple, Set
from presidio_analyzer import (
RecognizerResult,
EntityRecognizer,
AnalysisExplanation,
)
from flair.data import Sentence
from flair.models import SequenceTagger
### Creating FlairRecognizer class for NER(names, location)
class FlairRecognizer(EntityRecognizer):
ENTITIES = [
"LOCATION",
"PERSON",
"ORGANIZATION",
# "MISCELLANEOUS" # - There are no direct correlation with Presidio entities.
]
DEFAULT_EXPLANATION = "Identified as {} by Flair's Named Entity Recognition"
CHECK_LABEL_GROUPS = [
({"LOCATION"}, {"LOC", "LOCATION"}),
({"PERSON"}, {"PER", "PERSON"}),
({"ORGANIZATION"}, {"ORG"}),
# ({"MISCELLANEOUS"}, {"MISC"}), # Probably not PII
]
MODEL_LANGUAGES = {
#"en": "flair/ner-english-large",
#"es": "flair/ner-spanish-large",
"de": "flair/ner-german-large",
#"nl": "flair/ner-dutch-large",
}
PRESIDIO_EQUIVALENCES = {
"PER": "PERSON",
"LOC": "LOCATION",
"ORG": "ORGANIZATION",
# 'MISC': 'MISCELLANEOUS' # - Probably not PII
}
def __init__(
self,
supported_language: str = "en",
supported_entities: Optional[List[str]] = None,
check_label_groups: Optional[Tuple[Set, Set]] = None,
model: SequenceTagger = None,
):
self.check_label_groups = (
check_label_groups if check_label_groups else self.CHECK_LABEL_GROUPS
)
supported_entities = supported_entities if supported_entities else self.ENTITIES
self.model = (
model
if model
else SequenceTagger.load(self.MODEL_LANGUAGES.get(supported_language))
)
super().__init__(
supported_entities=supported_entities,
supported_language=supported_language,
name="Flair Analytics",
)
print("Flair class initialized")
def load(self) -> None:
"""Load the model, not used. Model is loaded during initialization."""
pass
def get_supported_entities(self) -> List[str]:
"""
Return supported entities by this model.
:return: List of the supported entities.
"""
return self.supported_entities
# Class to use Flair with Presidio as an external recognizer.
def analyze(
self, text: str, entities: List[str], nlp_artifacts: NlpArtifacts = None
) -> List[RecognizerResult]:
"""
Analyze text using Text Analytics.
:param text: The text for analysis.
:param entities: Not working properly for this recognizer.
:param nlp_artifacts: Not used by this recognizer.
:param language: Text language. Supported languages in MODEL_LANGUAGES
:return: The list of Presidio RecognizerResult constructed from the recognized
Flair detections.
"""
results = []
sentences = Sentence(text)
self.model.predict(sentences)
# If there are no specific list of entities, we will look for all of it.
if not entities:
entities = self.supported_entities
for entity in entities:
if entity not in self.supported_entities:
continue
for ent in sentences.get_spans("ner"):
if not self.__check_label(
entity, ent.labels[0].value, self.check_label_groups
):
continue
textual_explanation = self.DEFAULT_EXPLANATION.format(
ent.labels[0].value
)
explanation = self.build_flair_explanation(
round(ent.score, 2), textual_explanation
)
flair_result = self._convert_to_recognizer_result(ent, explanation)
results.append(flair_result)
return results
def _convert_to_recognizer_result(self, entity, explanation) -> RecognizerResult:
entity_type = self.PRESIDIO_EQUIVALENCES.get(entity.tag, entity.tag)
flair_score = round(entity.score, 2)
flair_results = RecognizerResult(
entity_type=entity_type,
start=entity.start_position,
end=entity.end_position,
score=flair_score,
analysis_explanation=explanation,
)
return flair_results
def build_flair_explanation(
self, original_score: float, explanation: str
) -> AnalysisExplanation:
"""
Create explanation for why this result was detected.
:param original_score: Score given by this recognizer
:param explanation: Explanation string
:return:
"""
explanation = AnalysisExplanation(
recognizer=self.__class__.__name__,
original_score=original_score,
textual_explanation=explanation,
)
return explanation
@staticmethod
def __check_label(
entity: str, label: str, check_label_groups: Tuple[Set, Set]
) -> bool:
return any(
[entity in egrp and label in lgrp for egrp, lgrp in check_label_groups]
)
class PIIService:
def __init__(self):
configuration = {
"nlp_engine_name": "spacy",
"models": [
{"lang_code": "de", "model_name": "de_core_news_sm"}
],
}
# Create NLP engine based on configuration
provider = NlpEngineProvider(nlp_configuration=configuration)
nlp_engine = provider.create_engine()
## Creating regex for PatternRecognizers - SWIFT, vehicle number, zipcode, ssn
swift_regex = r"\b[A-Z]{4}DE[A-Z0-9]{2}(?:[A-Z0-9]{3})?"
vehicle_number_with_hyphen_regex = r"\b[A-ZÄÖÜ]{1,3}-[A-ZÄÖÜ]{1,2}-[0-9]{1,4}"
vehicle_number_without_hyphen_regex = r"\b[A-ZÄÖÜ]{1,3}[A-ZÄÖÜ]{1,2}[0-9]{1,4}"
german_zipcode_regex = r"\b((?:0[1-46-9]\d{3})|(?:[1-357-9]\d{4})|(?:[4][0-24-9]\d{3})|(?:[6][013-9]\d{3}))\b(?![\d/])"
german_ssn_regex = r"\b\d{2}\s?\d{6}\s?[A-Z]\s?\d{3}\b"
# Creating Presidio pattern object
vehicle_numbers_pattern1 = Pattern(name="vehicle_pattern", regex=vehicle_number_without_hyphen_regex, score=1)
vehicle_numbers_pattern2 = Pattern(name="vehicle_pattern", regex=vehicle_number_with_hyphen_regex, score=1)
swift_pattern = Pattern(name="bank_swift_pattern", regex=swift_regex, score=1)
germanzipcode_pattern = Pattern(name="german_zip_pattern",regex=german_zipcode_regex, score=1)
german_ssn_pattern = Pattern(name="german_ssn_pattern",regex=german_ssn_regex, score=1)
# Define the recognizer
swift_recognizer = PatternRecognizer(supported_entity="SWIFT", supported_language="de",patterns=[swift_pattern])
vehicle_number_recognizer = PatternRecognizer(supported_entity="VEHICLE_NUMBER", supported_language="de",patterns=[vehicle_numbers_pattern1,vehicle_numbers_pattern2])
germanzip_recognizer = PatternRecognizer(supported_entity="GERMAN_ZIP", supported_language="de",patterns=[germanzipcode_pattern])
germanssn_recognizer = PatternRecognizer(supported_entity="GERMAN_SSN", supported_language="de",patterns=[german_ssn_pattern])
## Lading flair entity model for person, location ID
print("Loading flair")
flair_recognizer = FlairRecognizer(supported_language="de")
print("Flair loaded")
registry = RecognizerRegistry()
#registry.load_predefined_recognizers()
#registry.add_recognizer(SpacyRecognizer(supported_language="de"))
#registry.add_recognizer(SpacyRecognizer(supported_language="en"))
registry.remove_recognizer("SpacyRecognizer")
registry.add_recognizer(flair_recognizer)
registry.add_recognizer(swift_recognizer)
registry.add_recognizer(vehicle_number_recognizer)
registry.add_recognizer(germanzip_recognizer)
registry.add_recognizer(germanssn_recognizer)
## Adding predefined recognizers
registry.add_recognizer(IbanRecognizer(supported_language="de"))
registry.add_recognizer(DateRecognizer(supported_language="de"))
registry.add_recognizer(EmailRecognizer(supported_language="de"))
registry.add_recognizer(IpRecognizer(supported_language="de"))
registry.add_recognizer(PhoneRecognizer(supported_language="de"))
registry.add_recognizer(UrlRecognizer(supported_language="de"))
registry.add_recognizer(PhoneRecognizer(supported_language="de"))
print("Recognizer registry loaded")
self.analyzer = AnalyzerEngine(registry=registry, nlp_engine=nlp_engine, supported_languages=["de"])
#print(f"Type of recognizers ::\n {self.analyzer.registry.recognizers}")
print("PII initialized")
self.anonymizer = AnonymizerEngine()
def identify(self, text):
results_de = self.analyzer.analyze(
text,
language='de'
)
#anonymized_results = self.anonymize(results_de, text)
entities = []
for result in results_de:
result_dict = result.to_dict()
temp_entity = {
"start":result_dict['start'],
"end":result_dict['end'],
"entity_type":result_dict['entity_type'],
"score":result_dict['score'],
"word":text[result_dict['start']:result_dict['end']]
}
entities.append(temp_entity)
return {"entities":entities, "text":text}#, "anonymized_text":anonymized_results['text']}
"""def anonymize(self, entities, text):
anonymized_results = self.anonymizer.anonymize(
text=text,
analyzer_results=entities,
#operators={"DEFAULT": OperatorConfig("replace", {"new_value": "<ANONYMIZED>"})},
)
return ""#json.loads(anonymized_results.to_json())"""
def add_mask(self, data):
masked_data = []
entity_count = {}
for item_idx,item in enumerate(data['entities']):
entity_type = item['entity_type']
word = item['word']
suffix = entity_count.get(entity_type, 0) + 1
entity_count[entity_type] = suffix
masked_word = f"{entity_type}_{suffix}"
item['mask'] = masked_word
#data['entities'][item_idx]['mask'] = masked_word
masked_data.append(item)
return masked_data
def anonymize(self, entities, text):
print("anonymyzing")
updated_text = text
for ent_idx, ent in enumerate(entities):
#text[ent['start']:ent['end']] = ent['mask']
updated_text = updated_text[:ent['start']] + " " + ent['mask'] + " " + updated_text[ent['end']:]
return updated_text
def remove_overlapping_entities(entities):
return |