semantic_model

Package

Modules

semantic_model module

class SemanticModel(rdf_file=None, namespaces=None, format=None, parse_namespaces=False, verbose=False, id='semantic_model', dir_conf=None)[source]

Bases: object

count_instances()[source]
Return type:

int

count_triples(s=None, p=None, o=None)[source]
Return type:

int

filter_graph(query)[source]

Filter the graph based on class and predicate filters. The filtering is done using OR(class_filter) and OR(predicate_filter).

Parameters:
  • class_filter – List of class URIs to include (None = no class filtering)

  • predicate_filter – List of predicates to include (None = no predicate filtering)

Return type:

Graph

Returns:

Filtered graph

get_dir(folder_list=None, filename=None)[source]

Get the directory path for storing model-related files.

Parameters:
  • folder_list (List[str]) – List of folder names to create.

  • filename (Optional[str]) – Name of the file to create.

Returns:

The full path to the directory or file, and a boolean indicating if the file exists.

Return type:

Tuple[str, bool]

get_graph(filename, format=None, mappings_dir=None)[source]
Return type:

Graph

get_graph_copy()[source]

Create a complete copy of the graph including namespace bindings.

Returns:

A new graph instance with all triples and namespace bindings copied

Return type:

Graph

get_instance(value, datatype=None, lang=None)[source]

Get a specific instance by URI or create a literal

Parameters:
  • value (Union[str, URIRef, Literal]) – The URI or literal value

  • is_literal – Flag to indicate if the value should be treated as a literal

  • datatype (Optional[URIRef]) – Optional datatype URI for literals

  • lang (Optional[str]) – Optional language tag for literals

Return type:

SemanticObject

Returns:

SemanticObject representing the URI or Literal

get_instances_of_type(class_uris)[source]

Get all instances that match any of the specified types (including subtypes)

Parameters:

class_uris (Union[str, URIRef, SemanticType, Tuple, List]) – Single URI or tuple/list of URIs representing the types to match

Return type:

List[SemanticObject]

Returns:

List of SemanticObject instances that match any of the specified types

get_property(uri)[source]

Get a specific property by URI

Return type:

SemanticProperty

get_type(uri)[source]

Get a specific type by URI

Return type:

SemanticType

parse_namespaces(graph, namespaces=None)[source]
parse_spreadsheet(spreadsheet, mappings_dir=None)[source]

Parse spreadsheet into RDF graph using brickify tool

reason(namespaces=None)[source]

Perform RDFS and OWL reasoning to infer additional triples. Currently, we infer: - Inverse properties - Symmetric properties - Transitive properties - Equivalent classes (owl:equivalentClass) - Subclass reasoning (rdfs:subClassOf) - Equivalent properties (owl:equivalentProperty) - Property chains (owl:propertyChainAxiom) - SameAs reasoning (owl:sameAs)

serialize(folder_list=None)[source]
visualize(query=None, include_full_uri=True)[source]

Visualize RDF graph with optional class and predicate filtering. The filter acts as an OR filter.

Parameters:
  • query – SPARQL CONSTRUCT query to filter the graph

  • include_full_uri – If True, include the last row with the full instance URI. If False, remove it.

class SemanticObject(value, model, datatype=None, lang=None)[source]

Bases: object

Class to represent an ontology instance or literal

get_namespace()[source]
get_predicate_object_pairs()[source]

Return all attributes of this instance

Return type:

Dict[str, Any]

get_short_name()[source]
isinstance(cls)[source]

Check if this instance is of any of the given class types (including inheritance)

Parameters:

cls (Union[str, SemanticType, Tuple[Union[str, SemanticType], ...], List[Union[str, SemanticType]]]) – Single class or tuple/list of classes to check against

Return type:

bool

Returns:

True if instance matches any of the specified classes

property type: List[SemanticType]

Get all types of this instance

class SemanticProperty(uri, graph)[source]

Bases: object

Represents an ontology property

get_short_name()[source]
isproperty(cls)[source]

Check if this instance is of any of the given property types (including inheritance)

Parameters:

property – Single property or tuple/list of properties to check against

Return type:

bool

Returns:

True if instance matches any of the specified properties

property domain: List[URIRef]

Get the domain (valid subject types) of this property

property range: List[URIRef]

Get the range (valid object types) of this property

class SemanticType(uri, model, validate=False)[source]

Bases: object

Represents an ontology class with inheritance

get_short_name()[source]
get_type_attributes()[source]

Find all possible attributes (properties) that can be used with instances of this class.

This method looks for all ObjectProperties defined in the ontology that could be used with instances of this class (self.uri). These are properties that either: 1. Have no domain restrictions (can be used with any class) 2. Have this class or any of its parent classes in their domain

Return type:

Dict[str, List[Any]]

Returns:

Dictionary mapping property names to lists of their allowed range values

has_subclasses()[source]

Check if this type has any subclasses

Return type:

bool

istype(cls)[source]

Check if this instance is of any of the given class types (including inheritance)

Parameters:

cls (Union[str, SemanticType, Tuple[Union[str, SemanticType], ...], List[Union[str, SemanticType]]]) – Single class or tuple/list of classes to check against

Return type:

bool

Returns:

True if instance matches any of the specified classes

property parent_classes: Set[str]

Get all parent classes (including indirect) using RDFS reasoning

get_short_name(uri, namespaces)[source]