{ "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": 2, "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": 3, "metadata": {}, "outputs": [], "source": [ "forge = KnowledgeGraphForge(\"../../configurations/demo-forge.yml\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Imports" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "from kgforge.core.commons.strategies import ResolvingStrategy" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Terms" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### scope\n", "Resolve a resource for `female` in the terms." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "female = forge.resolve(\"female\", scope=\"terms\")" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "kgforge.core.resource.Resource" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(female)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\n", " id: http://purl.obolibrary.org/obo/PATO_0000383\n", " label: female\n", "}\n" ] } ], "source": [ "print(female)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### target" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\n", " id: http://purl.obolibrary.org/obo/PATO_0000383\n", " label: female\n", "}\n" ] } ], "source": [ "print(forge.resolve(\"female\", scope=\"terms\", target=\"sex\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### type" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\n", " id: http://purl.obolibrary.org/obo/PATO_0000383\n", " label: female\n", "}\n" ] } ], "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": 10, "metadata": {}, "outputs": [], "source": [ "epfl = forge.resolve(\"EPFL\", scope=\"entities\")" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "kgforge.core.resource.Resource" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(epfl)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\n", " id: https://www.grid.ac/institutes/grid.5333.6\n", " label: École Polytechnique Fédérale de Lausanne\n", " acronym: EPFL\n", "}\n" ] } ], "source": [ "print(epfl)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### target" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\n", " id: https://www.grid.ac/institutes/grid.5333.6\n", " label: École Polytechnique Fédérale de Lausanne\n", " acronym: EPFL\n", "}\n" ] } ], "source": [ "print(forge.resolve(\"EPFL\", scope=\"entities\", target=\"agents\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### type" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\n", " id: https://www.grid.ac/institutes/grid.5333.6\n", " label: École Polytechnique Fédérale de Lausanne\n", " acronym: EPFL\n", "}\n" ] } ], "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": 15, "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": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\n", " id: http://purl.obolibrary.org/obo/PATO_0000384\n", " label: male\n", "}\n" ] } ], "source": [ "print(forge.resolve(text, scope=\"terms\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### exact match" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "None\n" ] } ], "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": 18, "metadata": {}, "outputs": [], "source": [ "results = forge.resolve(text, scope=\"terms\", strategy=ResolvingStrategy.ALL_MATCHES)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "list" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(results)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(results)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "kgforge.core.resource.Resource" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(results[0])" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\n", " id: http://purl.obolibrary.org/obo/PATO_0000384\n", " label: male\n", "}\n", "{\n", " id: http://purl.obolibrary.org/obo/PATO_0000383\n", " label: female\n", "}\n" ] } ], "source": [ "print(*results, sep=\"\\n\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python [conda env:kgforge37]", "language": "python", "name": "conda-env-kgforge37-py" }, "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.7.5" } }, "nbformat": 4, "nbformat_minor": 4 }