Skip to content

Commit

Permalink
Added find() method
Browse files Browse the repository at this point in the history
  • Loading branch information
jesper-friis committed Nov 18, 2024
1 parent 938dbb4 commit 6d83771
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 2 deletions.
2 changes: 1 addition & 1 deletion demo/horizontal/emmo2meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ def get_dim(restriction, name, descr=None):
Property(
name,
type=ptype,
dims=dimensions,
shape=dimensions,
unit=unit,
description=descr,
)
Expand Down
79 changes: 78 additions & 1 deletion ontopy/ontology.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -50,7 +51,7 @@
)

if TYPE_CHECKING:
from typing import List, Sequence
from typing import Iterator, List, Sequence

Check warning on line 54 in ontopy/ontology.py

View check run for this annotation

Codecov / codecov/patch

ontopy/ontology.py#L54

Added line #L54 was not covered by tests


# Default annotations to look up
Expand Down Expand 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(

Check warning on line 2123 in ontopy/ontology.py

View check run for this annotation

Codecov / codecov/patch

ontopy/ontology.py#L2123

Added line #L2123 was not covered by tests
"`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(

Check warning on line 2162 in ontopy/ontology.py

View check run for this annotation

Codecov / codecov/patch

ontopy/ontology.py#L2162

Added line #L2162 was not covered by tests
subject.iri
), predicate, o
yield subject, predicate, o
else:
yield BlankNode(self.world, s), predicate, o

Check warning on line 2167 in ontopy/ontology.py

View check run for this annotation

Codecov / codecov/patch

ontopy/ontology.py#L2167

Added line #L2167 was not covered by tests


class BlankNode:
"""Represents a blank node.
Expand Down
26 changes: 26 additions & 0 deletions tests/ontopy_tests/test_ontology.py
Original file line number Diff line number Diff line change
@@ -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
)

0 comments on commit 6d83771

Please sign in to comment.