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]>
79 lines
2.3 KiB
Go
79 lines
2.3 KiB
Go
package store
|
|
|
|
import "sort"
|
|
|
|
// Fuse merges ranked lists by reciprocal rank: an item scores the sum, over
|
|
// the lists it appears in, of 1/(k+rank), counting rank from 1. It needs no
|
|
// scores, only orders, which is what makes it the way to combine answers
|
|
// from sources that score on different scales — a vector store and a text
|
|
// index, say.
|
|
//
|
|
// k damps how much the head of any one list decides the outcome; 60 is the
|
|
// constant from the original description and is used when k is not
|
|
// positive. Metadata comes from the first list an item appeared in.
|
|
func Fuse(k int, lists ...[]Match) []Match {
|
|
if k <= 0 {
|
|
k = 60
|
|
}
|
|
return merge(lists, func(_ int, rank int, _ Match) float64 {
|
|
return 1 / float64(k+rank)
|
|
})
|
|
}
|
|
|
|
// Blend merges ranked lists by weighted sum of the scores they already
|
|
// carry, which is the right combination when the sources score on one
|
|
// scale. A weight missing for a list counts as an equal share, 1/n.
|
|
func Blend(w []float64, lists ...[]Match) []Match {
|
|
share := 0.0
|
|
if len(lists) > 0 {
|
|
share = 1 / float64(len(lists))
|
|
}
|
|
return merge(lists, func(list, _ int, m Match) float64 {
|
|
if list < len(w) {
|
|
return m.Score * w[list]
|
|
}
|
|
return m.Score * share
|
|
})
|
|
}
|
|
|
|
// merge accumulates one score per id across lists and orders the result,
|
|
// highest first, ties broken by where the item was first seen so the answer
|
|
// is the same on every run.
|
|
func merge(lists [][]Match, score func(list, rank int, m Match) float64) []Match {
|
|
sum := map[string]float64{}
|
|
var seen []Match
|
|
at := map[string]bool{}
|
|
for li, l := range lists {
|
|
for i, m := range l {
|
|
if !at[m.ID] {
|
|
at[m.ID] = true
|
|
seen = append(seen, m)
|
|
}
|
|
sum[m.ID] += score(li, i+1, m)
|
|
}
|
|
}
|
|
out := make([]Match, len(seen))
|
|
for i, m := range seen {
|
|
m.Score = sum[m.ID]
|
|
out[i] = m
|
|
}
|
|
sort.SliceStable(out, func(i, j int) bool { return out[i].Score > out[j].Score })
|
|
return out
|
|
}
|
|
|
|
// top orders matches highest first, ties broken by id so the answer never
|
|
// depends on map order, and keeps the first k. A k of zero or less keeps
|
|
// them all.
|
|
func top(ms []Match, k int) []Match {
|
|
sort.Slice(ms, func(i, j int) bool {
|
|
if ms[i].Score != ms[j].Score {
|
|
return ms[i].Score > ms[j].Score
|
|
}
|
|
return ms[i].ID < ms[j].ID
|
|
})
|
|
if k > 0 && k < len(ms) {
|
|
ms = ms[:k]
|
|
}
|
|
return ms
|
|
}
|