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]>
119 lines
3.5 KiB
Go
119 lines
3.5 KiB
Go
package store
|
|
|
|
import "testing"
|
|
|
|
// ids reads the order of a ranked list, which is the part of a fusion that
|
|
// has to be right.
|
|
func ids(ms []Match) []string {
|
|
out := make([]string, len(ms))
|
|
for i, m := range ms {
|
|
out[i] = m.ID
|
|
}
|
|
return out
|
|
}
|
|
|
|
func equal[T comparable](t *testing.T, got, want []T) {
|
|
t.Helper()
|
|
if len(got) != len(want) {
|
|
t.Fatalf("got %v, want %v", got, want)
|
|
}
|
|
for i := range want {
|
|
if got[i] != want[i] {
|
|
t.Fatalf("got %v, want %v", got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestFuse(t *testing.T) {
|
|
a := []Match{{ID: "a"}, {ID: "b"}, {ID: "c"}}
|
|
b := []Match{{ID: "c"}, {ID: "a"}}
|
|
|
|
got := Fuse(60, a, b)
|
|
equal(t, ids(got), []string{"a", "c", "b"})
|
|
|
|
// 1/61 + 1/62, 1/63 + 1/61, 1/62 — worked out by hand.
|
|
eq(t, got[0].Score, 1.0/61+1.0/62)
|
|
eq(t, got[1].Score, 1.0/63+1.0/61)
|
|
eq(t, got[2].Score, 1.0/62)
|
|
}
|
|
|
|
func TestFuseIgnoresScores(t *testing.T) {
|
|
// Fusion by rank is the point: a source that scores on a wild scale
|
|
// cannot outvote one that scores small, only out-rank it.
|
|
a := []Match{{ID: "a", Score: 1000}, {ID: "b", Score: 999}}
|
|
b := []Match{{ID: "b", Score: 0.001}, {ID: "a", Score: 0.0009}}
|
|
if got := ids(Fuse(60, a, b)); got[0] != "a" {
|
|
t.Errorf("got %v, want a first: it is first in one list and second in the other", got)
|
|
}
|
|
one := Fuse(60, a)
|
|
eq(t, one[0].Score, 1.0/61)
|
|
}
|
|
|
|
func TestFuseConstant(t *testing.T) {
|
|
a := []Match{{ID: "a"}, {ID: "b"}}
|
|
if s := Fuse(0, a)[0].Score; s != 1.0/61 {
|
|
t.Errorf("k of zero gave %v, want the default 60 to give %v", s, 1.0/61)
|
|
}
|
|
if s := Fuse(1, a)[0].Score; s != 1.0/2 {
|
|
t.Errorf("k of one gave %v, want %v", s, 0.5)
|
|
}
|
|
}
|
|
|
|
func TestFuseKeepsMetadata(t *testing.T) {
|
|
a := []Match{{ID: "a", Meta: map[string]any{"lang": "en"}}}
|
|
b := []Match{{ID: "a", Meta: map[string]any{"lang": "fr"}}}
|
|
got := Fuse(60, a, b)
|
|
if len(got) != 1 {
|
|
t.Fatalf("got %d results, want one: the same id is one result", len(got))
|
|
}
|
|
if got[0].Meta["lang"] != "en" {
|
|
t.Errorf("metadata came from %v, want the first list", got[0].Meta)
|
|
}
|
|
}
|
|
|
|
func TestBlend(t *testing.T) {
|
|
a := []Match{{ID: "a", Score: 1.0}, {ID: "b", Score: 0.5}}
|
|
b := []Match{{ID: "b", Score: 0.8}, {ID: "c", Score: 0.2}}
|
|
|
|
got := Blend([]float64{0.7, 0.3}, a, b)
|
|
equal(t, ids(got), []string{"a", "b", "c"})
|
|
eq(t, got[0].Score, 0.7) // 1.0 * 0.7
|
|
eq(t, got[1].Score, 0.5*0.7+0.8*0.3) // 0.59
|
|
eq(t, got[2].Score, 0.2*0.3) // 0.06
|
|
}
|
|
|
|
func TestBlendWithoutWeights(t *testing.T) {
|
|
a := []Match{{ID: "a", Score: 1.0}, {ID: "b", Score: 0.5}}
|
|
b := []Match{{ID: "b", Score: 0.8}, {ID: "c", Score: 0.2}}
|
|
|
|
got := Blend(nil, a, b)
|
|
equal(t, ids(got), []string{"b", "a", "c"})
|
|
eq(t, got[0].Score, 0.5*0.5+0.8*0.5) // 0.65
|
|
eq(t, got[1].Score, 0.5)
|
|
eq(t, got[2].Score, 0.1)
|
|
}
|
|
|
|
func TestMergeEmpty(t *testing.T) {
|
|
if got := Fuse(60); len(got) != 0 {
|
|
t.Errorf("fusing nothing gave %v, want nothing", got)
|
|
}
|
|
if got := Blend(nil); len(got) != 0 {
|
|
t.Errorf("blending nothing gave %v, want nothing", got)
|
|
}
|
|
if got := Fuse(60, nil, []Match{}); len(got) != 0 {
|
|
t.Errorf("fusing empty lists gave %v, want nothing", got)
|
|
}
|
|
}
|
|
|
|
func TestTopOrdersAndTrims(t *testing.T) {
|
|
ms := []Match{{ID: "c", Score: 1}, {ID: "a", Score: 3}, {ID: "b", Score: 2}}
|
|
equal(t, ids(top(append([]Match(nil), ms...), 2)), []string{"a", "b"})
|
|
equal(t, ids(top(append([]Match(nil), ms...), 0)), []string{"a", "b", "c"})
|
|
equal(t, ids(top(append([]Match(nil), ms...), 99)), []string{"a", "b", "c"})
|
|
}
|
|
|
|
func TestTopBreaksTiesByID(t *testing.T) {
|
|
ms := []Match{{ID: "z", Score: 1}, {ID: "a", Score: 1}, {ID: "m", Score: 1}}
|
|
equal(t, ids(top(ms, 3)), []string{"a", "m", "z"})
|
|
}
|