drepr.models.parsers.v3.sm_parser#
Classes
|
|
|
This SemanticModel parser supports various ways to define a semantic model. |
- class SMParseContext(nodes: dict[str, drepr.models.sm.ClassNode | drepr.models.sm.DataNode | drepr.models.sm.LiteralNode] = <factory>, edges: dict[int, drepr.models.sm.Edge] = <factory>, prefixes: dict[str, str] = <factory>)[source]#
Bases:
object- Parameters:
nodes (dict[str, drepr.models.sm.ClassNode | drepr.models.sm.DataNode | drepr.models.sm.LiteralNode]) –
edges (dict[int, drepr.models.sm.Edge]) –
- nodes: dict[str, drepr.models.sm.ClassNode | drepr.models.sm.DataNode | drepr.models.sm.LiteralNode]#
- edges: dict[int, drepr.models.sm.Edge]#
- class SMParser(default_resource_id: str, resources: list[drepr.models.resource.Resource], attrs: ParsedAttrs, path_parser: PathParser, attr_parser: AttrParser)[source]#
Bases:
objectThis SemanticModel parser supports various ways to define a semantic model.
``` semantic_model:
- <class_id>:
[id]: <class_id> [uri]: <uri> [properties]: <properties> [links]: <links> [static_properties]: <static_properties> [inverse_static_properties]: <inverse_static_properties> [subject]: <attr>
- prefixes:
<prefix>: <absolute uri>
where:
<class_id>: (optional) is the id of the class. It can be specified via the key or as in a separate field named <id>
<uri> (optional): if not provided, we extract the URI from the class_id and the class id
must have the following format: <prefix>:<name>:<alphanumeric & _> where <prefix>:<name> is used to create the URI. * <properties>: (optional)
- If it is a list, each item can be either:
[<predicate>, <attr>]
[<predicate>, <attr>, <data_type>]
[<predicate>, <attr>, <is_required>]
[<predicate>, <attr>, <data_type>, <is_required>]
- If it is a tuple, each item is (<predicate>, value) where value can be either:
{ attr: <attr>, [data_type: <data_type=none>], [is_required: <is_required=false>] }
<attr>
<predicate> is either relative or absolute URI.
<data_type> is either relative or absolute URI specifying the data type of the property.
<is_required> is a boolean value specifying whether the property is required or not.
- <attr> is the attribute of the property. It can be either a string for the attribute id or a dictionary
to construct the attribute on fly. The dictionary will comply with the schema of the attribute.
- <static_properties>: (optional)
- If it is a list, each item can be either:
[<predicate>, <value>]
[<predicate>, <value>, <data_type>]
- If it is a dictionary, each item is (<predicate>, item-value) where item-value can be either:
{ value: <value>, data_type: <data_type> }
<value>
<predicate> is either relative or absolute URI.
<data_type> is either relative or absolute URI specifying the data type of the property.
<value> is the value of the property. It must be either a string, integer, float, or boolean.
- <inverse_static_properties>: (optional)
- If it is a list, each item can be either:
[<predicate>, <value>]
If it is a tuple, each item is (<predicate>, item-value) where item-value must be a string, integer, float, or boolean.
- <links>: (optional)
- If it is a list, each item can be either:
a. [<predicate>, <target>] a. [<predicate>, <target>, <is_required>]
- If it is a tuple, each item is (<predicate>, item-value) where item-value can be either:
{ target: <target>, is_required: <is_required> }
<target>
- If it is a dict, then it is:
{ prop: <predicate>, target: <target>, [is_required: <is_required=false>] }
<predicate> is either relative or absolute URI.
<is_required> is a boolean value specifying whether the link is required or not.
- <target> is the target of the link. It can be either a string for the class id or a dictionary
to construct the class on fly. The dictionary will comply with the schema of the class.
- <subject> (optional): is the attribute that can be used as subject attribute of the class. It can be either a string
for the attribute id or a dictionary to construct the attribute on fly. The dictionary will comply with the schema of the attribute.
- Parameters:
default_resource_id (str) –
resources (list[drepr.models.resource.Resource]) –
attrs (ParsedAttrs) –
path_parser (PathParser) –
attr_parser (AttrParser) –
- CLS_KEYS = {'id', 'inverse_static_properties', 'links', 'properties', 'static_properties', 'subject', 'uri'}#
- REG_SM_CLASS = re.compile('^((.+):[a-zA-Z0-9_]+)$')#
- REG_SM_DNODE = re.compile('^((?:(?!--).)+:\\d+)--((?:(?!\\^\\^).)+)(?:\\^\\^(.+))?$')#
- REG_SM_LNODE = re.compile('^((?:(?!--).)+:\\d+)--((?:(?!--).)+)--((?:(?!\\^\\^).)+)(?:\\^\\^(.+))?$')#
- REG_SM_REL = re.compile('^((?:(?!--).)+:\\d+)--((?:(?!--).)+)--((?:(?!--).)+:\\d+)$')#
- parse(sm: dict) SemanticModel[source]#
- Parameters:
sm (dict) –
- Return type:
- parse_class(context: SMParseContext, class_id: str, class_conf: dict)[source]#
- Parameters:
context (SMParseContext) –
class_id (str) –
class_conf (dict) –
- parse_static_property(context: SMParseContext, class_id: str, prop: tuple | list, trace: str)[source]#
Parse a static property of a class. Can parse the following format:
- If prop is a list, it can be either:
[<predicate>, <value>]
[<predicate>, <value>, <data_type>]
- If prop is a tuple, it must be a tuple of (key, val), where val can be either:
{ value: <value>, data_type: <data_type> }
<value>
<predicate> is either relative or absolute URI.
<data_type> is either relative or absolute URI specifying the data type of the property.
<value> is the value of the property. It must be either a string, integer, float, or boolean.
- Parameters:
context (SMParseContext) –
class_id (str) –
trace (str) –
- parse_property(context: SMParseContext, class_id: str, prop: tuple | list, trace: str)[source]#
Parse a property of a class. Can parse the following format:
- If prop is a list, it can be either:
[<predicate>, <attr>]
[<predicate>, <attr>, <data_type>]
[<predicate>, <attr>, <is_required>]
[<predicate>, <attr>, <data_type>, <is_required>]
- If prop is a tuple, it must be a tuple of (key, value), where value can be either:
{ attr: <attr>, [data_type: <data_type=none>], [is_required: <is_required=false>] }
<attr>
<predicate> is either relative or absolute URI.
<data_type> is either relative or absolute URI specifying the data type of the property.
<is_required> is a boolean value specifying whether the property is required or not.
- <attr> is the attribute of the property. It can be either a string for the attribute id or a dictionary
to construct the attribute on fly. The dictionary will comply with the schema of the attribute.
- Parameters:
context (SMParseContext) –
class_id (str) –
trace (str) –
- parse_inverse_static_property(context: SMParseContext, class_id: str, prop: tuple | list, trace: str)[source]#
Parse an inverse static property of a class. Can parse the following format:
- If prop is a list, it can be either:
[<predicate>, <value>]
If prop is a tuple, it must be a tuple of (key, value).
<predicate> is either relative or absolute URI.
<value> is the value of the property. It must be either a string, integer, float, or boolean.
- Parameters:
context (SMParseContext) –
class_id (str) –
trace (str) –
- parse_link(context: SMParseContext, class_id: str, link: tuple | list | dict, trace: str)[source]#
Parse a link of a class. Can parse the following format:
- If link is a list, it can be either:
a. [<predicate>, <target>] a. [<predicate>, <target>, <is_required>]
- If link is a tuple, it must be a tuple of (key, value), where value can be either:
{ target: <target>, is_required: <is_required> }
<target>
<predicate> is either relative or absolute URI.
<is_required> is a boolean value specifying whether the link is required or not.
- <target> is the target of the link. It can be either a string for the class id or a dictionary
to construct the class on fly. The dictionary will comply with the schema of the class.