split

package
v0.3.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 23, 2026 License: MIT Imports: 10 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Estimate

func Estimate(s string) int

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.

func Names

func Names() []string

Names lists every splitter New can build, in order.

func New

func New(name string, o Options) (semantic.Splitter, error)

New returns the splitter registered under name.

func Register

func Register(name string, build Build)

Register adds a splitter to the ones New can build, replacing any of the same name. It is how a caller reaches its own splitter from configuration.

Types

type Build

type Build func(Options) (semantic.Splitter, error)

Build makes one splitter from a set of options.

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.

func (Chars) Split

func (c Chars) Split(ctx context.Context, d semantic.Doc) ([]semantic.Chunk, error)

Split cuts d into windows of Size runes.

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.

func (Code) Split

func (c Code) Split(ctx context.Context, d semantic.Doc) ([]semantic.Chunk, error)

Split cuts d at its top-level declarations.

type Embed

type Embed func(ctx context.Context, texts []string) ([][]float32, error)

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.

func (Markdown) Split

func (m Markdown) Split(ctx context.Context, d semantic.Doc) ([]semantic.Chunk, error)

Split cuts d at its headings.

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

type Or []semantic.Splitter

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}}.

func (Or) Split

func (o Or) Split(ctx context.Context, d semantic.Doc) ([]semantic.Chunk, error)

Split returns the chunks of the first splitter that produces any.

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.

func (Paragraphs) Split

func (p Paragraphs) Split(ctx context.Context, d semantic.Doc) ([]semantic.Chunk, error)

Split cuts d on blank lines.

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.

func (Recursive) Split

func (r Recursive) Split(ctx context.Context, d semantic.Doc) ([]semantic.Chunk, error)

Split cuts d at the coarsest separator that fits.

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.

func (Semantic) Split

func (s Semantic) Split(ctx context.Context, d semantic.Doc) ([]semantic.Chunk, error)

Split cuts d where the meaning of its sentences shifts. It returns an error if Embed is missing, fails, or answers with the wrong number of vectors.

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.

func (Sentences) Split

func (s Sentences) Split(ctx context.Context, d semantic.Doc) ([]semantic.Chunk, error)

Split cuts d on sentence boundaries.

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.

func (Tokens) Split

func (t Tokens) Split(ctx context.Context, d semantic.Doc) ([]semantic.Chunk, error)

Split cuts d into runs of whole words worth about Size tokens.

type Words

type Words struct {
	Size    int // words per chunk; zero or less returns the whole text
	Overlap int // words repeated from the end of the previous chunk
}

Words cuts on whitespace, counting words. A word carries the space after it, so the chunks still join back into the document.

func (Words) Split

func (w Words) Split(ctx context.Context, d semantic.Doc) ([]semantic.Chunk, error)

Split cuts d into runs of Size words.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL