{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"ExecuteTime": {
"end_time": "2019-09-23T18:50:19.036357Z",
"start_time": "2019-09-23T18:50:19.031896Z"
}
},
"source": [
"# RDF Conversions\n",
"\n",
"This notebook demonstrates how to [convert](https://nexus-forge.readthedocs.io/en/latest/interaction.html#converting) data in any RDF format (n3, Turtle, ...) to Resources and vice-versa. "
]
},
{
"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": "markdown",
"metadata": {},
"source": [
"A configuration file is needed in order to create a KnowledgeGraphForge session. A configuration can be generated using the notebook [00-Initialization.ipynb](00%20-%20Initialization.ipynb)."
]
},
{
"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 import Resource"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Resource to rdflib.Graph"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"jane = Resource(type=\"Person\", name=\"Jane Doe\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"forge.register(jane)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"graph = forge.as_graph(jane)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"len(graph) == 2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for triple in graph:\n",
" print(triple)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"graph_store_metadata = forge.as_graph(jane, store_metadata=True)\n",
"len(graph_store_metadata)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for triple in graph_store_metadata:\n",
" print(triple)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## rdflib.Graph to Resource"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#data in n3 format\n",
"data = '''\n",
"@prefix schema: .\n",
"[] a schema:Person;\n",
" schema:name \"Jane Doe\" ;\n",
" schema:knows ;\n",
" schema:affiliation .\n",
"\n",
" a schema:Person;\n",
" schema:name \"John Doe\" .\n",
" \n",
" a schema:Organization;\n",
" schema:name \"EPFL\" .\n",
"'''"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import rdflib\n",
"graph = rdflib.Graph()\n",
"graph.parse(data=data, format=\"n3\")\n",
"len(graph)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"resources = forge.from_graph(graph)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"len(resources) == 3"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Apply a JSON-LD Frame"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"frame = {\n",
" \"@type\": [ 'http://schema.org/Person'],\n",
" \"@embed\": True\n",
"} \n",
"resources = forge.from_graph(data=graph, frame= frame)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"len(resources) == 2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"forge.as_jsonld(resources)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Select a Type"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"_type= [\"http://schema.org/Organization\"]\n",
"resources = forge.from_graph(data=graph, type= _type)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"resources"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"isinstance(resources, Resource)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"epfl_json = {'id': 'https://www.grid.ac/institutes/grid.5333.6',\n",
" 'type': 'schema:Organization',\n",
" 'schema:name': 'EPFL'}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"epfl_json == forge.as_json(resources)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Use the forge Model context"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"_type= [\"http://schema.org/Organization\"]\n",
"resources = forge.from_graph(data=graph, type= _type, use_model_context=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"isinstance(resources, Resource)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python (nexusforgelatest)",
"language": "python",
"name": "nexusforgelatest"
},
"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
}