Skip to content

Commit 2f7f57e

Browse files
patelchaitanyntkathole
authored andcommitted
fix: Stop TextChunker once a chunk reaches the end of the text
The sliding window kept advancing after the last word was covered, so a leftover window with at least min_chunk_size words was emitted as an extra chunk whose words were all in the previous chunk. Break out of the loop right after the chunk that reaches the end of the text. Fixes #7063 Signed-off-by: Chaitany Patel <patelchaitany93@gmail.com>
1 parent f887ffd commit 2f7f57e

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

‎sdk/python/feast/chunker.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,7 @@ def _chunk_by_words(
147147
)
148148
chunk_index += 1
149149

150+
if i + self.config.chunk_size >= len(words):
151+
break
152+
150153
return chunks

‎sdk/python/tests/unit/test_doc_embedder.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,21 @@ def test_overlap(self):
7373

7474
assert words_0[-3:] == words_1[:3]
7575

76+
def test_no_trailing_chunk_contained_in_previous(self):
77+
"""Once a chunk reaches the end of the text, no further chunk is emitted."""
78+
config = ChunkingConfig(
79+
chunk_size=100, chunk_overlap=20, min_chunk_size=20, max_chunk_chars=None
80+
)
81+
chunker = TextChunker(config=config)
82+
text = " ".join([f"w{i}" for i in range(180)])
83+
84+
chunks = chunker.load_parse_and_chunk(
85+
source=text, source_id="doc1", source_column="text"
86+
)
87+
88+
assert [c["text"].split()[-1] for c in chunks] == ["w99", "w179"]
89+
assert [len(c["text"].split()) for c in chunks] == [100, 100]
90+
7691
def test_min_chunk_size_filters_small_trailing(self):
7792
"""Trailing chunk smaller than min_chunk_size is dropped."""
7893
config = ChunkingConfig(

0 commit comments

Comments
 (0)