diff --git a/demo/horizontal/emmo2meta.py b/demo/horizontal/emmo2meta.py index 91753a914..d5259b154 100644 --- a/demo/horizontal/emmo2meta.py +++ b/demo/horizontal/emmo2meta.py @@ -269,7 +269,7 @@ def get_dim(restriction, name, descr=None): Property( name, type=ptype, - dims=dimensions, + shape=dimensions, unit=unit, description=descr, ) diff --git a/ontopy/ontology.py b/ontopy/ontology.py index e6fc1d053..33b42538e 100644 --- a/ontopy/ontology.py +++ b/ontopy/ontology.py @@ -13,6 +13,7 @@ import uuid import tempfile import types +import re from pathlib import Path from collections import defaultdict from collections.abc import Iterable @@ -50,7 +51,7 @@ ) if TYPE_CHECKING: - from typing import List, Sequence + from typing import Iterator, List, Sequence # Default annotations to look up @@ -2089,6 +2090,82 @@ def difference(self, other: owlready2.Ontology) -> set: s2 = set(other.get_unabbreviated_triples(blank="_:b")) return s1.difference(s2) + def find( + self, text: str, domain="world", case_sensitive=False, regex=False + ) -> "Iterator": + """A simple alternative to the Owlready2 `search()` method. + + This method searches through all literal strings in the given domain. + + Args: + text: Free text string to search for. + domain: Domain to search. Should be one of: + - "ontology": Current ontology. + - "imported": Current and all imported ontologies. + - "world": The world. + case_sensitive: Whether the search is case sensitive. + regex: Whether to use regular expression search. + + Returns: + Iterator over `(subject, predicate, literal_string)` triples, + converted to EMMOntoPy objects. + + """ + # pylint: disable=too-many-locals,too-many-branches + + if domain == "ontology": + ontologies = [self] + elif domain == "imported": + ontologies = [self] + self.get_imported_ontologies(recursive=True) + elif domain == "world": + ontologies = [self.world] + else: + raise ValueError( + "`domain` must be 'ontology', 'imported' or 'world'. " + f"Got: {domain}" + ) + + # Define our match function + if regex: + flags = 0 if case_sensitive else re.IGNORECASE + pattern = re.compile(f"{text}", flags=flags) + + def matchfun(string): + """Match function using regex.""" + return re.match(pattern, string) + + else: + if not case_sensitive: + text = text.lower() + + def matchfun(string): + """Match function without regex.""" + if case_sensitive: + return text in string + return text in string.lower() + + ontology_storid = self.world._abbreviate( + "http://www.w3.org/2002/07/owl#Ontology" + ) + for onto in ontologies: + for s, p, o, _ in onto._get_data_triples_spod_spod( + None, None, None, None + ): + predicate = self.world.get(self.world._unabbreviate(p)) + if isinstance(o, str) and matchfun(o): + assert isinstance( + s, int + ), "subject should be a storid" # nosec + if s >= 0: + subject = self.world.get(self.world._unabbreviate(s)) + if s == ontology_storid: + yield self.world.get_ontology( + subject.iri + ), predicate, o + yield subject, predicate, o + else: + yield BlankNode(self.world, s), predicate, o + class BlankNode: """Represents a blank node. diff --git a/tests/ontopy_tests/test_ontology.py b/tests/ontopy_tests/test_ontology.py new file mode 100644 index 000000000..0c77a6cbc --- /dev/null +++ b/tests/ontopy_tests/test_ontology.py @@ -0,0 +1,26 @@ +"""Test Ontology class methods.""" + +from ontopy import get_ontology +from ontopy.testutils import ontodir + +animal = get_ontology(ontodir / "animal.ttl").load() + + +# if True: +def test_find(): + """Test find() method.""" + m1 = (animal.chasing, animal.prefLabel, "chasing") + for domain in "ontology", "imported", "world": + assert m1 in animal.find("chas", domain=domain) + assert m1 in animal.find("chas", domain=domain, regex=True) + assert m1 in animal.find("chas", domain=domain, case_sensitive=True) + assert m1 in animal.find( + "chas", domain=domain, regex=True, case_sensitive=True + ) + + assert m1 in animal.find("Chas", domain=domain) + assert m1 in animal.find("Chas", domain=domain, regex=True) + assert m1 not in animal.find("Chas", domain=domain, case_sensitive=True) + assert m1 not in animal.find( + "Chas", domain=domain, regex=True, case_sensitive=True + )