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.2 KiB
Go
79 lines
2.2 KiB
Go
package dedupe
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/hanzoai/semantic"
|
|
)
|
|
|
|
// Unique returns ts with repeated assertions removed, keeping the first
|
|
// spelling of each. Two triples repeat when they fold to the same canonical
|
|
// key, so "works at" and "WORKS_AT" under a synonym table are one claim.
|
|
//
|
|
// The kept triple's score is raised to the best any repetition carried: the
|
|
// same claim found twice is better evidence than the same claim found once.
|
|
func Unique(ts []semantic.Triple, c Canon) []semantic.Triple {
|
|
at := make(map[[3]string]int, len(ts))
|
|
out := make([]semantic.Triple, 0, len(ts))
|
|
for _, t := range ts {
|
|
k := c.Key(t)
|
|
if i, ok := at[k]; ok {
|
|
if t.Score > out[i].Score {
|
|
out[i].Score = t.Score
|
|
}
|
|
continue
|
|
}
|
|
at[k] = len(out)
|
|
out = append(out, t)
|
|
}
|
|
return out
|
|
}
|
|
|
|
// Alike scores two assertions on 0 to 1. Assertions about different subjects
|
|
// are not about the same thing and score 0. Otherwise the predicate carries
|
|
// most of the weight and the object the rest, because two documents that
|
|
// agree on the object while naming the relation differently are usually
|
|
// saying one thing, and two that agree on the relation while naming different
|
|
// objects are usually not.
|
|
func Alike(a, b semantic.Triple, c Canon, m Metric) float64 {
|
|
if fold(a.Subject) != fold(b.Subject) {
|
|
return 0
|
|
}
|
|
x, y := c.Key(a), c.Key(b)
|
|
if x == y {
|
|
return 1
|
|
}
|
|
p, o := 1.0, 1.0
|
|
if x[1] != y[1] {
|
|
p = Text(x[1], y[1], m)
|
|
}
|
|
if x[2] != y[2] {
|
|
o = Text(x[2], y[2], m)
|
|
}
|
|
return p*0.6 + o*0.4
|
|
}
|
|
|
|
// Repeats reports the assertions in ts that say the same thing as another,
|
|
// as index pairs into ts, lower index first. A pair is reported when it
|
|
// scores at least like.
|
|
//
|
|
// Indices rather than triples, because the caller knows what each index meant
|
|
// and a copy of the triple would lose that.
|
|
func Repeats(ctx context.Context, ts []semantic.Triple, c Canon, like float64, m Metric) ([][2]int, error) {
|
|
if like < 0 || like > 1 {
|
|
return nil, ErrOption
|
|
}
|
|
var out [][2]int
|
|
for i := range ts {
|
|
if err := ctx.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
for j := i + 1; j < len(ts); j++ {
|
|
if Alike(ts[i], ts[j], c, m) >= like {
|
|
out = append(out, [2]int{i, j})
|
|
}
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|