Skip to content

Ad-hoc recognizers are not asked for their own entities when entities is omitted #2425

Description

@RaphaelFakhri

Describe the bug
When AnalyzerEngine.analyze is called without entities, it builds the entity list from the registry only (self.get_supported_entities(language)) and passes that list to every recognizer, including the ad-hoc ones. An ad-hoc recognizer that filters its output by the entities argument never sees its own entity names, so it returns nothing.

PatternRecognizer ignores the entities argument, so ad-hoc pattern and deny-list recognizers are not affected. Recognizers that honor the argument are, for example GLiNERRecognizer (if entities and presidio_entity not in entities: continue), LMRecognizer subclasses (requested_entities = [e for e in entities if e in self.supported_entities]) and custom EntityRecognizer subclasses that follow the analyze(text, entities, nlp_artifacts) contract.

Passing the same recognizer with entities=["AD_HOC_ENTITY"] works, so the result depends on whether the caller lists entities explicitly.

To reproduce

from typing import List

from presidio_analyzer import AnalyzerEngine, EntityRecognizer, RecognizerResult


class AdHocRecognizer(EntityRecognizer):
    def __init__(self):
        super().__init__(supported_entities=["AD_HOC_ENTITY"])

    def load(self):
        pass

    def analyze(self, text: str, entities: List[str], nlp_artifacts=None):
        if "AD_HOC_ENTITY" not in entities:
            return []
        return [RecognizerResult("AD_HOC_ENTITY", 0, 4, 0.8)]


analyzer = AnalyzerEngine()
print(analyzer.analyze("John lives here", language="en",
                       ad_hoc_recognizers=[AdHocRecognizer()]))
# AD_HOC_ENTITY is missing from the results
print(analyzer.analyze("John lives here", language="en",
                       ad_hoc_recognizers=[AdHocRecognizer()],
                       entities=["AD_HOC_ENTITY"]))
# AD_HOC_ENTITY is returned

Expected behavior
With entities omitted, every recognizer is asked for all entities supported by the recognizers used in the request, ad-hoc ones included, so the first call also returns AD_HOC_ENTITY.

Additional context
Checked on main at 211afc3.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions