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

set_label will INSERT if label not already presented. Fixes #788 #789

Merged
merged 1 commit into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading