Skip to content

Commit

Permalink
set_label will INSERT if label not already presented. Fixes #788 (#789)
Browse files Browse the repository at this point in the history
  • Loading branch information
cmungall authored Aug 1, 2024
1 parent a112885 commit 2458242
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
from collections import defaultdict
from dataclasses import dataclass
from io import TextIOWrapper
Expand Down Expand Up @@ -140,6 +141,10 @@ def sssom_mappings(self, *args, **kwargs) -> Iterable[Mapping]:
def label(self, curie: CURIE, **kwargs) -> str:
return self._delegate_first(lambda i: i.label(curie, **kwargs))

def set_label(self, curie: CURIE, label: str) -> None:
logging.debug(f"Assuming {curie} is in first aggregated resource, label={label}")
return self._delegate_first(lambda i: i.set_label(curie, label))

def curies_by_label(self, label: str) -> List[CURIE]:
return list(self._delegate_iterator(lambda i: i.curies_by_label(label)))

Expand Down
18 changes: 13 additions & 5 deletions src/oaklib/implementations/sqldb/sql_implementation.py
Original file line number Diff line number Diff line change
Expand Up @@ -788,11 +788,19 @@ def _execute(self, stmt):
self.save()

def set_label(self, curie: CURIE, label: str) -> bool:
stmt = (
update(Statements)
.where(and_(Statements.subject == curie, Statements.predicate == LABEL_PREDICATE))
.values(value=label)
)
existing_label = self.label(curie)
if existing_label:
stmt = (
update(Statements)
.where(and_(Statements.subject == curie, Statements.predicate == LABEL_PREDICATE))
.values(value=label)
)
else:
stmt = (
insert(Statements)
.values(subject=curie, predicate=LABEL_PREDICATE, value=label)
.execution_options(autocommit=True)
)
self._execute(stmt)

def basic_search(self, search_term: str, config: SearchConfiguration = None) -> Iterable[CURIE]:
Expand Down
4 changes: 2 additions & 2 deletions src/oaklib/utilities/lexical/lexical_indexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,8 @@ def add_labels_from_uris(oi: BasicOntologyInterface):
if curie.startswith("<http"):
label = label.replace(">", "")
label = " ".join(label.split("_"))
# print(f'{curie} ==> {label} // {type(oi)}')
oi.set_label(curie, label)
# print(oi.get_label_by_curie(curie))



def create_lexical_index(
Expand Down Expand Up @@ -127,6 +126,7 @@ def _invert_mapping_pred(mapping_pred: PRED_CURIE) -> PRED_CURIE:
logging.info("Creating mapping index")
mapping_pairs_by_curie = defaultdict(list)
for curie in oi.entities():
logging.debug(f"Finding mappings for {curie}")
pairs = list(oi.simple_mappings_by_curie(curie))
for pred, object_id in pairs:
mapping_pairs_by_curie[curie].append((pred, object_id))
Expand Down

0 comments on commit 2458242

Please sign in to comment.