Ontology Generation from Data Using Semantica: A Complete 6-Stage Pipeline

Semantica converts raw structured data containing entities and relationships into production-ready OWL ontologies through a fully automated six-stage pipeline orchestrated by the OntologyGenerator class.

Ontology generation from data is the process of automatically deriving formal knowledge representations from unstructured or semi-structured inputs. The Semantica framework (semantica-agi/semantica) implements this through a modular Python architecture that transforms JSON-like entity-relationship payloads into Turtle-encoded OWL ontologies ready for reasoning and knowledge graph integration.

The 6-Stage Ontology Generation Pipeline

The OntologyGenerator class in semantica/ontology/ontology_generator.py orchestrates a deterministic pipeline that processes raw data through six distinct logical stages. Each stage is implemented as a dedicated private method within the generator, ensuring traceability and observability via integrated progress tracking and logging utilities.

Stage 1: Semantic Network Parsing

The pipeline begins with _stage1_parse_semantic_network, which normalizes raw entities and relationships into a canonical semantic network. This stage extracts core concepts from the input data, resolves relationship types, and constructs a normalized graph representation that serves as the foundation for all subsequent inference operations.

Stage 2: YAML-to-Definition Conversion

In _stage2_yaml_to_definition, the semantic network is converted into provisional class definitions using the ClassInferrer.infer_classes method. This stage analyzes entity type frequencies and attribute patterns to propose an initial class taxonomy based on the statistical distribution of types in the source data.

Stage 3: Definition-to-Types Mapping

The _stage3_definition_to_types stage transforms provisional definitions into OWL-compatible types by invoking PropertyGenerator.infer_properties. This process distinguishes between object properties (representing relationships between entities) and datatype properties (representing literal attributes like strings or integers).

Stage 4: Hierarchy Generation

Taxonomic structure emerges in _stage4_hierarchy_generation, which delegates to ClassInferrer.build_class_hierarchy to establish subClassOf relationships. The engine analyzes entity similarities and property inheritance patterns to construct a logical class hierarchy that minimizes redundancy while maintaining semantic precision.

Stage 5: TTL Generation

The OWLGenerator class serializes the completed ontology into Turtle (TTL) or other OWL formats. This stage handles IRI generation through NamespaceManager and applies NamingConventions to ensure consistent naming styles across classes and properties.

Stage 6: Symbolic Validation

The final stage invokes OntologyValidator.validate to execute SHACL constraint checking and reasoning verification. This guarantees logical consistency and detects potential contradictions before the ontology enters production use.

Core Inference Engines and Components

Three specialized inference engines power the ontology generation process, each implemented as a dedicated module within the semantica/ontology/ directory.

ClassInferrer: Automated Class Discovery

Located in semantica/ontology/class_inferrer.py, the ClassInferrer analyzes entity type frequencies to discover candidate classes and construct hierarchical relationships. It employs statistical thresholds to distinguish between concrete classes and abstract superclasses, ensuring the resulting taxonomy reflects the actual structure of the source data.

PropertyGenerator: Relationship and Attribute Mapping

The PropertyGenerator in semantica/ontology/property_generator.py creates object properties from inter-entity relationships and datatype properties from entity attributes. It maps JSON value types (strings, numbers, booleans) to appropriate OWL/XSD datatypes while preserving cardinality constraints inferred from the data distribution.

SHACLGenerator and Ontology Validation

The SHACLGenerator (semantica/ontology/shacl_generator.py) produces SHACL shape graphs that define constraints for the generated ontology. When combined with OntologyValidator, this enables automated validation of subclass hierarchies, domain-range constraints, and property cardinalities through standards-compliant SHACL validation.

Practical Implementation: Generating Ontologies from Raw Data

Semantica accepts standard JSON payloads containing entities and relationships. The input format requires an entities array with type indicators and a relationships array defining connections between identifiers.

Basic Ontology Generation

from semantica.ontology import OntologyGenerator

# Initialize with a custom namespace

gen = OntologyGenerator(base_uri="https://example.org/ontology/")

