-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NEW: Add PII classifier powered by presidio
- Loading branch information
Showing
2 changed files
with
37 additions
and
1 deletion.
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,37 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING, Iterator | ||
|
||
from medkit._import import import_optional | ||
from medkit.core.text import Entity, span_utils | ||
from medkit.core.text.operation import NEROperation | ||
|
||
if TYPE_CHECKING: | ||
from medkit.core.text import Segment | ||
|
||
presidio_analyzer = import_optional("presidio_analyzer", extra="deid") | ||
|
||
__all__ = ["PIIClassifier"] | ||
|
||
|
||
class PIIClassifier(NEROperation): | ||
"""Classify sensitive text information.""" | ||
|
||
def __init__(self, uid: str | None = None, name: str | None = None, **kwargs): | ||
super().__init__(uid=uid, name=name, **kwargs) | ||
self._analyzer = presidio_analyzer.AnalyzerEngine() | ||
|
||
def run(self, segments: list[Segment]) -> list[Entity]: | ||
return [entity for segment in segments for entity in self._run_one(segment)] | ||
|
||
def _run_one(self, segment: Segment) -> Iterator[Entity]: | ||
for result in self._analyzer.analyze(text=segment.text, language="en"): | ||
text, spans = span_utils.extract( | ||
text=segment.text, spans=segment.spans, ranges=[(result.start, result.end)] | ||
) | ||
yield Entity( | ||
label=result.entity_type, | ||
text=text, | ||
spans=spans, | ||
metadata={"score": result.score}, | ||
) |
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