-
Notifications
You must be signed in to change notification settings - Fork 3k
feat: add strip_whitespaces and replace_regexes to DocumentCleaner #10400
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
Merged
julian-risch
merged 7 commits into
deepset-ai:main
from
VedantMadane:feat/document-cleaner-expansion
Feb 2, 2026
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cef0ab2
feat: add strip_whitespace and regex_replace to DocumentCleaner
VedantMadane ec65d07
Merge branch 'main' into feat/document-cleaner-expansion
VedantMadane 8359403
docs: Add release notes for DocumentCleaner enhancements
VedantMadane 8d0d5d8
Merge branch 'main' into feat/document-cleaner-expansion
VedantMadane 12b2dd7
Merge branch 'main' into feat/document-cleaner-expansion
VedantMadane 76b4bef
refactor: rename parameters and handle page breaks in replace_regexes
VedantMadane c01bb06
fmt
julian-risch 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -230,3 +230,95 @@ def test_other_document_fields_are_not_lost(self): | |
| assert res["documents"][0].score == document.score | ||
| assert res["documents"][0].embedding == document.embedding | ||
| assert res["documents"][0].sparse_embedding == document.sparse_embedding | ||
|
|
||
| def test_strip_whitespace(self): | ||
| """Test that strip_whitespace removes only leading and trailing whitespace.""" | ||
| cleaner = DocumentCleaner( | ||
| remove_empty_lines=False, remove_extra_whitespaces=False, strip_whitespace=True | ||
| ) | ||
| result = cleaner.run( | ||
| documents=[Document(content=" \n\nHello World\n\n Some text here \n\n ")] | ||
| ) | ||
| assert len(result["documents"]) == 1 | ||
| # strip_whitespace should only remove leading/trailing whitespace, preserving internal whitespace | ||
| assert result["documents"][0].content == "Hello World\n\n Some text here" | ||
|
|
||
| def test_strip_whitespace_preserves_internal_formatting(self): | ||
| """Test that strip_whitespace preserves internal whitespace like markdown formatting.""" | ||
| cleaner = DocumentCleaner( | ||
| remove_empty_lines=False, remove_extra_whitespaces=False, strip_whitespace=True | ||
| ) | ||
| markdown_content = """ | ||
|
|
||
| # Header | ||
|
|
||
| This is a paragraph. | ||
|
|
||
| - Item 1 | ||
| - Item 2 | ||
|
|
||
| """ | ||
| result = cleaner.run(documents=[Document(content=markdown_content)]) | ||
| assert len(result["documents"]) == 1 | ||
| expected = """# Header | ||
|
|
||
| This is a paragraph. | ||
|
|
||
| - Item 1 | ||
| - Item 2""" | ||
| assert result["documents"][0].content == expected | ||
|
|
||
| def test_regex_replace_single_pattern(self): | ||
| """Test regex_replace with a single pattern.""" | ||
| cleaner = DocumentCleaner( | ||
| remove_empty_lines=False, | ||
| remove_extra_whitespaces=False, | ||
| regex_replace={r"\n\n+": "\n"}, | ||
| ) | ||
| result = cleaner.run(documents=[Document(content="Line 1\n\n\n\nLine 2\n\nLine 3")]) | ||
| assert len(result["documents"]) == 1 | ||
| assert result["documents"][0].content == "Line 1\nLine 2\nLine 3" | ||
|
|
||
| def test_regex_replace_multiple_patterns(self): | ||
| """Test regex_replace with multiple patterns.""" | ||
| cleaner = DocumentCleaner( | ||
| remove_empty_lines=False, | ||
| remove_extra_whitespaces=False, | ||
| regex_replace={r"\n\n+": "\n", r"\s{2,}": " "}, | ||
| ) | ||
| result = cleaner.run(documents=[Document(content="Hello World\n\n\nGoodbye")]) | ||
| assert len(result["documents"]) == 1 | ||
| assert result["documents"][0].content == "Hello World\nGoodbye" | ||
|
|
||
| def test_regex_replace_custom_replacement(self): | ||
| """Test regex_replace with custom replacement strings.""" | ||
| cleaner = DocumentCleaner( | ||
| remove_empty_lines=False, | ||
| remove_extra_whitespaces=False, | ||
| regex_replace={r"\[REDACTED\]": "***", r"(\d{4})-(\d{2})-(\d{2})": r"\2/\3/\1"}, | ||
|
Member
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. Just a note for our team: This is quite powerful and I think we could include such an example in our documentation. That would make it easier for users to understand what the parameter can be used for. |
||
| ) | ||
| result = cleaner.run( | ||
| documents=[Document(content="Name: [REDACTED], Date: 2024-01-15")] | ||
| ) | ||
| assert len(result["documents"]) == 1 | ||
| assert result["documents"][0].content == "Name: ***, Date: 01/15/2024" | ||
|
|
||
| def test_strip_whitespace_and_regex_replace_combined(self): | ||
| """Test using both strip_whitespace and regex_replace together.""" | ||
| cleaner = DocumentCleaner( | ||
| remove_empty_lines=False, | ||
| remove_extra_whitespaces=False, | ||
| strip_whitespace=True, | ||
| regex_replace={r"\n\n+": "\n"}, | ||
| ) | ||
| result = cleaner.run( | ||
| documents=[Document(content="\n\n Hello\n\n\nWorld \n\n")] | ||
| ) | ||
| assert len(result["documents"]) == 1 | ||
| assert result["documents"][0].content == "Hello\nWorld" | ||
|
|
||
| def test_init_with_new_params(self): | ||
| """Test that new parameters are properly initialized.""" | ||
| cleaner = DocumentCleaner(strip_whitespace=True, regex_replace={r"\n+": "\n"}) | ||
| assert cleaner.strip_whitespace is True | ||
| assert cleaner.regex_replace == {r"\n+": "\n"} | ||
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.