# Structured data input

data = {
    "entities": [
        {"type": "Person", "name": "Alice", "age": 28},
        {"type": "Person", "name": "Bob", "age": 35},
        {"type": "Company", "name": "Acme", "founded": "1999-01-01"},
    ],
    "relationships": [
        {"type": "employedBy", "source": "Alice", "target": "Acme"},
        {"type": "employedBy", "source": "Bob", "target": "Acme"},
    ],
}

# Execute the full 6-stage pipeline

ontology = gen.generate_ontology(data, name="EnterpriseOntology")

# Inspect inferred structure

print("Classes:", [c["name"] for c in ontology["classes"]])
print("Properties:", [p["name"] for p in ontology["properties"]])

Exporting to OWL/Turtle Format

from semantica.ontology import OntologyGenerator, OWLGenerator

gen = OntologyGenerator(base_uri="https://example.org/ontology/")
ont = gen.generate_ontology(data)

# Serialize to Turtle format

owl = OWLGenerator(base_uri="https://example.org/ontology/")
turtle_output = owl.serialize(ont, format="turtle")

# Write to file or knowledge graph

with open("output.ttl", "w") as f:
    f.write(turtle_output)

Generating SHACL Shapes for Validation

from semantica.ontology import SHACLGenerator

# Create shapes graph from generated ontology

shacl = SHACLGenerator(base_uri="https://example.org/ontology/")
shapes_graph = shacl.generate(ontology)

# Export validation rules

shacl_ttl = shacl.serialize(shapes_graph, "turtle")
print(shacl_ttl)

CLI Usage for Automated Workflows

Semantica provides a command-line interface for integration into CI/CD pipelines and batch processing workflows. The CLI invokes the same OntologyGenerator pipeline with configurable base URIs and output formats.

semantica ontology generate \
    --base-uri https://example.org/ontology/ \
    --input data.json \
    --output my_ontology.ttl

This command executes all six stages automatically, applying the default naming conventions and namespace management rules defined in semantica/ontology/naming_conventions.py and semantica/ontology/namespace_manager.py.

Summary

  • Six-stage automation: The OntologyGenerator in semantica/ontology/ontology_generator.py orchestrates parsing, definition, typing, hierarchy, serialization, and validation through discrete private methods.
  • Statistical inference: ClassInferrer and PropertyGenerator analyze entity frequencies and relationships to derive OWL classes and properties without manual modeling.
  • Standards compliance: Output formats include Turtle-encoded OWL with optional SHACL shape generation for constraint validation.
  • Production-ready: Integrated logging, progress tracking, and symbolic validation via OntologyValidator ensure generated ontologies are logically consistent and ready for knowledge graph deployment.

Frequently Asked Questions

What data format does Semantica require for ontology generation?

Semantica accepts JSON payloads containing two top-level keys: entities (an array of objects with a type field) and relationships (an array with type, source, and target fields). The framework normalizes this structure during Stage 1 (_stage1_parse_semantic_network) into an internal semantic network representation.

Can I customize the URI structure and naming conventions?

Yes. The OntologyGenerator accepts a base_uri parameter to define the namespace root, while NamingConventions in semantica/ontology/naming_conventions.py centralizes styling rules for classes (PascalCase) and properties (camelCase). The NamespaceManager handles IRI minting to ensure unique, resolvable identifiers throughout the generated ontology.

How does Semantica validate the generated ontology?

Validation occurs in Stage 6 through OntologyValidator.validate, which performs SHACL constraint checking and reasoning validation. Additionally, you can generate explicit SHACL shape graphs using SHACLGenerator (semantica/ontology/shacl_generator.py) to validate external data against the inferred ontology structure.

What output formats are supported besides Turtle?

While the examples demonstrate Turtle (TTL) serialization via OWLGenerator, the underlying implementation uses standard OWLAPI-compatible libraries that support RDF/XML, OWL/XML, and N-Triples formats. Specify the desired format in the serialize() method's format parameter.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →