or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

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

0

# Ontospy

1

2

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.

3

4

## Package Information

5

6

- **Package Name**: ontospy

7

- **Language**: Python

8

- **Installation**: `pip install ontospy`

9

- **Version**: 2.1.1

10

11

## Core Imports

12

13

```python

14

import ontospy

15

```

16

17

Common usage patterns:

18

19

```python

20

from ontospy import Ontospy, Ontology, OntoClass, OntoProperty

21

```

22

23

For entity classes:

24

25

```python

26

from ontospy import RdfEntity, OntoSKOSConcept, OntoShape

27

```

28

29

## Basic Usage

30

31

```python

32

import ontospy

33

34

# Load an ontology from file

35

g = ontospy.Ontospy("path/to/ontology.owl", build_all=True)

36

37

# Load from URL

38

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

39

40

# Get basic statistics

41

stats = g.stats()

42

print(stats)

43

44

# Access ontology components

45

classes = g.all_classes

46

properties = g.all_properties

47

ontologies = g.all_ontologies

48

49

# Get specific class by URI

50

person_class = g.get_class(uri="http://xmlns.com/foaf/0.1/Person")

51

52

# Print class hierarchy

53

g.printClassTree()

54

55

# Serialize to RDF format

56

rdf_output = g.serialize(format="turtle")

57

```

58

59

## Architecture

60

61

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

62

63

- **Ontospy Class**: Main analysis engine that loads RDF data and extracts ontological structures

64

- **Entity Classes**: Pythonic representations of RDF constructs (classes, properties, concepts, shapes)

65

- **SPARQL Helper**: Query interface for programmatic ontology interrogation

66

- **Documentation Generator**: Visualization and documentation generation system

67

- **CLI Interface**: Command-line tools for interactive ontology exploration

68

69

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

70

71

## Capabilities

72

73

### Core Ontology Analysis

74

75

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

76

77

```python { .api }

78

class Ontospy:

79

def __init__(self, uri_or_path=None, data=None, file_obj=None,

80

rdf_format="", verbose=False, hide_base_schemas=True,

81

hide_implicit_types=True, hide_implicit_preds=True,

82

hide_individuals=True, sparql_endpoint=None,

83

credentials=None, build_all=True, pref_title="qname",

84

pref_lang="en"): ...

85

86

def load_rdf(self, uri_or_path=None, data=None, file_obj=None,

87

rdf_format="", verbose=False): ...

88

89

def build_all(self, verbose=False, hide_base_schemas=True,

90

hide_implicit_types=True, hide_implicit_preds=True,

91

hide_individuals=True): ...

92

93

def stats(self): ...

94

def serialize(self, format="turtle"): ...

95

def query(self, sparql_query): ...

96

```

97

98

[Core Analysis](./core-analysis.md)

99

100

### Entity Classes

101

102

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.

103

104

```python { .api }

105

class RdfEntity:

106

def __init__(self, uri, rdftype=None, namespaces=None,

107

ext_model=False, is_Bnode=False,

108

pref_title="qname", pref_lang="en"): ...

109

110

def bestLabel(self, prefLanguage="", qname_allowed=True, quotes=False): ...

111

def ancestors(self, cl=None, noduplicates=True): ...

112

def descendants(self, cl=None, noduplicates=True): ...

113

114

class OntoClass(RdfEntity):

115

def count(self): ...

116

def describe(self): ...

117

118

class OntoProperty(RdfEntity):

119

def describe(self): ...

120

```

121

122

[Entity Classes](./entity-classes.md)

123

124

### Documentation Generation

125

126

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

127

128

```python { .api }

129

from ontospy.gendocs.actions import build_visualization

130

from ontospy.gendocs.viz_factory import VizFactory

131

132

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

133

134

class VizFactory:

135

def __init__(self, ontospy_graph, title=""): ...

136

def build(self, output_path=""): ...

137

def preview(self): ...

138

```

139

140

[Documentation Generation](./documentation-generation.md)

141

142

### Command Line Interface

143

144

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

145

146

```python { .api }

147

# Entry points

148

def main_cli(): ...

149

150

# CLI commands

151

def scan(): ...

152

def gendocs(): ...

153

def shell(): ...

154

def lib(): ...

155

```

156

157

[Command Line Interface](./cli.md)

158

159

### SPARQL Querying

160

161

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

162

163

```python { .api }

164

class SparqlHelper:

165

def __init__(self, rdfgraph, sparql_endpoint=False): ...

166

def getAllClasses(self, hide_base_schemas=True, hide_implicit_types=True): ...

167

def getAllProperties(self, hide_implicit_preds=True): ...

168

def getSKOSInstances(self): ...

169

def getShapes(self): ...

170

```

171

172

[SPARQL Querying](./sparql-querying.md)

173

174

## Types

175

176

```python { .api }

177

# Core types used across the API

178

URIRef = rdflib.URIRef

179

Literal = rdflib.Literal

180

Graph = rdflib.Graph

181

182

# Entity collections

183

EntityList = list # List of RdfEntity objects

184

StatsResult = list # List of tuples: (category, count)

185

TreeStructure = list # Nested list representing hierarchy

186

187

# Configuration options

188

RdfFormat = str # "turtle", "xml", "n3", "json-ld", etc.

189

TitlePreference = str # "qname" or "label"

190

LanguageCode = str # ISO language code, e.g. "en", "fr"

191

```