mirror of
https://github.com/hanzoai/semantic.git
synced 2026-10-30 15:04:19 +00:00
The first commit was the shape. This is the implementation: every stage of the pipeline, the graph and its measures, the type system over it, entailment, deduplication and conflict resolution, the decision layer, the stores and the export formats. Twenty-six thousand lines of Go and sixteen thousand of tests, against no dependency but the standard library. Where the Python reaches for torch, spacy or a database driver, the Go states an interface and reports what is missing rather than pretending. A caller can tell "this build cannot serve that" from "nobody has heard of it", which is the difference between a port and a stub. The deterministic paths are complete and are what the tests exercise; the model-shaped ones are one adapter away. Porting a bug is not parity, so several places now disagree with the original on purpose. The JSON repair that closed a truncated reply counted each bracket kind separately and closed them backwards on the one input it exists for; it now works off a stack and ignores brackets inside strings. The entity patterns joined words with \s, so a heading ran into the sentence beneath it and became one name while the real relation was lost; a name is separated by horizontal space now. Eigenvector centrality ran plain power iteration, which on a bipartite graph swings between the two ends of a sign-paired eigenvalue forever and reported every node in a star as equally central; it iterates with A+I, which moves no eigenvector and settles. Similarity summed its weighted parts in map order, and float addition is not associative, so the same comparison scored differently between runs and duplicate detection returned a different answer each time. An inferred ontology named object-property endpoints by raw source type and data-property endpoints by class, so the schema referred to classes it did not contain. A property with no IRI of its own was minted as a class and came back Pascal-cased, renaming the term on the way out of the document. The command built a pipeline with every stage nil and panicked on each invocation. It is now the composition the package documents, and Run reports a missing ingester instead of dereferencing it. Co-authored-by: Hanzo Dev <[email protected]>
84 lines
2.9 KiB
Go
84 lines
2.9 KiB
Go
package split
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"sort"
|
|
"sync"
|
|
|
|
"github.com/hanzoai/semantic"
|
|
)
|
|
|
|
// 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 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
|
|
}
|
|
|
|
// Build makes one splitter from a set of options.
|
|
type Build func(Options) (semantic.Splitter, error)
|
|
|
|
var (
|
|
mu sync.RWMutex
|
|
built = map[string]Build{
|
|
"chars": func(o Options) (semantic.Splitter, error) { return Chars{Size: o.Size, Overlap: o.Overlap}, nil },
|
|
"words": func(o Options) (semantic.Splitter, error) { return Words{Size: o.Size, Overlap: o.Overlap}, nil },
|
|
"tokens": func(o Options) (semantic.Splitter, error) {
|
|
return Tokens{Size: o.Size, Overlap: o.Overlap, Count: o.Count}, nil
|
|
},
|
|
"sentences": func(o Options) (semantic.Splitter, error) {
|
|
return Sentences{Size: o.Size, Max: o.Max, Overlap: o.Overlap}, nil
|
|
},
|
|
"paragraphs": func(o Options) (semantic.Splitter, error) { return Paragraphs{Size: o.Size, Overlap: o.Overlap}, nil },
|
|
"recursive": func(o Options) (semantic.Splitter, error) {
|
|
return Recursive{Size: o.Size, Overlap: o.Overlap, Separators: o.Separators}, nil
|
|
},
|
|
"markdown": func(o Options) (semantic.Splitter, error) { return Markdown{Size: o.Size}, nil },
|
|
"code": func(o Options) (semantic.Splitter, error) { return Code{Size: o.Size}, nil },
|
|
"semantic": func(o Options) (semantic.Splitter, error) {
|
|
if o.Embed == nil {
|
|
return nil, errors.New("split: semantic needs Options.Embed")
|
|
}
|
|
return Semantic{Embed: o.Embed, Threshold: o.Threshold, Size: o.Size}, nil
|
|
},
|
|
}
|
|
)
|
|
|
|
// New returns the splitter registered under name.
|
|
func New(name string, o Options) (semantic.Splitter, error) {
|
|
mu.RLock()
|
|
build, ok := built[name]
|
|
mu.RUnlock()
|
|
if !ok {
|
|
return nil, fmt.Errorf("split: no splitter named %q", name)
|
|
}
|
|
return build(o)
|
|
}
|
|
|
|
// Names lists every splitter New can build, in order.
|
|
func Names() []string {
|
|
mu.RLock()
|
|
defer mu.RUnlock()
|
|
names := make([]string, 0, len(built))
|
|
for name := range built {
|
|
names = append(names, name)
|
|
}
|
|
sort.Strings(names)
|
|
return names
|
|
}
|
|
|
|
// 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.
|
|
func Register(name string, build Build) {
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
built[name] = build
|
|
}
|