-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: add no-op NLP engine #2071
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
Open
ultramancode
wants to merge
4
commits into
data-privacy-stack:main
Choose a base branch
from
ultramancode:feature/no-op-nlp-engine
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,4 @@ | ||
| nlp_engine_name: no_op | ||
| models: | ||
| - lang_code: en | ||
| model_name: no_op | ||
This file contains hidden or 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
This file contains hidden or 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
168 changes: 168 additions & 0 deletions
168
presidio-analyzer/presidio_analyzer/nlp_engine/no_op_nlp_engine.py
This file contains hidden or 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,168 @@ | ||
| import logging | ||
| from typing import Any, Dict, Iterable, Iterator, List, Tuple, Union | ||
|
|
||
| from spacy.tokens import Doc | ||
| from spacy.vocab import Vocab | ||
|
|
||
| from presidio_analyzer.nlp_engine import NlpArtifacts, NlpEngine | ||
|
|
||
| logger = logging.getLogger("presidio-analyzer") | ||
|
|
||
|
|
||
| class NoOpNlpEngine(NlpEngine): | ||
| """An NLP engine that performs no linguistic processing. | ||
|
|
||
| This engine is intended for AnalyzerEngine configurations where all active | ||
| recognizers are self-contained and do not require NLP artifacts. It loads no | ||
| external model and returns empty artifacts for each analyzed text. | ||
| The default LemmaContextAwareEnhancer can use context passed explicitly to | ||
| ``AnalyzerEngine.analyze()``, but cannot use context words from the analyzed | ||
| text because this engine produces no tokens or lemmas. | ||
| """ | ||
|
|
||
| engine_name = "no_op" | ||
| is_available = True | ||
|
|
||
| def __init__( | ||
| self, | ||
| models: List[Dict[str, str]], | ||
| ): | ||
| self.models = self._validate_models(models) | ||
| self._vocab = Vocab() | ||
| self.nlp = None | ||
|
|
||
| def load(self) -> None: | ||
| """Mark configured languages as loaded without loading external models.""" | ||
| nlp = {} | ||
| for model in self.models: | ||
| nlp[model["lang_code"]] = None | ||
|
|
||
| self.nlp = nlp | ||
| logger.info("Loaded no-op NLP engine with languages: %s", list(self.nlp.keys())) | ||
|
|
||
| @classmethod | ||
| def _validate_models(cls, models: List[Dict[str, str]]) -> List[Dict[str, str]]: | ||
| if not isinstance(models, list) or not models: | ||
| raise ValueError("models must be a non-empty list") | ||
|
|
||
| seen_languages = set() | ||
| validated_models = [] | ||
| for model in models: | ||
| validated_model = cls._validate_model_params(model) | ||
| language = validated_model["lang_code"] | ||
| if language in seen_languages: | ||
| raise ValueError( | ||
| f"Duplicate language '{language}' in no-op NLP engine models" | ||
| ) | ||
| seen_languages.add(language) | ||
| validated_models.append(validated_model) | ||
|
|
||
| return validated_models | ||
|
|
||
| @classmethod | ||
| def _validate_model_params(cls, model: Dict) -> Dict[str, str]: | ||
| """Validate that required model parameters are present.""" | ||
| if not isinstance(model, dict): | ||
| raise ValueError("Each model must be a dictionary") | ||
| if "lang_code" not in model: | ||
| raise ValueError("lang_code is missing from model configuration") | ||
| if "model_name" not in model: | ||
| raise ValueError("model_name is missing from model configuration") | ||
| if not isinstance(model["lang_code"], str) or not model["lang_code"]: | ||
| raise ValueError("lang_code must be a non-empty string") | ||
| if model["lang_code"] != model["lang_code"].strip(): | ||
| raise ValueError("lang_code must not contain leading or trailing spaces") | ||
| if not isinstance(model["model_name"], str): | ||
| raise ValueError("model_name must be a string") | ||
| return {"lang_code": model["lang_code"], "model_name": model["model_name"]} | ||
|
|
||
| def is_loaded(self) -> bool: | ||
| """Return True if the no-op engine has been initialized.""" | ||
| return self.nlp is not None | ||
|
|
||
| def process_text(self, text: str, language: str) -> NlpArtifacts: | ||
| """Return empty NLP artifacts for the given text and language.""" | ||
| self._validate_string(text, "text") | ||
| self._validate_loaded_language(language) | ||
| return self._create_empty_nlp_artifacts(language) | ||
|
|
||
| def process_batch( | ||
| self, | ||
| texts: Iterable[Union[str, Tuple[str, Any]]], | ||
| language: str, | ||
| batch_size: int = 1, | ||
| n_process: int = 1, | ||
| as_tuples: bool = False, | ||
| **kwargs, | ||
| ) -> Iterator[Union[Tuple[str, NlpArtifacts], Tuple[str, NlpArtifacts, Any]]]: | ||
| """Return empty NLP artifacts for each text in a batch.""" | ||
| if kwargs: | ||
| logger.warning( | ||
| "Ignoring unsupported kwargs in NoOpNlpEngine.process_batch: %s", | ||
| sorted(kwargs.keys()), | ||
| ) | ||
| self._validate_loaded_language(language) | ||
|
|
||
| for item in texts: | ||
| if as_tuples: | ||
| if not isinstance(item, tuple) or len(item) != 2: | ||
| raise ValueError( | ||
| "When 'as_tuples' is True, " | ||
| "'texts' must be an iterable of tuples (text, context)." | ||
| ) | ||
|
ultramancode marked this conversation as resolved.
|
||
| text, context = item | ||
| text = str(text) | ||
| yield text, self._create_empty_nlp_artifacts(language), context | ||
| else: | ||
| text = str(item) | ||
| yield text, self._create_empty_nlp_artifacts(language) | ||
|
|
||
| def is_stopword(self, word: str, language: str) -> bool: | ||
| """Return False because no stop-word vocabulary is available.""" | ||
| self._validate_string(word, "word") | ||
| self._validate_loaded_language(language) | ||
| return False | ||
|
|
||
| def is_punct(self, word: str, language: str) -> bool: | ||
| """Return False because no punctuation vocabulary is available.""" | ||
| self._validate_string(word, "word") | ||
| self._validate_loaded_language(language) | ||
| return False | ||
|
|
||
| def get_supported_entities(self) -> List[str]: | ||
| """Return no entities because this engine does not run NER.""" | ||
| return [] | ||
|
|
||
| def get_supported_languages(self) -> List[str]: | ||
| """Return the languages configured for this no-op engine.""" | ||
| if not self.nlp: | ||
| raise ValueError("NLP engine is not loaded. Consider calling .load()") | ||
| return list(self.nlp.keys()) | ||
|
|
||
| def _validate_loaded_language(self, language: str) -> None: | ||
| self._validate_string(language, "language") | ||
| if not self.nlp: | ||
| raise ValueError("NLP engine is not loaded. Consider calling .load()") | ||
|
|
||
| if language not in self.nlp: | ||
| raise ValueError( | ||
| f"Language '{language}' is not supported by this NLP engine. " | ||
| f"Supported languages: {list(self.nlp.keys())}" | ||
| ) | ||
|
|
||
| @staticmethod | ||
| def _validate_string(value: Any, parameter_name: str) -> str: | ||
| if not isinstance(value, str): | ||
| raise TypeError(f"{parameter_name} must be a string") | ||
| return value | ||
|
|
||
| def _create_empty_nlp_artifacts(self, language: str) -> NlpArtifacts: | ||
| return NlpArtifacts( | ||
| entities=[], | ||
| tokens=Doc(self._vocab, words=[]), | ||
| tokens_indices=[], | ||
| lemmas=[], | ||
| nlp_engine=self, | ||
| language=language, | ||
| scores=[], | ||
| ) | ||
|
Comment on lines
+159
to
+168
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
This file contains hidden or 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
This file contains hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.