Skip to content

Commit

Permalink
fix minor issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Binh Vu committed Nov 7, 2023
1 parent d49d4d7 commit 693d5fb
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 26 deletions.
1 change: 1 addition & 0 deletions sand/controllers/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def export_sms(id: int):
f"/{table_bp.name}/<id>/export",
methods=["GET"],
)
@inject
def export_table_data(
id: int, export: MultiServiceProvider[IExport] = Provide["export"]
):
Expand Down
7 changes: 6 additions & 1 deletion sand/extensions/export/drepr/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
15 changes: 5 additions & 10 deletions sand/extensions/export/drepr/semanticmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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] = {
Expand All @@ -27,15 +22,15 @@ 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:
"""Convert sm model into drepr model.
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
"""
Expand All @@ -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 = (
Expand Down Expand Up @@ -119,7 +114,7 @@ def get_drepr_sm(
return drepr_sm.SemanticModel(
nodes=nodes,
edges=edges,
prefixes=prefixes,
prefixes=namespace.kgns_prefixes,
)


Expand Down
16 changes: 12 additions & 4 deletions sand/extensions/search/default_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion sand/extensions/search/wikidata_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 7 additions & 4 deletions sand/helpers/mapping_utils.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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:
Expand All @@ -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)

Expand All @@ -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)
4 changes: 4 additions & 0 deletions sand/helpers/namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion sand/models/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,5 @@ def init(
return EntityAR(
import_func(appcfg.entity.constructor)(**appcfg.entity.args),
default_entities,
namespace.kgns,
namespace.uri_to_id,
)
4 changes: 2 additions & 2 deletions sand/models/ontology.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]):
Expand All @@ -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
)
6 changes: 3 additions & 3 deletions www/src/models/AssistantService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ export class AssistantService extends RStore<number, AssistantRecord> {
const resp: AxiosResponse<Prediction> = 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({
Expand Down

0 comments on commit 693d5fb

Please sign in to comment.