Files
zeekayandHanzo Dev 1cd0cd7644 semantic: the pipeline, end to end
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]>
2026-09-08 12:57:59 -07:00

132 lines
3.2 KiB
Go

package store
import (
"math"
"testing"
)
const tol = 1e-9
func eq(t *testing.T, got, want float64) {
t.Helper()
if math.Abs(got-want) > tol {
t.Errorf("got %.17g, want %.17g", got, want)
}
}
func TestDot(t *testing.T) {
for _, c := range []struct {
name string
a, b []float32
want float64
}{
{"orthogonal", []float32{1, 0}, []float32{0, 1}, 0},
{"same", []float32{1, 2, 3}, []float32{4, 5, 6}, 32},
{"opposed", []float32{1, 2}, []float32{-1, -2}, -5},
{"empty", nil, nil, 0},
{"short second is zero padded", []float32{1, 2}, []float32{3}, 3},
{"short first is zero padded", []float32{3}, []float32{1, 2}, 3},
} {
t.Run(c.name, func(t *testing.T) { eq(t, Dot(c.a, c.b), c.want) })
}
}
func TestNorm(t *testing.T) {
for _, c := range []struct {
name string
v []float32
want float64
}{
{"three four five", []float32{3, 4}, 5},
{"unit", []float32{1, 0, 0}, 1},
{"zero", []float32{0, 0}, 0},
{"empty", nil, 0},
{"negative", []float32{-3, -4}, 5},
} {
t.Run(c.name, func(t *testing.T) { eq(t, Norm(c.v), c.want) })
}
}
func TestUnit(t *testing.T) {
got := Unit([]float32{3, 4})
want := []float32{0.6, 0.8}
for i := range want {
eq(t, float64(got[i]), float64(want[i]))
}
// A unit vector held in float32 is unit to float32 precision.
if n := Norm(got); math.Abs(n-1) > 1e-6 {
t.Errorf("length is %v, want 1", n)
}
zero := Unit([]float32{0, 0})
if len(zero) != 2 || zero[0] != 0 || zero[1] != 0 {
t.Errorf("zero vector became %v, want it left alone", zero)
}
v := []float32{3, 4}
Unit(v)
if v[0] != 3 || v[1] != 4 {
t.Errorf("Unit changed its argument: %v", v)
}
}
func TestCos(t *testing.T) {
// 32 / (sqrt(14) * sqrt(77)), computed by hand.
const skew = 0.9746318461970762
for _, c := range []struct {
name string
a, b []float32
want float64
}{
{"identical", []float32{1, 0}, []float32{1, 0}, 1},
{"orthogonal", []float32{1, 0}, []float32{0, 1}, 0},
{"opposed", []float32{1, 0}, []float32{-1, 0}, -1},
{"length ignored", []float32{1, 1}, []float32{5, 5}, 1},
{"skew", []float32{1, 2, 3}, []float32{4, 5, 6}, skew},
{"zero has no direction", []float32{0, 0}, []float32{1, 1}, 0},
} {
t.Run(c.name, func(t *testing.T) { eq(t, Cos(c.a, c.b), c.want) })
}
}
func TestDist(t *testing.T) {
for _, c := range []struct {
name string
a, b []float32
want float64
}{
{"same point", []float32{1, 2}, []float32{1, 2}, 0},
{"three four five", []float32{1, 2}, []float32{4, 6}, 5},
{"one axis", []float32{0}, []float32{2}, 2},
{"short is zero padded", []float32{3}, []float32{0, 4}, 5},
} {
t.Run(c.name, func(t *testing.T) { eq(t, Dist(c.a, c.b), c.want) })
}
}
func TestMetricScore(t *testing.T) {
a, b := []float32{1, 2}, []float32{4, 6}
for _, c := range []struct {
metric Metric
want float64
}{
{Cosine, Cos(a, b)},
{Inner, 16},
{Euclid, -5},
{"", Cos(a, b)},
{"nonsense", Cos(a, b)},
} {
t.Run(string(c.metric), func(t *testing.T) { eq(t, c.metric.Score(a, b), c.want) })
}
}
func TestMetricOrdersNearestFirst(t *testing.T) {
q := []float32{1, 0}
near, far := []float32{1, 0.1}, []float32{0, 1}
for _, m := range []Metric{Cosine, Inner, Euclid} {
if m.Score(q, near) <= m.Score(q, far) {
t.Errorf("%s scored the far vector at least as near", m)
}
}
}