-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1cc238e
commit 387330e
Showing
2 changed files
with
28 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -1,6 +1,8 @@ | ||
# SPDX-FileCopyrightText: 2022-present deepset GmbH <[email protected]> | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
import re | ||
|
||
import pytest | ||
|
||
from haystack import Document | ||
|
@@ -165,6 +167,27 @@ def test_split_by_page(self): | |
assert docs[2].meta["split_idx_start"] == text.index(docs[2].content) | ||
assert docs[2].meta["page_number"] == 3 | ||
|
||
def test_split_by_function(self): | ||
splitting_function = lambda input_str: input_str.split(".") | ||
splitter = DocumentSplitter(split_by="function", splitting_function=splitting_function, split_length=1) | ||
text = "This.Is.A.Test" | ||
result = splitter.run(documents=[Document(content=text)]) | ||
docs = result["documents"] | ||
|
||
word_list = ["This", "Is", "A", "Test"] | ||
assert len(docs) == 4 | ||
for w_target, w_split in zip(word_list, docs): | ||
assert w_split.content == w_target | ||
|
||
splitting_function = lambda input_str: re.split("[\s]{2,}", input_str) | ||
splitter = DocumentSplitter(split_by="function", splitting_function=splitting_function, split_length=1) | ||
text = "This Is\n A Test" | ||
result = splitter.run(documents=[Document(content=text)]) | ||
docs = result["documents"] | ||
assert len(docs) == 4 | ||
for w_target, w_split in zip(word_list, docs): | ||
assert w_split.content == w_target | ||
|
||
def test_split_by_word_with_overlap(self): | ||
splitter = DocumentSplitter(split_by="word", split_length=10, split_overlap=2) | ||
text = "This is a text with some words. There is a second sentence. And there is a third sentence." | ||
|