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]>
123 lines
3.7 KiB
Go
123 lines
3.7 KiB
Go
package split
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"unicode/utf8"
|
|
|
|
"github.com/hanzoai/semantic"
|
|
)
|
|
|
|
// Sentences fills chunks with whole sentences. With neither Size nor Max set
|
|
// each sentence becomes a chunk.
|
|
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
|
|
}
|
|
|
|
// Split cuts d on sentence boundaries.
|
|
func (s Sentences) Split(ctx context.Context, d semantic.Doc) ([]semantic.Chunk, error) {
|
|
if err := ctx.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
if d.Text == "" {
|
|
return nil, nil
|
|
}
|
|
p := pack{size: s.Size, max: s.Max, overlap: s.Overlap}
|
|
return chunks(d, p.spans(d.Text, sentences(d.Text))), nil
|
|
}
|
|
|
|
// Paragraphs fills chunks with whole paragraphs, a paragraph being what lies
|
|
// between two blank lines. With Size unset each paragraph becomes a chunk.
|
|
type Paragraphs struct {
|
|
Size int // runes per chunk; zero or less is unbounded
|
|
Overlap int // paragraphs repeated from the end of the previous chunk
|
|
}
|
|
|
|
// Split cuts d on blank lines.
|
|
func (p Paragraphs) Split(ctx context.Context, d semantic.Doc) ([]semantic.Chunk, error) {
|
|
if err := ctx.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
if d.Text == "" {
|
|
return nil, nil
|
|
}
|
|
return chunks(d, pack{size: p.Size, overlap: p.Overlap}.spans(d.Text, paragraphs(d.Text))), nil
|
|
}
|
|
|
|
// 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 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
|
|
}
|
|
|
|
// Split cuts d at the coarsest separator that fits.
|
|
func (r Recursive) Split(ctx context.Context, d semantic.Doc) ([]semantic.Chunk, error) {
|
|
if err := ctx.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
if d.Text == "" {
|
|
return nil, nil
|
|
}
|
|
if r.Size <= 0 {
|
|
return chunks(d, []span{{0, len(d.Text)}}), nil
|
|
}
|
|
seps := r.Separators
|
|
if seps == nil {
|
|
seps = separators()
|
|
}
|
|
pieces := divide(d.Text, span{0, len(d.Text)}, seps, r.Size)
|
|
return chunks(d, pack{size: r.Size, overlap: r.Overlap}.spans(d.Text, pieces)), nil
|
|
}
|
|
|
|
// separators returns the default hierarchy, freshly, so that a caller who
|
|
// keeps the slice and edits it cannot change what the next call does.
|
|
func separators() []string { return []string{"\n\n", "\n", ". ", " ", ""} }
|
|
|
|
// divide cuts s down until every piece fits in size or the separators run out.
|
|
// The separator stays on the piece it ends, so the pieces still tile s.
|
|
func divide(text string, s span, seps []string, size int) []span {
|
|
if utf8.RuneCountInString(text[s.start:s.end]) <= size || len(seps) == 0 {
|
|
return []span{s}
|
|
}
|
|
sep, rest := seps[0], seps[1:]
|
|
if sep == "" {
|
|
var out []span
|
|
for i := s.start; i < s.end; {
|
|
j := advance(text, i, size)
|
|
if j > s.end {
|
|
j = s.end
|
|
}
|
|
out = append(out, span{i, j})
|
|
i = j
|
|
}
|
|
return out
|
|
}
|
|
var pieces []span
|
|
for i := s.start; i < s.end; {
|
|
k := strings.Index(text[i:s.end], sep)
|
|
if k < 0 {
|
|
pieces = append(pieces, span{i, s.end})
|
|
break
|
|
}
|
|
end := i + k + len(sep)
|
|
pieces = append(pieces, span{i, end})
|
|
i = end
|
|
}
|
|
if len(pieces) <= 1 {
|
|
return divide(text, s, rest, size)
|
|
}
|
|
var out []span
|
|
for _, p := range pieces {
|
|
out = append(out, divide(text, p, rest, size)...)
|
|
}
|
|
return out
|
|
}
|