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.8 KiB
Go
123 lines
3.8 KiB
Go
package export
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/hanzoai/semantic"
|
|
"github.com/hanzoai/semantic/store"
|
|
)
|
|
|
|
// Source walks a graph, handing every assertion to yield in order. It returns
|
|
// yield's error unchanged, so a format that cannot write stops the walk
|
|
// instead of being handed the rest of a graph it has no use for.
|
|
//
|
|
// A source may be walked more than once: [Triples], [Match] and [Walk] all
|
|
// replay, which is what [GEXF] needs, since its grammar puts every node before
|
|
// every edge. A source over an io.Reader is the exception and says so.
|
|
type Source func(ctx context.Context, yield func(semantic.Triple) error) error
|
|
|
|
// Triples is the source of a slice already in memory.
|
|
func Triples(ts []semantic.Triple) Source {
|
|
return func(ctx context.Context, yield func(semantic.Triple) error) error {
|
|
for _, t := range ts {
|
|
if err := ctx.Err(); err != nil {
|
|
return err
|
|
}
|
|
if err := yield(t); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// Match is the source of what a triple store holds for a pattern. An empty
|
|
// field matches anything, so Match(st, "", "", "") is the whole store.
|
|
//
|
|
// A store answers with the assertion and not with what was known about it, so
|
|
// the triples this yields carry no confidence and no source span.
|
|
func Match(st store.Triple, s, p, o string) Source {
|
|
return func(ctx context.Context, yield func(semantic.Triple) error) error {
|
|
got, err := st.Match(ctx, s, p, o)
|
|
if err != nil {
|
|
return fmt.Errorf("match %q %q %q: %w", s, p, o, err)
|
|
}
|
|
for _, t := range got {
|
|
if err := yield(semantic.Triple{Subject: t[0], Predicate: t[1], Object: t[2]}); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// Walk is the source of the edges reachable from roots in a graph store,
|
|
// breadth first from each root in turn, every edge yielded once.
|
|
//
|
|
// A graph store reports a node's neighbours without saying which relation
|
|
// leads to them, so the walk cannot recover the predicate and records label in
|
|
// its place. Where the predicates matter, export from the triple store.
|
|
func Walk(g store.Graph, label string, roots ...string) Source {
|
|
return func(ctx context.Context, yield func(semantic.Triple) error) error {
|
|
queued := map[string]bool{}
|
|
var queue []string
|
|
for _, r := range roots {
|
|
if r != "" && !queued[r] {
|
|
queued[r] = true
|
|
queue = append(queue, r)
|
|
}
|
|
}
|
|
for len(queue) > 0 {
|
|
if err := ctx.Err(); err != nil {
|
|
return err
|
|
}
|
|
from := queue[0]
|
|
queue = queue[1:]
|
|
out, err := g.Out(ctx, from)
|
|
if err != nil {
|
|
return fmt.Errorf("out %q: %w", from, err)
|
|
}
|
|
for _, to := range out {
|
|
if err := yield(semantic.Triple{Subject: from, Predicate: label, Object: to}); err != nil {
|
|
return err
|
|
}
|
|
if !queued[to] {
|
|
queued[to] = true
|
|
queue = append(queue, to)
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// fail is the source that yields nothing and reports err, so a read that
|
|
// cannot start reports it where the walk is rather than where the call was.
|
|
func fail(err error) Source {
|
|
return func(context.Context, func(semantic.Triple) error) error { return err }
|
|
}
|
|
|
|
// once guards a source over a reader. The bytes are gone after the first
|
|
// walk, so a second says so instead of answering with an empty graph.
|
|
func once(s Source) Source {
|
|
spent := false
|
|
return func(ctx context.Context, yield func(semantic.Triple) error) error {
|
|
if spent {
|
|
return ErrDrained
|
|
}
|
|
spent = true
|
|
return s(ctx, yield)
|
|
}
|
|
}
|
|
|
|
// whole reports whether an assertion names all three of its terms. The
|
|
// formats that identify nodes call it, because a node with no name is a
|
|
// document a reader cannot resolve.
|
|
func whole(t semantic.Triple, n int) error {
|
|
if t.Subject == "" || t.Predicate == "" || t.Object == "" {
|
|
return fmt.Errorf("assertion %d (%q %q %q): %w", n, t.Subject, t.Predicate, t.Object, ErrTerm)
|
|
}
|
|
return nil
|
|
}
|