{ "cells": [ { "cell_type": "markdown", "metadata": { "ExecuteTime": { "end_time": "2019-09-23T18:50:19.036357Z", "start_time": "2019-09-23T18:50:19.031896Z" } }, "source": [ "# Resolving\n", "\n", "Resolvers are helpers to find commonly used resources that one may want to link resources to. Resolvers are configured in the `Resolvers` section of the configuration file." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2019-09-23T18:50:20.068658Z", "start_time": "2019-09-23T18:50:19.054054Z" } }, "outputs": [], "source": [ "from kgforge.core import KnowledgeGraphForge" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "forge = KnowledgeGraphForge(\"../../configurations/forge.yml\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Imports" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from kgforge.core.commons.strategies import ResolvingStrategy" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Discover scopes, resolvers and targets\n", "\n", "With the `forge.resolvers()` method, the user can inspect the available scopes, resolvers, and targets.\n", "A scope is linked to a data source (directory, store, or URL). Each scope will have at least one resolver, which is an implementation of the Forge Resolver. A resolver can have multiple targets, and it will delimitate the resolution within the resolver's scope." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "forge.resolvers()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Terms" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To find resources using resolver, you can use regex expressions within the provided string." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### scope\n", "Resolve a resource for `female` in the terms." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "female = forge.resolve(\"female\", scope=\"terms\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "type(female)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(female)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### target" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(forge.resolve(\"female\", scope=\"terms\", target=\"sex\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### type" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "print(forge.resolve(\"female\", scope=\"terms\", type=\"Class\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Entity" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### scope\n", "Resolving a resource for `EPFL` in the entities." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "epfl = forge.resolve(\"EPFL\", scope=\"entities\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "type(epfl)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(epfl)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### target" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(forge.resolve(\"EPFL\", scope=\"entities\", target=\"agents\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### type" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(forge.resolve(\"EPFL\", scope=\"entities\", type=\"Organization\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Strategies" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Different strategies can be used to resolve terms. \n", "\n", "In the following example, the missing 'e' at the end is intended for the demonstration." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "text = \"mal\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### best match" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The default applied strategy is `strategy=ResolvingStrategy.BEST_MATCH`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(forge.resolve(text, scope=\"terms\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### exact match" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(forge.resolve(text, scope=\"terms\", strategy=ResolvingStrategy.EXACT_MATCH))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### fuzzy match (all matches)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The result list is ordered by matching relevance." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "results = forge.resolve(text, scope=\"terms\", strategy=ResolvingStrategy.ALL_MATCHES, limit=3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "type(results)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "len(results)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "type(results[0])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(*results, sep=\"\\n\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.10" } }, "nbformat": 4, "nbformat_minor": 4 }