Documentation
¶
Overview ¶
Package split cuts a document into chunks.
Every splitter here works the same way. It finds the boundaries it is allowed to cut on — runes, words, sentences, paragraphs, headings, code blocks — and then merges neighbouring pieces until one more would pass Size. The pieces tile the text, so with Overlap zero the chunks of a document concatenate back into the document exactly, byte for byte, and a chunk boundary never falls inside a rune.
Size is measured in the splitter's own unit: runes for Chars, words for Words, tokens for Tokens, runes for the rest. Size and Max both bound a chunk; with neither set nothing is merged, so Sentences gives a chunk per sentence and Paragraphs a chunk per paragraph. Chars, Words and Tokens have no unit boundary to fall back on, so with Size unset they return the whole text as one chunk.
Overlap repeats that many trailing units at the head of the next chunk. A chunk always advances by at least one unit, so an Overlap at or above Size costs repetition, not progress.
Index ¶
- func Estimate(s string) int
- func Names() []string
- func New(name string, o Options) (semantic.Splitter, error)
- func Register(name string, build Build)
- type Build
- type Chars
- type Code
- type Embed
- type Markdown
- type Options
- type Or
- type Paragraphs
- type Recursive
- type Semantic
- type Sentences
- type Tokens
- type Words
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Estimate ¶
Estimate guesses a token count at four characters per token, the ratio byte-pair tokenizers average over English prose. It is a stand-in for a real tokenizer, not a substitute: on code, on other scripts, and on any single short word it is wrong, and a caller who needs the real number should pass its tokenizer as Tokens.Count.
Types ¶
type Chars ¶
type Chars struct {
Size int // runes per chunk; zero or less returns the whole text
Overlap int // runes repeated from the end of the previous chunk
}
Chars cuts fixed windows of runes. It is the splitter with no opinion about the text: it will cut mid-word and mid-sentence, but never mid-rune, and it is the only one whose chunk count is known before reading the document.
type Code ¶
type Code struct {
Size int // runes per chunk; zero or less gives one chunk per declaration
}
Code cuts source at the top of each declaration and keeps its body with it. A declaration longer than Size stays whole: half a function is not a fact about the program.
type Embed ¶
Embed turns texts into vectors. Semantic calls it once per document with every sentence in it, so an implementation is free to batch, and the context is the one the split was started with, so a slow model is cancellable.
type Markdown ¶
type Markdown struct {
Size int // runes per chunk; zero or less gives one chunk per section
}
Markdown cuts before every heading, so a section arrives at extraction with the heading that says what it is about. Sections shorter than Size are merged; a section longer than Size is left whole rather than cut somewhere the document did not offer a break.
type Options ¶
type Options struct {
Size int // chunk size, in the splitter's own unit
Overlap int // units repeated from the end of the previous chunk
Max int // units per chunk, where a splitter counts them
Separators []string // separator hierarchy for recursive
Threshold float64 // similarity below which semantic cuts
Embed Embed // vectors for semantic
Count func(string) int // token count for tokens
}
Options are the settings a splitter can be built from by name, for callers whose choice of splitter arrives as configuration rather than as code. Each splitter reads the fields that apply to it and ignores the rest.
type Or ¶
Or tries each splitter in turn and returns the first result that is neither an error nor empty. It is how a splitter that needs a model falls back to one that needs nothing: Or{Semantic{Embed: e}, Sentences{Size: 1000}}.
type Paragraphs ¶
type Paragraphs struct {
Size int // runes per chunk; zero or less is unbounded
Overlap int // paragraphs repeated from the end of the previous chunk
}
Paragraphs fills chunks with whole paragraphs, a paragraph being what lies between two blank lines. With Size unset each paragraph becomes a chunk.
type Recursive ¶
type Recursive struct {
Size int // runes per chunk; zero or less returns the whole text
Overlap int // pieces repeated from the end of the previous chunk
Separators []string // tried in order; nil means blank line, line, sentence, word, rune
}
Recursive cuts on the first separator that gets a piece under Size and works down the list for pieces that are still too big, so a document breaks at paragraphs where it can, at lines where it cannot, and at sentences, words and finally runes as the text resists. The resulting pieces are then merged back up to Size, which is what keeps a run of short paragraphs from becoming a run of tiny chunks.
type Semantic ¶
type Semantic struct {
Embed Embed // required
Threshold float64 // cut when adjacent similarity falls below this
Size int // runes per chunk; zero or less lets similarity alone decide
}
Semantic keeps consecutive sentences together while they mean similar things and cuts where they stop. What counts as similar is the cosine of the angle between adjacent sentence vectors; Threshold is where the cut falls. The vectors come from Embed, which this package never supplies: no splitter here reaches the network on its own.
type Sentences ¶
type Sentences struct {
Size int // runes per chunk; zero or less is unbounded
Max int // sentences per chunk; zero or less is unbounded
Overlap int // sentences repeated from the end of the previous chunk
}
Sentences fills chunks with whole sentences. With neither Size nor Max set each sentence becomes a chunk.
type Tokens ¶
type Tokens struct {
Size int // tokens per chunk; zero or less returns the whole text
Overlap int // words repeated from the end of the previous chunk
Count func(string) int // tokens in a string; nil uses Estimate
}
Tokens fills chunks to a token budget. Counting tokens needs the tokenizer of whatever model the chunks are headed for, which is not something this package can know, so the count is a parameter; the default is an estimate. Words stay whole either way, so a chunk holds a few tokens fewer than Size rather than a fraction of a word.