Class: Pikuri::VectorDb::Chunker::FixedWindow
- Inherits:
-
Object
- Object
- Pikuri::VectorDb::Chunker::FixedWindow
- Defined in:
- lib/pikuri/vector_db/chunker/fixed_window.rb
Overview
Sliding-window chunker: splits text on whitespace into words, then walks
forward emitting ~+size+-token chunks, each after the first re-including
overlap tokens of the previous chunk's tail so an answer straddling a
boundary stays intact in at least one chunk (standard RAG practice).
Whitespace-split gives word boundaries for Western-European languages — good enough for any text FileType.read_as_text flattens to prose; CJK (no whitespace) degrades to one-huge-unit chunks, a documented limitation.
Per-chunk tokenizer cost: the greedy "add a word, re-count" algorithm
calls tokenizer.count O(n_words × chunks) times — negligible for
Tokenizer::CharHeuristic, but one HTTP round-trip each for
Tokenizer::LlamaServer, so indexing a large corpus takes minutes. A
one-time boot/reindex cost, accepted for v1.
Forward-progress guard: the constructor rejects overlap >= size, and
the inner loop always advances at least one word — termination holds even
if a pathological tokenizer reports misleading counts.
Instance Attribute Summary collapse
-
#overlap ⇒ Integer
readonly
Tokens of overlap between adjacent chunks.
-
#size ⇒ Integer
readonly
Target token count per chunk.
Instance Method Summary collapse
-
#chunk(text) ⇒ Array<String>
Chunk
textinto approximately +size+-token windows with +overlap+-token tail repeats. - #initialize(size:, overlap: 0, tokenizer: Tokenizer::CharHeuristic.new) ⇒ FixedWindow constructor
Constructor Details
#initialize(size:, overlap: 0, tokenizer: Tokenizer::CharHeuristic.new) ⇒ FixedWindow
46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/pikuri/vector_db/chunker/fixed_window.rb', line 46 def initialize(size:, overlap: 0, tokenizer: Tokenizer::CharHeuristic.new) raise ArgumentError, "size must be positive (got #{size})" if size <= 0 raise ArgumentError, "overlap must be >= 0 (got #{overlap})" if overlap.negative? if overlap >= size raise ArgumentError, "overlap (#{overlap}) must be strictly less than size (#{size}) " \ "— the sliding window would not advance" end @size = size @overlap = overlap @tokenizer = tokenizer end |
Instance Attribute Details
#overlap ⇒ Integer (readonly)
Returns tokens of overlap between adjacent chunks.
31 32 33 |
# File 'lib/pikuri/vector_db/chunker/fixed_window.rb', line 31 def overlap @overlap end |
#size ⇒ Integer (readonly)
Returns target token count per chunk.
27 28 29 |
# File 'lib/pikuri/vector_db/chunker/fixed_window.rb', line 27 def size @size end |
Instance Method Details
#chunk(text) ⇒ Array<String>
Chunk text into approximately +size+-token windows
with +overlap+-token tail repeats. Empty /
whitespace-only input returns [].
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/pikuri/vector_db/chunker/fixed_window.rb', line 67 def chunk(text) words = text.split return [] if words.empty? chunks = [] start = 0 while start < words.length finish = find_chunk_end(words, start) chunks << words[start...finish].join(' ') break if finish >= words.length start = find_next_start(words, start, finish) end chunks end |