Strategies
Sliding window
A window of size characters that moves forward step characters at a time.
sliding-window moves a window of size characters through the text, step characters at a time. Each position of the window is a chunk.
const { chunks } = await chunkDocument( { path: "notes.txt" }, { strategy: "sliding-window", size: 1000, step: 500 },);When to use it
Section titled “When to use it”Use sliding-window for text without structure when you want every part of the text to appear in more than one chunk, so that a passage is complete in at least one of them wherever it falls. With step at half of size, the default, most characters are in two chunks.
For documents with structure, prefer structure. For fewer, less overlapping chunks, use fixed-overlap with a small overlap.
How it works
Section titled “How it works”sliding-window is fixed-overlap with an overlap of size - step:
- The first chunk is the first
sizecharacters, moved back to a word boundary withboundary: "word". - Each following chunk starts
size - stepcharacters before the end of the previous one, which is aboutstepcharacters after the previous start.
Example
Section titled “Example”# Returns policy
You can return most items within 30 days of delivery. Items must be unused and in their original packaging.
## Start a return
1. Sign in and open **Orders**.2. Choose the item you want to return.3. Print the prepaid shipping label.
## Refunds
We refund the original payment method within 5 business days of receiving the item. Shipping costs are not refunded.
| Item | Refund || --- | --- || Unused item | Full price || Opened item | 80% |import { chunkDocument } from "docchunk";
const { chunks } = await chunkDocument( { path: "returns-policy.md" }, { strategy: "sliding-window", size: 200, step: 100 },);
for (const chunk of chunks) { console.log(chunk.text); console.log("---");}# Returns policy
You can return most items within 30 days of delivery. Items must be unused and in their original packaging.
## Start a return
1. Sign in and open **Orders**.2. Choose the item you---their original packaging.
## Start a return
1. Sign in and open **Orders**.2. Choose the item you want to return.3. Print the prepaid shipping label.
## Refunds
We refund the original payment---ou want to return.3. Print the prepaid shipping label.
## Refunds
We refund the original payment method within 5 business days of receiving the item. Shipping costs are not refunded.
| Item |---ent method within 5 business days of receiving the item. Shipping costs are not refunded.
| Item | Refund || --- | --- || Unused item | Full price || Opened item | 80% |---Options
Section titled “Options”| Option | Type | Default | Description |
|---|---|---|---|
size |
number |
1500 |
Characters in each window. A positive integer. |
step |
number |
750 |
Characters the window moves forward each time. A positive integer, at most size. |
boundary |
"word" or "char" |
"word" |
"word" moves a cut back to a space near the end of the chunk. "char" cuts at exactly size. |
Good to know
Section titled “Good to know”- The step isn’t exact with word boundaries. When a window’s end moves back to a space, the next window starts earlier by the same amount.
- Chunks can start in the middle of a word, as in
fixed-overlap. - With
stepequal tosize, windows don’t overlap, and the result is the same asfixed.
Related strategies
Section titled “Related strategies”fixed-overlap: the same strategy, set by the overlap instead of the step.fixed: windows with no overlap.