From 693d5fb7771d563b25baea8edf3a40b3c9097209 Mon Sep 17 00:00:00 2001 From: Binh Vu Date: Tue, 7 Nov 2023 08:55:06 -0800 Subject: [PATCH] fix minor issues --- sand/controllers/table.py | 1 + sand/extensions/export/drepr/main.py | 7 ++++++- sand/extensions/export/drepr/semanticmodel.py | 15 +++++---------- sand/extensions/search/default_search.py | 16 ++++++++++++---- sand/extensions/search/wikidata_search.py | 2 +- sand/helpers/mapping_utils.py | 11 +++++++---- sand/helpers/namespace.py | 4 ++++ sand/models/entity.py | 2 +- sand/models/ontology.py | 4 ++-- www/src/models/AssistantService.ts | 6 +++--- 10 files changed, 42 insertions(+), 26 deletions(-) diff --git a/sand/controllers/table.py b/sand/controllers/table.py index aa97c47..77cf3f8 100644 --- a/sand/controllers/table.py +++ b/sand/controllers/table.py @@ -82,6 +82,7 @@ def export_sms(id: int): f"/{table_bp.name}//export", methods=["GET"], ) +@inject def export_table_data( id: int, export: MultiServiceProvider[IExport] = Provide["export"] ): diff --git a/sand/extensions/export/drepr/main.py b/sand/extensions/export/drepr/main.py index b1971b6..c1962a8 100644 --- a/sand/extensions/export/drepr/main.py +++ b/sand/extensions/export/drepr/main.py @@ -89,7 +89,12 @@ def export_data_model(self, table: Table, sm: O.SemanticModel) -> DRepr: ] dsm = get_drepr_sm( - self.appcfg, sm, self.ontprop_ar, get_attr_id, get_ent_attr_id + self.appcfg, + self.namespace, + sm, + self.ontprop_ar, + get_attr_id, + get_ent_attr_id, ) datatype_transformations = [] diff --git a/sand/extensions/export/drepr/semanticmodel.py b/sand/extensions/export/drepr/semanticmodel.py index 5397386..4933874 100644 --- a/sand/extensions/export/drepr/semanticmodel.py +++ b/sand/extensions/export/drepr/semanticmodel.py @@ -2,15 +2,10 @@ import drepr.models.sm as drepr_sm import sm.outputs.semantic_model as O -from sm.namespaces.wikidata import WikidataNamespace from sand.config import AppConfig from sand.helpers.namespace import NamespaceService -from sand.models.ontology import OntProperty, OntPropertyDataType - -prefixes = WikidataNamespace.create().prefix2ns.copy() -prefixes.update(drepr_sm.SemanticModel.get_default_prefixes()) - +from sand.models.ontology import OntPropertyAR, OntPropertyDataType # mapping from predefined datatypes to D-REPR datatype datatype_mapping: Mapping[OntPropertyDataType, drepr_sm.DataType] = { @@ -27,7 +22,7 @@ def get_drepr_sm( appcfg: AppConfig, namespace: NamespaceService, sm: O.SemanticModel, - id2props: Mapping[str, OntProperty], + ontprop_ar: OntPropertyAR, get_attr_id: Callable[[int], str], get_ent_attr_id: Callable[[int], str], ) -> drepr_sm.SemanticModel: @@ -35,7 +30,7 @@ def get_drepr_sm( Args: sm: the semantic model we want to convert - id2props: mapping from the id to ontology property + ontprop_ar: mapping from the id to ontology property get_attr_id: get attribute id from column index get_ent_attr_id: for each entity column, to generate url, we create an extra attribute containing the entity uri, this function get its id based on the column index """ @@ -54,7 +49,7 @@ def get_drepr_sm( # usually, that will be displayed from the UI so users know that datatypes: Set[OntPropertyDataType] = { - id2props[kgns.uri_to_id(inedge.abs_uri)].datatype + ontprop_ar.get_by_uri(inedge.abs_uri).datatype for inedge in sm.in_edges(node.id) } datatype = ( @@ -119,7 +114,7 @@ def get_drepr_sm( return drepr_sm.SemanticModel( nodes=nodes, edges=edges, - prefixes=prefixes, + prefixes=namespace.kgns_prefixes, ) diff --git a/sand/extensions/search/default_search.py b/sand/extensions/search/default_search.py index 8180e28..eaf6097 100644 --- a/sand/extensions/search/default_search.py +++ b/sand/extensions/search/default_search.py @@ -13,17 +13,25 @@ class DefaultSearch(IEntitySearch, IOntologySearch): - @inject def __init__( self, - default_entities: Mapping[str, Entity] = Provide["default_entities"], - default_classes: Mapping[str, OntClass] = Provide["default_classes"], - default_properties: Mapping[str, OntProperty] = Provide["default_properties"], + default_entities: Mapping[str, Entity], + default_classes: Mapping[str, OntClass], + default_properties: Mapping[str, OntProperty], ): self.default_entities = default_entities self.default_classes = default_classes self.default_properties = default_properties + @staticmethod + @inject + def create( + default_entities: Mapping[str, Entity] = Provide["default_entities"], + default_classes: Mapping[str, OntClass] = Provide["default_classes"], + default_properties: Mapping[str, OntProperty] = Provide["default_properties"], + ): + return DefaultSearch(default_entities, default_classes, default_properties) + def local_search(self, mapping: Mapping[str, T], search_text: str) -> list[T]: """performs local partial text search across default entities/classes/properties""" query_tokens = re.findall(r"[a-z]+|\d+", search_text.lower()) diff --git a/sand/extensions/search/wikidata_search.py b/sand/extensions/search/wikidata_search.py index 4e0b613..ace0e05 100644 --- a/sand/extensions/search/wikidata_search.py +++ b/sand/extensions/search/wikidata_search.py @@ -17,7 +17,7 @@ def extended_wikidata_search() -> Union[IEntitySearch, IOntologySearch]: """extended version of wikidata search by aggregating default search""" search = AggregatedSearch() - search.add(DefaultSearch()) + search.add(DefaultSearch.create()) search.add(WikidataSearch()) return search diff --git a/sand/helpers/mapping_utils.py b/sand/helpers/mapping_utils.py index 2f26dde..be00093 100644 --- a/sand/helpers/mapping_utils.py +++ b/sand/helpers/mapping_utils.py @@ -1,7 +1,7 @@ from __future__ import annotations from itertools import chain -from typing import Iterator, Mapping, TypeVar +from typing import Callable, Iterator, Mapping, TypeVar from sm.namespaces.namespace import KnowledgeGraphNamespace @@ -13,11 +13,11 @@ def __init__( self, main: Mapping[str, V], default: Mapping[str, V], - kgns: KnowledgeGraphNamespace, + uri_to_id: Callable[[str], str], ): self.main = main self.default = default - self.kgns = kgns + self.uri_to_id = uri_to_id def __getitem__(self, key: str): if key in self.main: @@ -27,6 +27,9 @@ def __getitem__(self, key: str): def __iter__(self) -> Iterator[str]: return chain(iter(self.main), iter(self.default)) + def values(self) -> Iterator[V]: + return chain(self.main.values(), self.default.values()) + def __len__(self) -> int: return len(self.main) + len(self.default) @@ -39,4 +42,4 @@ def get(self, key: str, default=None): return self.default.get(key, default) def get_by_uri(self, uri: str, default=None): - return self.get(self.kgns.uri_to_id(uri), default) + return self.get(self.uri_to_id(uri), default) diff --git a/sand/helpers/namespace.py b/sand/helpers/namespace.py index 2acc02d..a8d2f5a 100644 --- a/sand/helpers/namespace.py +++ b/sand/helpers/namespace.py @@ -3,6 +3,7 @@ from typing import TYPE_CHECKING, Mapping, Union, cast from dependency_injector.wiring import Provide +from drepr.models.sm import SemanticModel from sm.misc.funcs import import_func from sm.namespaces.namespace import KnowledgeGraphNamespace @@ -24,6 +25,9 @@ def __init__( self.appcfg = appcfg self.kgns: KnowledgeGraphNamespace = import_func(appcfg.kgns)() + self.kgns_prefixes = self.kgns.prefix2ns.copy() + self.kgns_prefixes.update(SemanticModel.get_default_prefixes()) + self.default_entities = default_entities self.default_classes = default_classes self.default_properties = default_properties diff --git a/sand/models/entity.py b/sand/models/entity.py index a36a8d7..30bf200 100644 --- a/sand/models/entity.py +++ b/sand/models/entity.py @@ -93,5 +93,5 @@ def init( return EntityAR( import_func(appcfg.entity.constructor)(**appcfg.entity.args), default_entities, - namespace.kgns, + namespace.uri_to_id, ) diff --git a/sand/models/ontology.py b/sand/models/ontology.py index 0fc5766..d5dafd1 100644 --- a/sand/models/ontology.py +++ b/sand/models/ontology.py @@ -96,7 +96,7 @@ def init( ): cfg = appcfg.clazz func = import_func(cfg.constructor) - return OntClassAR(func(**cfg.args), default_classes, namespace.kgns) + return OntClassAR(func(**cfg.args), default_classes, namespace.uri_to_id) class OntPropertyAR(KGMapping[OntProperty]): @@ -109,5 +109,5 @@ def init( ): func = import_func(appcfg.property.constructor) return OntPropertyAR( - func(**appcfg.property.args), default_properties, namespace.kgns + func(**appcfg.property.args), default_properties, namespace.uri_to_id ) diff --git a/www/src/models/AssistantService.ts b/www/src/models/AssistantService.ts index d7fb1fd..efa8af4 100644 --- a/www/src/models/AssistantService.ts +++ b/www/src/models/AssistantService.ts @@ -64,13 +64,13 @@ export class AssistantService extends RStore { const resp: AxiosResponse = yield axios.get( `${this.remoteURL}/predict/${table.id}`, { - params: { algorithm: "mtab" }, + params: { algorithm: "default" }, } ); // deserialzie the results and put it back to the store - const rawsm = resp.data.mtab.sm; - const rawrows = resp.data.mtab.rows; + const rawsm = resp.data.default.sm; + const rawrows = resp.data.default.rows; const draftId = this.smStore.getNewCreateDraftId(table); const graph = this.smStore.deserialize({