Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated get_by_label() so that it now accepts label, name and full iri #582

Merged
merged 12 commits into from
Apr 15, 2023
Merged
Prev Previous commit
Next Next commit
Added options imported and colon_in_name to get_by_label()
jesper-friis committed Apr 14, 2023

Verified

This commit was signed with the committer’s verified signature. The key has expired.
justinsb Justin Santa Barbara
commit 4eb77efa8df2ed73cf99ebb42e3021d2042d4615
8 changes: 7 additions & 1 deletion ontopy/excelparser.py
Original file line number Diff line number Diff line change
@@ -21,6 +21,7 @@
from ontopy import get_ontology
from ontopy.utils import EMMOntoPyException, NoSuchLabelError
from ontopy.utils import ReadCatalogError, read_catalog
from ontopy.ontology import LabelDefinitionError
from ontopy.manchester import evaluate
import owlready2 # pylint: disable=C0411

@@ -276,7 +277,12 @@ def create_ontology_from_pandas( # pylint:disable=too-many-locals,too-many-bran
if not parents:
parents = [owlready2.Thing]

concept = onto.new_entity(name, parents)
try:
concept = onto.new_entity(name, parents)
except LabelDefinitionError:
concepts_with_errors["wrongly_defined"].append(name)
continue

added_rows.add(index)
# Add elucidation
try:
65 changes: 44 additions & 21 deletions ontopy/ontology.py
Original file line number Diff line number Diff line change
@@ -186,6 +186,15 @@ def __init__(self, *args, **kwargs):
doc="Whether to include imported ontologies in dir() listing.",
)

# Other settings
_colon_in_name = False
colon_in_name = property(
fget=lambda self: self._colon_in_name,
fset=lambda self, v: setattr(self, "_colon_in_name", bool(v)),
doc="Whether to accept colon in name-part of IRI. "
"If true, the name cannot be prefixed.",
)

def __dir__(self):
dirset = set(super().__dir__())
lst = list(self.get_entities(imported=self._dir_imported))
@@ -256,7 +265,12 @@ def get_unabbreviated_triples(
)

def get_by_label(
self, label: str, label_annotations: str = None, prefix: str = None
self,
label: str,
label_annotations: str = None,
prefix: str = None,
imported: bool = True,
colon_in_name: bool = None,
):
"""Returns entity with label annotation `label`.

@@ -271,6 +285,10 @@ def get_by_label(
the base iri of an ontology (with trailing slash (/) or hash
(#) stripped off). The search for a matching label will be
limited to this namespace.
imported: Whether to also look for `label` in imported ontologies.
colon_in_name: Whether to accept colon (:) in name-part of IRI.
Defaults to the `colon_in_name` property of `self`.
A true value cannot be combined with `prefix`.

If several entities have the same label, only the one which is
found first is returned.Use get_by_label_all() to get all matches.
@@ -294,17 +312,25 @@ def get_by_label(
except ValueError:
pass

splitlabel = label.split(":", 1)
if len(splitlabel) == 2 and not splitlabel[1].startswith("//"):
label = splitlabel[1]
if prefix and prefix != splitlabel[0]:
warnings.warn(
f"Prefix given both as argument ({prefix}) "
f"and in label ({splitlabel[0]}). "
"Prefix given in argument takes presendence "
if colon_in_name is None:
colon_in_name = self._colon_in_name
if colon_in_name:
if prefix:
raise ValueError(
"`prefix` cannot be combined with `colon_in_name`"
)
if not prefix:
prefix = splitlabel[0]
else:
splitlabel = label.split(":", 1)
if len(splitlabel) == 2 and not splitlabel[1].startswith("//"):
label = splitlabel[1]
if prefix and prefix != splitlabel[0]:
warnings.warn(
f"Prefix given both as argument ({prefix}) "
f"and in label ({splitlabel[0]}). "
"Prefix given in argument takes presendence "
)
if not prefix:
prefix = splitlabel[0]

if prefix:
entitylist = self.get_by_label_all(
@@ -331,16 +357,13 @@ def get_by_label(
if label_annotations
else (a.storid for a in self.label_annotations)
)
get_triples = (
self.world._get_data_triples_spod_spod
if imported
else self._get_data_triples_spod_spod
)
for annotation_id in annotation_ids:
#
# Question to reviewer:
# Should we make it an option (turned off by default) to search
# only the current ontology?
# I have sometimes been wishing for that. It is easily implemented,
# just remove ".world" in the line below.
for s, _, _, _ in self.world._get_data_triples_spod_spod(
None, annotation_id, label, None
):
for s, _, _, _ in get_triples(None, annotation_id, label, None):
return self.world[self._unabbreviate(s)]

# Special labels
@@ -1594,7 +1617,7 @@ def new_entity(

Throws exception if name consists of more than one word.
"""
if len(name.split(" ")) > 1:
if " " in name:
raise LabelDefinitionError(
f"Error in label name definition '{name}': "
f"Label consists of more than one word."