def _load(dirpath: Path, filename: str) -> Iterator[Dict[str, str]]:
    filepath = dirpath / filename
    if filepath.is_file():
        with filepath.open(encoding='utf-8') as f:
            yield from json.load(f)
    else:
        raise ConfigurationError(
            "<source>/<bucket> should be a valid file path")
Beispiel #2
0
    def __init__(self, graph: Graph, context_iri: Optional[str] = None) -> None:

        if context_iri is None:
            raise ConfigurationError(f"RdfModel requires a context")
        self._graph = graph
        self._context_cache = dict()
        self.classes_to_shapes = self._build_shapes_map()
        resolved_context = self.resolve_context(context_iri)
        self.context = Context(resolved_context, context_iri)
        self.types_to_shapes: Dict = self._build_types_to_shapes()
Beispiel #3
0
 def from_pretrained(dirpath: Path, filename):
     filepath = dirpath / filename
     if filepath.is_file():
         with filepath.open(mode='rb') as f:
             kb = pickle.load(f)
             aliases = pickle.load(f)
             model = pickle.load(f)
             index = pickle.load(f)
             return EntityLinkerServiceSkLearn(kb, aliases, model, index)
     else:
         raise ConfigurationError(f"{dirpath}/{filename} is not a valid file path")
Beispiel #4
0
def import_class(configuration: str, forge_module_name: str) -> Callable:
    # Example use:
    #   - import_class("DemoModel", "models")
    #   - import_class("CustomModel from package.models", "models")
    #   - import_class("CustomModel from package.models.custom_model", "models")
    archetype = forge_module_name[:-1].capitalize()
    matched = re.match("^([A-Za-z]+)(?: from ([a-z_.]+))?$", configuration)
    if matched:
        try:
            forge_module_import = f"kgforge.specializations.{forge_module_name}"
            class_name, module_import = matched.groups(
                default=forge_module_import)
            module = import_module(module_import)
            return getattr(module, class_name)
        except ModuleNotFoundError:
            raise ConfigurationError(
                f"{archetype} module not found for '{configuration}'")
        except AttributeError:
            raise ConfigurationError(
                f"{archetype} class not found for '{configuration}'")
    else:
        raise ConfigurationError(
            f"incorrect {archetype} configuration for '{configuration}'")
Beispiel #5
0
 def _initialize_service(self, source: str, targets: Dict[str, str],
                         **source_config) -> Any:
     # Resolver data could be accessed from a directory, a web service, or a Store.
     # Initialize the access to the resolver data according to the source type.
     # POLICY Should not use 'self'. This is not a function only for the specialization to work.
     origin = source_config.pop("origin")
     if origin == "directory":
         dirpath = Path(source)
         return self._service_from_directory(dirpath, targets)
     elif origin == "web_service":
         return self._service_from_web_service(source, targets)
     elif origin == "store":
         store = import_class(source, "stores")
         return self._service_from_store(store, targets, **source_config)
     else:
         raise ConfigurationError(
             f"unrecognized Resolver origin '{origin}'")
Beispiel #6
0
 def _initialize_service(self, source: str, **source_config) -> Any:
     # Model data could be loaded from a directory, an URL, or a Store.
     # Initialize the access to the model data according to the source type.
     # POLICY Should not use 'self'. This is not a function only for the specialization to work.
     origin = source_config.pop("origin")
     context_config = source_config.pop("context", {})
     context_iri = context_config.get("iri", None)
     if origin == "directory":
         dirpath = Path(source)
         return self._service_from_directory(dirpath, context_iri)
     elif origin == "url":
         return self._service_from_url(source, context_iri)
     elif origin == "store":
         store = import_class(source, "stores")
         return self._service_from_store(store, context_config, **source_config)
     else:
         raise ConfigurationError(f"unrecognized Model origin '{origin}'")
 def _service_from_directory(dirpath: Path, targets: Dict[str, str], **source_config)\
         -> Dict[str, List[Dict[str, str]]]:
     resolve_with_properties: List[str] = source_config.pop(
         "resolve_with_properties", None)
     if isinstance(resolve_with_properties, str):
         resolve_with_properties = [resolve_with_properties]
     elif resolve_with_properties is not None and not isinstance(
             resolve_with_properties, list):
         raise ConfigurationError(
             f"The 'resolve_with_properties' should be a list: {resolve_with_properties} provided."
         )
     return {
         target: {
             "data": list(_load(dirpath, filename)),
             "resolve_with_properties": resolve_with_properties
         }
         for target, filename in targets.items()
     }