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

Ignore case for filter methods #372

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- Added the ignore_case argument for `filter_by_text_equal` and `filter_by_text_contains` methods for the `ElementList` class in `filtering.py`

## [0.10.2] - 2022-11-07

### Changed
Expand Down
36 changes: 29 additions & 7 deletions py_pdf_parser/filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,35 +133,57 @@ def filter_by_tags(self, *tags: str) -> "ElementList":
)
return ElementList(self.document, new_indexes)

def filter_by_text_equal(self, text: str, stripped: bool = True) -> "ElementList":
def filter_by_text_equal(
self, text: str, stripped: bool = True, ignore_case: bool = False
) -> "ElementList":
"""
Filter for elements whose text is exactly the given string.

Args:
text (str): The text to filter for.
stripped (bool, optional): Whether to strip the text of the element before
comparison. Default: True.
ignore_case (bool): Whether to ignore case sensitivity when filtering for matches. Default: False.


Returns:
ElementList: The filtered list.
"""
new_indexes = set(
element._index for element in self if element.text(stripped) == text
)
if ignore_case:
new_indexes = set(
element._index
for element in self
if element.text(stripped).casefold() == text.casefold()
)
else:
new_indexes = set(
element._index for element in self if element.text(stripped) == text
)

return ElementList(self.document, new_indexes)

def filter_by_text_contains(self, text: str) -> "ElementList":
def filter_by_text_contains(
self, text: str, ignore_case: bool = False
) -> "ElementList":
"""
Filter for elements whose text contains the given string.

Args:
text (str): The text to filter for.

ignore_case (bool): Whether to ignore case sensitivity when filtering for matches. Default: False.
Returns:
ElementList: The filtered list.
"""
new_indexes = set(element._index for element in self if text in element.text())
if ignore_case:
new_indexes = set(
element._index
for element in self
if text.casefold() in element.text().casefold()
)
else:
new_indexes = set(
element._index for element in self if text in element.text()
)
return ElementList(self.document, new_indexes)

def filter_by_regex(
Expand Down
Loading