-
Notifications
You must be signed in to change notification settings - Fork 0
Fix handling of non-alphanumeric entities in cleaned text #81
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,9 @@ | ||
| import re | ||
| from copy import deepcopy | ||
| from string import punctuation | ||
|
|
||
| from aymurai.meta.pipeline_interfaces import Transform | ||
| from aymurai.meta.types import DataItem | ||
| from aymurai.utils.misc import get_element | ||
| from aymurai.meta.pipeline_interfaces import Transform | ||
|
|
||
|
|
||
| class AnonymizationEntityCleaner(Transform): | ||
|
|
@@ -45,6 +44,9 @@ def process(self, ent: dict) -> dict: | |
| # Clean the text | ||
| cleaned_text = pattern.sub("", original_text) | ||
|
|
||
| if not cleaned_text: | ||
| return None | ||
|
|
||
|
Comment on lines
+47
to
+49
|
||
| # Update the entity's alt text and indices | ||
| ent["attrs"]["aymurai_alt_text"] = cleaned_text | ||
| ent["attrs"]["aymurai_alt_start_char"] = start_char + leading_chars_removed | ||
|
|
@@ -61,11 +63,11 @@ def __call__(self, item: DataItem) -> DataItem: | |
| DataItem: processed item | ||
| """ | ||
| item = deepcopy(item) | ||
|
|
||
| ents = get_element(item, [self.field, "entities"]) or [] | ||
|
|
||
| # Filter out predictions that are punctuation marks only | ||
| ents = [ent for ent in ents if ent["text"] not in punctuation] | ||
| ents = [self.process(ent) for ent in ents] | ||
| # Filter out predictions with empty alt text and update the rest | ||
| item[self.field]["entities"] = [ | ||
|
Comment on lines
66
to
+69
Contributor
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. issue (bug_risk): Guard against missing
Comment on lines
+68
to
+69
|
||
| out for ent in ents if (out := self.process(ent)) is not None | ||
| ] | ||
|
Comment on lines
+68
to
+71
|
||
|
|
||
| return item | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Align the return type annotation and docstring with the new
Nonereturn path.The method can now return
Nonewhencleaned_textis empty, but the type hint and docstring still indicate it always returns adict. Please update the signature (e.g.-> dict | None) and docstring to match, or avoid returningNoneby returningentand handling the filtering elsewhere.Suggested implementation:
I had to reconstruct some surrounding context because only a fragment of the class was provided:
_clean_entity(...): -> dict, adjust thedef _clean_entity(...)line in the replacement block to match your existing parameters, and only change the return annotation to-> dict | None.__call__(or other transform entrypoint) method inAnonymizationEntityCleaner, remove or merge the__call__implementation I added so you do not duplicate the method. Keep your existing logic, only updating any calls to the helper method to handle the newNonereturn (e.g. by filtering outNonevalues).dict | None. If you are constrained to Python < 3.10, change it to-> Optional[dict]and importOptionalfromtyping.