Documentation
¶
Overview ¶
Package kg folds extracted triples into a knowledge graph and answers questions about its shape.
The same assertion arriving twice is one edge, not two. Nodes are keyed by the folded form of their name and edges by (subject, predicate, object), so repetition raises confidence and adds provenance rather than duplicating structure. Where a graph is kept is store's business; Mem is the in-memory default so this package and its callers work without a database.
Index ¶
- Variables
- func Fold(s string) string
- func Hash(s, p, o string) string
- type Edge
- type Graph
- func (g *Graph) Add(t semantic.Triple) bool
- func (g *Graph) Adj(id string) []string
- func (g *Graph) Between() map[string]float64
- func (g *Graph) Bridges() []Edge
- func (g *Graph) Close() map[string]float64
- func (g *Graph) Degree() map[string]float64
- func (g *Graph) Edges() []Edge
- func (g *Graph) Eigen() map[string]float64
- func (g *Graph) From(id string) []Edge
- func (g *Graph) Groups() [][]string
- func (g *Graph) In(id string) []string
- func (g *Graph) Merge(into, from string) bool
- func (g *Graph) Modularity(groups [][]string) float64
- func (g *Graph) Near(id string, hops int) *Graph
- func (g *Graph) Node(id string) (Node, bool)
- func (g *Graph) Nodes() []Node
- func (g *Graph) Out(id string) []string
- func (g *Graph) Parts() [][]string
- func (g *Graph) Path(from, to string) []string
- func (g *Graph) Rank() map[string]float64
- func (g *Graph) Set(id string, props map[string]any) bool
- func (g *Graph) Size() (nodes, edges int)
- func (g *Graph) Stat() Stat
- func (g *Graph) Sub(ids ...string) *Graph
- func (g *Graph) To(id string) []Edge
- func (g *Graph) Walk(id string, hops int) []Step
- type Mem
- func (m *Mem) Assert(ctx context.Context, s, p, o string) error
- func (m *Mem) Edge(ctx context.Context, from, to, label string) error
- func (m *Mem) Graph() *Graph
- func (m *Mem) Match(ctx context.Context, s, p, o string) ([][3]string, error)
- func (m *Mem) Node(ctx context.Context, id string, props map[string]any) error
- func (m *Mem) Out(ctx context.Context, id string) ([]string, error)
- type Node
- type Pair
- type Span
- type Stat
- type Step
Constants ¶
This section is empty.
Variables ¶
var Types = []string{"type", "is_a", "isa", "instance_of", "rdf:type"}
Types are the predicates read as a statement about what a node is rather than as an edge, when Build is given no others.
Functions ¶
Types ¶
type Edge ¶
type Edge struct {
From string
To string
Label string // predicate, surface form as first asserted
Score float64 // best confidence any assertion gave it
Count int // how many assertions carried it
Docs []string // documents that asserted it, first seen first
}
Edge is one relation asserted between two nodes.
type Graph ¶
type Graph struct {
// contains filtered or unexported fields
}
Graph holds nodes and the edges between them. Insertion order is preserved so every listing and every traversal is reproducible.
func Build ¶
Build folds triples into a graph. Predicates named in types state a node's type instead of making an edge; with none given, the package Types apply. Build(nil) is the empty graph, ready for Add.
func (*Graph) Add ¶
Add asserts one triple and reports whether it said anything: a triple missing a subject, predicate or object is dropped. Repeating an assertion raises its count, keeps the highest confidence seen, and records the further source; it never makes a second edge. A zero Score counts as 1, because an extractor that states no confidence is not stating no confidence.
func (*Graph) Between ¶
Between scores each node by how many shortest paths run through it, over the pairs it could serve. It finds the nodes a graph would come apart without, which is not the same as the nodes with the most edges.
Brandes' accumulation: one pass per node, dependencies summed backwards along the paths that pass through.
func (*Graph) Bridges ¶
Bridges are the links whose loss would split the graph further. Removing one raises the number of components by exactly one, which makes them the edges a knowledge graph can least afford to be wrong about.
This is Tarjan's rule: a link u–v bridges when nothing reachable from v without crossing it was discovered before u.
func (*Graph) Close ¶
Close scores each node by how near it is to the rest: the nodes it reaches over the total distance to them. A node one step from everything scores 1, and one that reaches nothing scores 0.
func (*Graph) Degree ¶
Degree scores each node by how many neighbours it has, over the most it could have. A node joined to everything scores 1.
func (*Graph) Eigen ¶
Eigen scores each node by the scores of its neighbours, settled by power iteration and scaled so the highest is 1. Being joined to important nodes counts for more than being joined to many.
func (*Graph) From ¶ added in v0.3.0
From lists the assertions made out of a node, first asserted first, and To those made into it. Out and In name the far end and nothing else; these carry the label, the confidence and the documents along with it, which is what a caller that means to cite its evidence needs.
func (*Graph) Groups ¶
Groups partitions the nodes into communities by moving each node to the neighbouring group that raises modularity most, until nothing moves. Nodes with no edges stay alone. Groups come back largest first.
func (*Graph) Merge ¶
Merge folds one node into another: edges, documents, properties and the higher confidence move to into, and from becomes one of its aliases. It is the mechanism an entity resolver drives — kg performs the merge, it does not decide which two names are the same thing. It reports whether anything moved.
func (*Graph) Modularity ¶
Modularity scores a partition: how much more of the graph's links fall inside its groups than chance would put there. Above about 0.3 is community structure worth believing; 0 is what random groups get.
func (*Graph) Near ¶
Near is the neighbourhood of a node: everything reachable within hops steps, ignoring direction, as a graph in its own right. Zero hops is the node alone; a node the graph does not hold gives an empty graph.
func (*Graph) Parts ¶
Parts are the connected components, largest first and then in the order their first node was asserted. Every node is in exactly one.
func (*Graph) Path ¶
Path is the shortest route between two nodes, ignoring direction, as the nodes along it including both ends. It is nil when there is no route; the number of steps is one less than its length.
func (*Graph) Rank ¶
Rank is PageRank over the asserted direction: the share of a random walker's time spent at each node, following edges with probability damping and jumping anywhere with the rest. Scores sum to 1. A node with no outgoing edges hands its share to the jump rather than losing it.
func (*Graph) Set ¶
Set records a node and its properties, creating it if this is the first the graph has heard of it, and reports whether the id was usable. Properties already present are left alone: the first writer of a value wins. It is how a node with no assertions yet — the isolated node analytics must still count — gets into the graph.
func (*Graph) Sub ¶
Sub is the subgraph induced by a set of nodes: those nodes, and every edge with both ends among them.
func (*Graph) Walk ¶ added in v0.3.0
Walk lists the assertions within hops steps of a node, nearest first.
It is the edge-wise counterpart of Near. Near says which nodes lie close and gives them back as a graph; Walk says which assertions do, and an assertion is what carries a label, a confidence and the documents behind it. A caller that walks in order to cite what it found wants the assertions, and building a subgraph to read the edges off it copies the neighbourhood to look at it.
Direction is ignored while walking, since an assertion relates both its ends however it happened to be written. Each edge is reported once, at the fewest hops any path reached it. Zero hops, or a node the graph does not hold, walks nowhere.
type Mem ¶
type Mem struct {
// contains filtered or unexported fields
}
Mem keeps a graph in memory and serves it through store.Graph and store.Triple, so a caller with no database still has somewhere to put a graph. Writes go through the same merge as Add: the same assertion twice is one edge. It is safe for concurrent use, and its zero value is an empty store ready to write to.
func Keep ¶
Keep serves an already built graph. Mem takes it over: later writes through Mem change it, so a caller that wants the original untouched passes a Sub of everything.
func (*Mem) Edge ¶
Edge records a relation between two nodes, creating either that the graph has not heard of.
func (*Mem) Graph ¶
Graph is the graph Mem holds, for the measures in this package. It is the live graph, not a copy, and reading it while another goroutine writes is the caller's business to order.
func (*Mem) Match ¶
Match returns the asserted triples matching a pattern, first asserted first. An empty subject, predicate or object stands for any, so Match with three empty strings returns everything.
type Node ¶
type Node struct {
ID string // stable key, the folded form of the name
Name string // surface form, as first asserted
Type string // set by a type predicate; empty until one arrives
Score float64 // best confidence any assertion gave it
Props map[string]any // properties, as written by a store caller
Docs []string // documents that asserted it, first seen first
Alias []string // ids folded into this one by Merge
}
Node is one thing the graph knows about.
type Stat ¶
type Stat struct {
Nodes int // nodes
Edges int // asserted edges
Links int // distinct undirected links between different nodes
Density float64 // links over the links a complete graph would have
Deg Span // neighbours per node
Parts int // connected components
Biggest int // nodes in the largest component
Whole bool // one component, so everything reaches everything
Path float64 // mean shortest path over the pairs that have one
Shape string // disconnected, sparse, moderate or dense
}
Stat is what the graph looks like from a distance.