Skip to content
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
3 changes: 3 additions & 0 deletions src/semchunk/semchunk.py
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,9 @@ def __call__(
chunks_and_offsets = list(chunked_texts)

if offsets:
if not chunks_and_offsets:
return [], []

chunks, offsets_ = zip(*chunks_and_offsets)

return list(chunks), list(offsets_)
Expand Down
33 changes: 33 additions & 0 deletions tests/test_empty_batch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""Check empty batch results and the neighboring input shapes."""

import pytest

import semchunk


@pytest.mark.parametrize("texts", [[], ()], ids=["list", "tuple"])
@pytest.mark.parametrize("offsets", [False, True])
@pytest.mark.parametrize("progress", [False, True])
@pytest.mark.parametrize("processes", [1, 2])
def test_empty_batch(texts, offsets, progress, processes):
chunker = semchunk.chunkerify(len, 4, memoize=False)

assert chunker(texts, offsets=offsets, progress=progress, processes=processes) == (([], []) if offsets else [])


@pytest.mark.parametrize("offsets", [False, True])
@pytest.mark.parametrize("progress", [False, True])
@pytest.mark.parametrize("processes", [1, 2])
def test_nonempty_batch(offsets, progress, processes):
chunker = semchunk.chunkerify(len, 4, memoize=False)
chunks = [["abcd", "ef"], []]
expected = (chunks, [[(0, 4), (5, 7)], []]) if offsets else chunks

assert chunker(("abcd ef", ""), offsets=offsets, progress=progress, processes=processes) == expected


@pytest.mark.parametrize("offsets", [False, True])
def test_empty_string(offsets):
chunker = semchunk.chunkerify(len, 4, memoize=False)

assert chunker("", offsets=offsets) == (([], []) if offsets else [])