Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def __init__(
supported_entities: Optional[List[str]] = None,
name: str = "MedicalNERRecognizer",
supported_language: str = "en",
context: Optional[List[str]] = None,
aggregation_strategy: str = "simple",
threshold: float = 0.3,
device: Optional[Union[str, int]] = None,
Expand All @@ -50,6 +51,7 @@ def __init__(
:param supported_entities: Entity types to return (None = all mapped).
:param name: Recognizer name
:param supported_language: Language code
:param context: Context words used for context-aware score enhancement
:param aggregation_strategy: Pipeline aggregation strategy
:param threshold: Minimum confidence score (0.0 - 1.0)
:param device: Device string/int (None = auto-detect)
Expand All @@ -61,6 +63,7 @@ def __init__(
supported_entities=supported_entities,
name=name,
supported_language=supported_language,
context=context,
aggregation_strategy=aggregation_strategy,
threshold=threshold,
device=device,
Expand Down
32 changes: 32 additions & 0 deletions presidio-analyzer/tests/test_medical_ner_recognizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
from presidio_analyzer.predefined_recognizers.ner.huggingface_ner_recognizer import (
HuggingFaceNerRecognizer,
)
from presidio_analyzer.recognizer_registry.recognizers_loader_utils import (
RecognizerListLoader,
)


def _make_pipeline_prediction(entity_group, score, word, start, end):
Expand Down Expand Up @@ -86,6 +89,35 @@ def test_default_aggregation_strategy(recognizer):
assert recognizer.aggregation_strategy == "simple"


def test_loader_with_language_context():
"""YAML loader should be able to pass language context."""
recognizer_conf = {
"name": "MedicalNERRecognizer",
"type": "predefined",
"supported_languages": [
{"language": "en", "context": ["diagnosis", "medication"]}
],
}

with (
patch(_PATCH_HF, new=MagicMock()),
patch(_PATCH_TORCH, new=MagicMock()),
patch(_PATCH_DEVICE) as mock_dd,
):
mock_dd.get_device.return_value = "cpu"
recognizers = list(
RecognizerListLoader.get(
recognizers=[recognizer_conf],
supported_languages=["en"],
global_regex_flags=26,
)
)

assert len(recognizers) == 1
assert isinstance(recognizers[0], MedicalNERRecognizer)
assert recognizers[0].context == ["diagnosis", "medication"]


def test_custom_supported_entities():
"""Users can override supported_entities to filter which types are returned."""
rec = _make_recognizer(
Expand Down
Loading