Skip to content
Merged
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
13 changes: 11 additions & 2 deletions src/store/src/Document/Transformer/TextSplitTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,22 @@
public const OPTION_CHUNK_SIZE = 'chunk_size';
public const OPTION_OVERLAP = 'overlap';

public function __construct(
private int $chunkSize = 1000,
private int $overlap = 200,
) {
if ($this->overlap < 0 || $this->overlap >= $this->chunkSize) {
throw new InvalidArgumentException(\sprintf('Overlap must be non-negative and less than chunk size. Got chunk size: %d, overlap: %d', $this->chunkSize, $this->overlap));
}
}

/**
* @param array{chunk_size?: int, overlap?: int} $options
*/
public function transform(iterable $documents, array $options = []): iterable
{
$chunkSize = $options[self::OPTION_CHUNK_SIZE] ?? 1000;
$overlap = $options[self::OPTION_OVERLAP] ?? 200;
$chunkSize = $options[self::OPTION_CHUNK_SIZE] ?? $this->chunkSize;
$overlap = $options[self::OPTION_OVERLAP] ?? $this->overlap;

if ($overlap < 0 || $overlap >= $chunkSize) {
throw new InvalidArgumentException('Overlap must be non-negative and less than chunk size.');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,63 @@ public function testSplitWithNegativeOverlap()
]));
}

public function testConstructorWithValidParameters()
{
$transformer = new TextSplitTransformer(500, 100);
$document = new TextDocument(Uuid::v4(), 'short text');

$chunks = iterator_to_array($transformer->transform([$document]));

$this->assertCount(1, $chunks);
$this->assertSame('short text', $chunks[0]->content);
}

public function testConstructorWithDefaultParameters()
{
$transformer = new TextSplitTransformer();
$document = new TextDocument(Uuid::v4(), 'short text');

$chunks = iterator_to_array($transformer->transform([$document]));

$this->assertCount(1, $chunks);
$this->assertSame('short text', $chunks[0]->content);
}

public function testConstructorWithNegativeOverlap()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Overlap must be non-negative and less than chunk size. Got chunk size: 1000, overlap: -1');

new TextSplitTransformer(1000, -1);
}

public function testConstructorWithOverlapEqualToChunkSize()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Overlap must be non-negative and less than chunk size. Got chunk size: 500, overlap: 500');

new TextSplitTransformer(500, 500);
}

public function testConstructorWithOverlapGreaterThanChunkSize()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Overlap must be non-negative and less than chunk size. Got chunk size: 100, overlap: 200');

new TextSplitTransformer(100, 200);
}

public function testConstructorParametersAreUsedAsDefaults()
{
$transformer = new TextSplitTransformer(150, 25);
$document = new TextDocument(Uuid::v4(), $this->getLongText());

$chunks = iterator_to_array($transformer->transform([$document]));

$this->assertCount(12, $chunks);
$this->assertSame(150, mb_strlen($chunks[0]->content));
}

private function getLongText(): string
{
return trim(file_get_contents(\dirname(__DIR__, 5).'/fixtures/lorem.txt'));
Expand Down