-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
674 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from .presidio_analyser import PresidioAnalyserClassifier | ||
|
||
__all__ = [ | ||
"PresidioAnalyserClassifier", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from dataclasses import dataclass | ||
from typing import List | ||
|
||
from presidio_analyzer import AnalyzerEngine, RecognizerResult | ||
|
||
from ...core import BaseTextClassifier, Score | ||
|
||
|
||
@dataclass | ||
class PresidioAnalyserClassifier(BaseTextClassifier[List[RecognizerResult]]): | ||
language: str = "en" | ||
entities: List[str] | None = None | ||
threshold: float = 0.7 | ||
|
||
def __post_init__(self) -> None: | ||
# Set up the engine, loads the NLP module (spaCy model by default) and other PII recognizers | ||
self._analyzer = AnalyzerEngine(default_score_threshold=self.threshold) | ||
|
||
def score(self, input: str) -> Score[List[RecognizerResult]]: | ||
# Call analyzer to get results | ||
results = self._analyzer.analyze(text=input, entities=self.entities, language=self.language) | ||
|
||
return Score[List[RecognizerResult]]( | ||
flagged=len(results) > 0, | ||
value=results, | ||
description="Return True if entities are found in the input", | ||
explanation=( | ||
f"Found {len(results)} entities in input" if len(results) > 0 else "Did not find entities in input" | ||
), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.