{
"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"
]
},
{
"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 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)\n",
"len(graph)"
]
},
{
"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": 50,
"metadata": {},
"outputs": [],
"source": [
"_type= [\"http://schema.org/Organization\"]\n",
"resources = forge.from_graph(data=graph, type= _type)"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 51,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(resources) == 1"
]
},
{
"cell_type": "code",
"execution_count": 55,
"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": 59,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 59,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"epfl_json == forge.as_json(resources[0])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Use the forge Model context"
]
},
{
"cell_type": "code",
"execution_count": 60,
"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": 61,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 61,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(resources) == 1"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda env:kgforge_is_awesome]",
"language": "python",
"name": "kgforge_is_awesome"
},
"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.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}