Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NEW: Add PII classifier powered by presidio #54

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions medkit/text/deid.py
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},
)
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ dependencies = [
]

[project.optional-dependencies]
deid = [
"presidio-analyzer >=2.2.33,<3",
]
edsnlp = [
"edsnlp>=0.9",
]
Expand Down Expand Up @@ -140,6 +143,7 @@ webrtc-voice-detector = [
]
all = [
"""medkit-lib[\
deid,\
edsnlp,\
hf-entity-matcher,\
hf-transcriber,\
Expand Down