]> BookStack Code Mirror - bookstack/blob - tests/Search/TextChunkerTest.php
Improved vector text chunking
[bookstack] / tests / Search / TextChunkerTest.php
1 <?php
2
3 namespace Search;
4
5 use BookStack\Search\Vectors\TextChunker;
6 use Tests\TestCase;
7
8 class TextChunkerTest extends TestCase
9 {
10     public function test_it_chunks_text()
11     {
12         $chunker = new TextChunker(3, []);
13         $chunks = $chunker->chunk('123456789');
14
15         $this->assertEquals(['123', '456', '789'], $chunks);
16     }
17
18     public function test_chunk_size_must_be_greater_than_zero()
19     {
20         $this->expectException(\InvalidArgumentException::class);
21         $chunker = new TextChunker(-5, []);
22     }
23
24     public function test_it_works_through_given_delimiters()
25     {
26         $chunker = new TextChunker(5, ['-', '.', '']);
27         $chunks = $chunker->chunk('12-3456.789abcdefg');
28
29         $this->assertEquals(['12', '3456', '789ab', 'cdefg'], $chunks);
30     }
31
32     public function test_it_attempts_to_pack_chunks()
33     {
34         $chunker = new TextChunker(8, [' ', '']);
35         $chunks = $chunker->chunk('123 456 789 abc def');
36
37         $this->assertEquals(['123 456', '789 abc', 'def'], $chunks);
38     }
39
40     public function test_it_attempts_to_pack_using_subchunks()
41     {
42         $chunker = new TextChunker(8, [' ', '-', '']);
43         $chunks = $chunker->chunk('123 456-789abc');
44
45         $this->assertEquals(['123 456', '789abc'], $chunks);
46     }
47 }