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

feat: added split by page to DocumentSplitter #6753

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 11 additions & 6 deletions haystack/components/preprocessors/document_splitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,20 @@ class DocumentSplitter:
"""

def __init__(
self, split_by: Literal["word", "sentence", "passage"] = "word", split_length: int = 200, split_overlap: int = 0
self,
split_by: Literal["word", "sentence", "page", "passage"] = "word",
split_length: int = 200,
split_overlap: int = 0,
):
"""
:param split_by: The unit by which the document should be split. Choose from "word" for splitting by " ",
"sentence" for splitting by ".", or "passage" for splitting by "\\n\\n".
"sentence" for splitting by ".", "page" for splittling by "\f" or "passage" for splitting by "\\n\\n".
sjrl marked this conversation as resolved.
Show resolved Hide resolved
:param split_length: The maximum number of units in each split.
:param split_overlap: The number of units that each split should overlap.
"""

self.split_by = split_by
if split_by not in ["word", "sentence", "passage"]:
if split_by not in ["word", "sentence", "page", "passage"]:
raise ValueError("split_by must be one of 'word', 'sentence' or 'passage'.")
sjrl marked this conversation as resolved.
Show resolved Hide resolved
if split_length <= 0:
raise ValueError("split_length must be greater than 0.")
Expand Down Expand Up @@ -60,16 +63,18 @@ def run(self, documents: List[Document]):
split_docs += [Document(content=txt, meta=metadata) for txt in text_splits]
return {"documents": split_docs}

def _split_into_units(self, text: str, split_by: Literal["word", "sentence", "passage"]) -> List[str]:
if split_by == "passage":
def _split_into_units(self, text: str, split_by: Literal["word", "sentence", "passage", "page"]) -> List[str]:
if split_by == "page":
split_at = "\f"
elif split_by == "passage":
split_at = "\n\n"
elif split_by == "sentence":
split_at = "."
elif split_by == "word":
split_at = " "
else:
raise NotImplementedError(
"DocumentSplitter only supports 'passage', 'sentence' or 'word' split_by options."
"DocumentSplitter only supports 'page', 'passage', 'sentence' or 'word' split_by options."
)
units = text.split(split_at)
# Add the delimiter back to all units except the last one
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
enhancements:
- |
Added split by page to DocumentSplitter, which will split the document at \f