or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

cli.mdcore-analysis.mddocumentation-generation.mdentity-classes.mdindex.mdsparql-querying.md
tile.json

tessl/pypi-ontospy

Query, inspect and visualize ontologies encoded via RDF and OWL.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/ontospy@2.1.x

To install, run

npx @tessl/cli install tessl/pypi-ontospy@2.1.0

index.mddocs/

Ontospy

A comprehensive Python library and command-line tool for inspecting, querying, and visualizing RDF vocabularies and ontologies encoded using W3C Semantic Web standards (RDF, RDFS, OWL, SKOS). Ontospy provides an intuitive API for loading and interrogating ontologies, with capabilities to analyze ontological structures, extract metadata, and generate detailed HTML documentation.

Package Information

  • Package Name: ontospy
  • Language: Python
  • Installation: pip install ontospy
  • Version: 2.1.1

Core Imports

import ontospy

Common usage patterns:

from ontospy import Ontospy, Ontology, OntoClass, OntoProperty

For entity classes:

from ontospy import RdfEntity, OntoSKOSConcept, OntoShape

Basic Usage

import ontospy

# Load an ontology from file
g = ontospy.Ontospy("path/to/ontology.owl", build_all=True)

# Load from URL
g = ontospy.Ontospy("http://xmlns.com/foaf/0.1/", build_all=True)

# Get basic statistics
stats = g.stats()
print(stats)

# Access ontology components
classes = g.all_classes
properties = g.all_properties
ontologies = g.all_ontologies

# Get specific class by URI
person_class = g.get_class(uri="http://xmlns.com/foaf/0.1/Person")

# Print class hierarchy
g.printClassTree()

# Serialize to RDF format
rdf_output = g.serialize(format="turtle")

Architecture

Ontospy follows a modular architecture centered around entity extraction and analysis:

  • Ontospy Class: Main analysis engine that loads RDF data and extracts ontological structures
  • Entity Classes: Pythonic representations of RDF constructs (classes, properties, concepts, shapes)
  • SPARQL Helper: Query interface for programmatic ontology interrogation
  • Documentation Generator: Visualization and documentation generation system
  • CLI Interface: Command-line tools for interactive ontology exploration

The library uses RDFLib as its core RDF processing engine and provides higher-level abstractions optimized for ontology analysis workflows.

Capabilities

Core Ontology Analysis

Load and analyze RDF ontologies from files, URLs, or SPARQL endpoints. Extract classes, properties, individuals, and ontological structures with comprehensive metadata and relationship information.

class Ontospy:
    def __init__(self, uri_or_path=None, data=None, file_obj=None, 
                 rdf_format="", verbose=False, hide_base_schemas=True,
                 hide_implicit_types=True, hide_implicit_preds=True,
                 hide_individuals=True, sparql_endpoint=None, 
                 credentials=None, build_all=True, pref_title="qname", 
                 pref_lang="en"): ...
    
    def load_rdf(self, uri_or_path=None, data=None, file_obj=None, 
                 rdf_format="", verbose=False): ...
    
    def build_all(self, verbose=False, hide_base_schemas=True,
                  hide_implicit_types=True, hide_implicit_preds=True,
                  hide_individuals=True): ...
    
    def stats(self): ...
    def serialize(self, format="turtle"): ...
    def query(self, sparql_query): ...

Core Analysis

Entity Classes

Structured representations of RDF resources including classes, properties, ontologies, SKOS concepts, and SHACL shapes. Each entity provides methods for traversing relationships, extracting metadata, and analyzing structural properties.

class RdfEntity:
    def __init__(self, uri, rdftype=None, namespaces=None, 
                 ext_model=False, is_Bnode=False, 
                 pref_title="qname", pref_lang="en"): ...
    
    def bestLabel(self, prefLanguage="", qname_allowed=True, quotes=False): ...
    def ancestors(self, cl=None, noduplicates=True): ...
    def descendants(self, cl=None, noduplicates=True): ...

class OntoClass(RdfEntity):
    def count(self): ...
    def describe(self): ...

class OntoProperty(RdfEntity):
    def describe(self): ...

Entity Classes

Documentation Generation

Generate professional HTML documentation, interactive visualizations, and markdown exports. Supports multiple output formats including D3.js visualizations, Bootstrap-themed websites, and single-page documentation.

from ontospy.gendocs.actions import build_visualization
from ontospy.gendocs.viz_factory import VizFactory

def build_visualization(g, viz_index, path=None, title="", theme=""): ...

class VizFactory:
    def __init__(self, ontospy_graph, title=""): ...
    def build(self, output_path=""): ...
    def preview(self): ...

Documentation Generation

Command Line Interface

Interactive command-line tools for ontology exploration, analysis, and documentation generation. Includes shell environment, library management, and batch processing capabilities.

# Entry points
def main_cli(): ...

# CLI commands  
def scan(): ...
def gendocs(): ...
def shell(): ...
def lib(): ...

Command Line Interface

SPARQL Querying

Programmatic SPARQL query interface for extracting specific ontological patterns, performing complex analysis, and interrogating ontology structures using standard query language.

class SparqlHelper:
    def __init__(self, rdfgraph, sparql_endpoint=False): ...
    def getAllClasses(self, hide_base_schemas=True, hide_implicit_types=True): ...
    def getAllProperties(self, hide_implicit_preds=True): ...
    def getSKOSInstances(self): ...
    def getShapes(self): ...

SPARQL Querying

Types

# Core types used across the API
URIRef = rdflib.URIRef
Literal = rdflib.Literal
Graph = rdflib.Graph

# Entity collections
EntityList = list  # List of RdfEntity objects
StatsResult = list  # List of tuples: (category, count)
TreeStructure = list  # Nested list representing hierarchy

# Configuration options
RdfFormat = str  # "turtle", "xml", "n3", "json-ld", etc.
TitlePreference = str  # "qname" or "label"
LanguageCode = str  # ISO language code, e.g. "en", "fr"