ontology

package
v0.2.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 9, 2026 License: MIT Imports: 10 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package ontology is the type system a graph is read through: classes, properties, the domains and ranges that connect them, and the subclass hierarchy over both. It infers a schema from observed data, checks data against a schema, and reads and writes the interchange formats — JSON-LD, JSON Schema, Turtle and N-Triples.

Index

Constants

View Source
const DefaultBase = "https://semantica.dev/ontology/"

DefaultBase is the namespace terms are minted in when none is given.

Variables

View Source
var ErrCollision = errors.New("name collision")

ErrCollision reports two source names that normalize to one term, which would silently merge two different things into one.

View Source
var Standard = map[string]string{
	"rdf":     "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
	"rdfs":    "http://www.w3.org/2000/01/rdf-schema#",
	"owl":     "http://www.w3.org/2002/07/owl#",
	"xsd":     "http://www.w3.org/2001/XMLSchema#",
	"skos":    "http://www.w3.org/2004/02/skos/core#",
	"sh":      "http://www.w3.org/ns/shacl#",
	"dc":      "http://purl.org/dc/elements/1.1/",
	"dcterms": "http://purl.org/dc/terms/",
}

Standard are the prefixes every RDF document may assume.

Functions

func Camel

func Camel(name string) string

Camel is Pascal with the first word left lower: "has name" is "hasName". A name already in camelCase is left alone, so normalizing is idempotent and "worksFor" does not become "worksfor".

func CheckClass

func CheckClass(name string) (suggestion string, ok bool)

CheckClass reports whether a class name follows the conventions, and what it would be if it did not.

func CheckProperty

func CheckProperty(name string, kind Kind) (suggestion string, ok bool)

CheckProperty reports whether a property name follows the conventions for its kind, and what it would be if it did not. An object property reads as a verb phrase; a data property only has to be lower or camel case.

func CheckSchema

func CheckSchema(name string) (suggestion string, ok bool)

CheckSchema reports whether an ontology name follows the conventions, and what it would be if it did not.

func ClassName

func ClassName(name string) string

ClassName is name written the way a class is written: PascalCase, singular.

func Count

func Count(n int) *int

Count returns a pointer to n, for setting Min and Max inline. A nil Min or Max leaves that end of the count open, which is not the same as a count of zero.

func Cycles

func Cycles(classes []Class) []string

Cycles lists the classes that reach themselves through the hierarchy. A cycle makes the subclass relation meaningless, so it is reported rather than followed.

func Pascal

func Pascal(name string) string

Pascal joins the words of name with each capitalised: "my class" is "MyClass", and so is "MY_CLASS".

func PropertyName

func PropertyName(name string) string

PropertyName is name written the way a property is written: camelCase.

func SchemaName

func SchemaName(name string) string

SchemaName is name written the way an ontology is written: title case with an "Ontology" suffix.

func Singular

func Singular(name string) string

Singular strips a regular English plural ending. It knows nothing of irregular plurals, and leaves words ending in a double s alone.

func Title

func Title(name string) string

Title capitalises each word and separates them with spaces.

func XSD

func XSD(v any) string

XSD names the datatype a value takes. It is deliberately coarse: the point is a range a validator can check, not an exact Go type.

Types

type Class

type Class struct {
	Name    string
	IRI     string
	Label   string
	Comment string
	Parent  string   // the class this one specialises, by name
	Attrs   []string // attribute names shared by the instances observed
	Seen    int      // instances observed
	From    string   // the source type this class was inferred from
}

Class is one type in the schema.

func Hierarchy

func Hierarchy(classes []Class) []Class

Hierarchy fills in the parent of every class that has none. Only the roots a vocabulary conventionally provides — Entity, Thing, Resource — are inferred: guessing a parent from a name overlap invents a taxonomy the data never showed.

type Entity

type Entity struct {
	ID    string
	Type  string
	Name  string
	Attrs map[string]any
}

Entity is one observed instance: what it is, what it is called, and the attributes carried on it. Framework bookkeeping has no place in Attrs, so it cannot be mistaken for a datatype property.

type Graph

type Graph struct {
	Entities []Entity
	Links    []Link
}

Graph is an instance graph: what was observed, before any schema is imposed.

type Inference

type Inference struct {
	Min  int    // observations a type needs before it becomes a class; zero means two
	Base string // namespace for minted IRIs; empty means DefaultBase
	Flat bool   // leave the class hierarchy alone
}

Inference derives a schema from data that has already been observed. It is the way the package is mostly used: entities and links come out of extraction, and the schema that describes them is read back off the data rather than written by hand.

The zero value infers a class from any type seen twice, mints IRIs in DefaultBase, and looks for a hierarchy.

func (Inference) Classes

func (in Inference) Classes(ents []Entity) ([]Class, error)

Classes groups the entities by type and returns one class per type seen at least Min times. Two source types that normalize to the same class name are a collision, not a merge: silently folding them would lose the distinction the data drew.

func (Inference) Properties

func (in Inference) Properties(g Graph, classes []Class) ([]Property, error)

Properties infers object properties from the links and datatype properties from the entity attributes, then merges the ones that normalize to the same name. A name claimed by both an object and a datatype property is a collision: the two cannot share an IRI and mean different things.

func (Inference) Schema

func (in Inference) Schema(g Graph) (Schema, error)

Schema infers classes from the entities, properties from the links and the entity attributes, and returns them as one schema.

type Kind

type Kind string

Kind separates the two sorts of property: one whose values are other entities, one whose values are literals.

const (
	// Object is a property whose values are entities.
	Object Kind = "object"
	// Data is a property whose values are literals.
	Data Kind = "data"
	// Annotation is a property that describes a term rather than constraining
	// its instances, such as a label or an editorial note.
	Annotation Kind = "annotation"
)

func ParseKind

func ParseKind(s string) Kind

ParseKind reads the spellings other vocabularies use for a property kind. An unrecognised spelling is returned unchanged so a caller can report it.

type Link struct {
	Type     string
	From     string
	To       string
	FromType string
	ToType   string
}

Link is one observed relation between two entities. From and To name their endpoints by ID or by Name; FromType and ToType carry the endpoint classes when the source states them.

type Namespace

type Namespace struct {
	Base    string
	Version string
	Bound   map[string]string // bindings beyond the standard ones
}

Namespace mints IRIs for terms and resolves prefixes. The zero value mints in DefaultBase and knows the standard prefixes.

func (Namespace) All

func (n Namespace) All() map[string]string

All returns every prefix in scope, standard and bound.

func (*Namespace) Bind

func (n *Namespace) Bind(prefix, iri string)

Bind records a prefix for an IRI.

func (Namespace) Class

func (n Namespace) Class(name string) string

Class returns the IRI for a class name, in PascalCase.

func (Namespace) Individual

func (n Namespace) Individual(name string) string

Individual returns the IRI for a named instance.

func (Namespace) Property

func (n Namespace) Property(name string) string

Property returns the IRI for a property name, in camelCase.

func (Namespace) Resolve

func (n Namespace) Resolve(prefix string) (string, bool)

Resolve returns the IRI a prefix stands for. Standard prefixes resolve without being bound; a binding of the same name wins.

func (Namespace) Root

func (n Namespace) Root() string

Root is the namespace terms are minted in. A version other than the first is carried in the namespace, never in the term IRIs, so that a term keeps one identity across versions.

type Property

type Property struct {
	Name    string
	IRI     string
	Kind    Kind
	Label   string
	Comment string
	Domain  []string // classes the property applies to
	Range   []string // classes for Object, datatypes for Data
	Min     *int     // fewest values allowed; nil leaves the count open
	Max     *int     // most values allowed; nil leaves the count open
	One     []string // the only values allowed
	Pattern string   // regular expression every value must match
	Seen    int      // observations behind the inference
}

Property is one relation in the schema. An Object property ranges over classes, a Data property over datatypes.

type Schema

type Schema struct {
	IRI        string
	Name       string
	Comment    string
	Version    string
	Base       string // namespace the terms live in
	Prefixes   map[string]string
	Classes    []Class
	Properties []Property
}

Schema is a whole ontology: the terms and the namespace they live in.

func FromStatements

func FromStatements(st []Statement) Schema

FromStatements builds a schema from triples already parsed.

func ParseJSONLD

func ParseJSONLD(src []byte) (Schema, error)

ParseJSONLD reads a schema from a JSON-LD document. The document may hold a graph or a single node, and terms may be written with any prefix the context binds.

func ParseTurtle

func ParseTurtle(src []byte) (Schema, error)

ParseTurtle reads an OWL ontology out of a Turtle or N-Triples document: the classes, the properties, their labels, comments, domains, ranges and the subclass hierarchy. Terms are named by the local part of their IRI, which is the identity the rest of the package indexes by.

func (Schema) Ancestors

func (s Schema) Ancestors(name string) []string

Ancestors lists the classes a class specialises, nearest parent first. A cycle in the hierarchy stops the walk rather than hanging it.

func (Schema) Class

func (s Schema) Class(name string) (Class, bool)

Class returns the class of that name, and whether the schema has one.

func (Schema) JSONLD

func (s Schema) JSONLD() ([]byte, error)

JSONLD writes the schema as a JSON-LD document: a context binding the prefixes and a graph of one node per term.

func (Schema) NTriples

func (s Schema) NTriples() string

NTriples writes the schema one fully expanded triple to a line.

func (Schema) Normalize

func (s Schema) Normalize() Schema

Normalize returns the schema with duplicate terms dropped and the IRIs, labels and endpoints left blank filled in. Terms keep their first occurrence and their order.

func (Schema) Property

func (s Schema) Property(name string) (Property, bool)

Property returns the property of that name, and whether the schema has one.

func (Schema) TermIRI

func (s Schema) TermIRI(name string) string

TermIRI is the IRI a name stands for in this schema: the one the term carries, or the one the namespace would mint for it.

func (Schema) Turtle

func (s Schema) Turtle() string

Turtle writes the schema as an OWL ontology in Turtle.

type Statement

type Statement struct {
	Subject   string
	Predicate string
	Object    string
	Literal   bool
	Datatype  string
	Lang      string
}

Statement is one RDF triple in lexical form. Subject and Predicate are IRIs or blank node labels; Object is one of those unless Literal is set, in which case it is the lexical value and Datatype or Lang describes it.

func Statements

func Statements(src []byte) ([]Statement, error)

Statements parses Turtle into triples. N-Triples is a subset of the same grammar, so it parses too and there is no second entry point for it. The parser is deliberately tolerant: it reads the constructs a serializer emits — prefixes, predicate and object lists, blank node property lists, collections, typed and tagged literals — and reports where it gives up.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL