1
0
mirror of https://github.com/balkian/gists.git synced 2024-10-31 23:51:44 +00:00
gists/migueljsonld.ipynb

86 lines
42 KiB
Plaintext
Raw Normal View History

2014-04-02 19:27:49 +00:00
{
"metadata": {
"name": "Untitled0"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": "Instalar\n========\nS\u00f3lo hace falta instalar pyld: https://github.com/digitalbazaar/pyld\n\n```bash\npip install pyld \n```\n\nTambi\u00e9n puedes instalar rdflib para convertir el fichero de xml-rdf a nt.\n\nModificar el frame\n==================\nLo que metas dentro del frame se usa para dos cosas:\n * La parte del contexto se usa para compactizar (reducir nombres, son los prefijos)\n * El total se usa para dar estructura al json final\n \nPor tanto, aqu\u00ed puedes decirle qu\u00e9 espacios de nombres y qu\u00e9 campos usar.\nComo ver\u00e1s, adem\u00e1s de hacer framing hago compact.\n\u00bfPor qu\u00e9?, pues porque el framing deja una lista de objetos colgando de \"@graph\", y si esa lista tiene un s\u00f3lo elemento podemos pasar esos atributos a la \"ra\u00edz\" para hacer m\u00e1s legible el json.\n\nEjemplo\n-------\n```json\n{ ...\n \"@graph\": [ {\"@id\": \"test\", \"name\": \"testing\"}]\n}\n```\n\nSe convierte en:\n\n```json\n{ ...\n \"@id\": \"test\",\n \"name\": \"testing\"\n\n}\n```\n\n\nSin l\u00edos\n========\n\nPuedes usar el json-ld playground para estas cosas: [Enlace](http://json-ld.org/playground/index.html#startTab=tab-framed&json-ld=%7B%22%40context%22%3A%7B%22foaf%22%3A%22http%3A%2F%2Fxmlns.com%2Ffoaf%2F0.1%2F%22%2C%22owl%22%3A%22http%3A%2F%2Fwww.w3.org%2F2002%2F07%2Fowl%23%22%2C%22rdfs%22%3A%22http%3A%2F%2Fwww.w3.org%2F2000%2F01%2Frdf-schema%23%22%2C%22image%22%3A%7B%22%40id%22%3A%22http%3A%2F%2Fschema.org%2Fimage%22%2C%22%40type%22%3A%22%40id%22%7D%2C%22tags%22%3A%22http%3A%2F%2Fwww.holygoat.co.uk%2Fowl%2Fredwood%2F0.1%2Ftags%2F%22%2C%22ewe%22%3A%22http%3A%2F%2Fgsi.dit.upm.es%2Fontologies%2Fewe%2Fns%2F%22%2C%22rdf%22%3A%22http%3A%2F%2Fwww.w3.org%2F1999%2F02%2F22-rdf-syntax-ns%23%22%2C%22xsd%22%3A%22http%3A%2F%2Fwww.w3.org%2F2001%2FXMLSchema%23%22%2C%22dcterms%22%3A%22http%3A%2F%2Fpurl.org%2Fdc%2Fterms%2F%22%2C%22homepage%22%3A%7B%22%40id%22%3A%22http%3A%2F%2Fschema.org%2Furl%22%2C%22%40type%22%3A%22%40id%22%7D%2C%22spinrdf%22%3A%22http%3A%2F%2Fspinrdf.org%2Fsp%23%22%7D%2C%22%40graph%22%3A%5B%7B%22%40id%22%3A%22_%3Ab0%22%2C%22spinrdf%3AvarName%22%3A%22message%22%7D%2C%7B%22%40id%22%3A%22_%3Ab1%22%2C%22spinrdf%3AvarName%22%3A%22action%22%7D%2C%7B%22spinrdf%3Atemplates%22%3A%7B%22%40list%22%3A%5B%7B%22spinrdf%3Apredicate%22%3A%7B%22%40id%22%3A%22rdf%3Atype%22%7D%2C%22spinrdf%3Aobject%22%3A%7B%22%40id%22%3A%22http%3A%2F%2Fgsi.dit.upm.es%2Fontologies%2Fewe%23Action%22%7D%2C%22%40id%22%3A%22_%3Ab11%22%2C%22spinrdf%3Asubject%22%3A%7B%22%40id%22%3A%22_%3Ab38%22%2C%22spinrdf%3AvarName%22%3A%22action%22%7D%7D%2C%7B%22spinrdf%3Apredicate%22%3A%7B%22%40id%22%3A%22dcterms%3Atitle%22%7D%2C%22spinrdf%3Aobject%22%3A%22New%20chat%20message%22%2C%22%40id%22%3A%22_%3Ab12%22%2C%22spinrdf%3Asubject%22%3A%7B%22%40id%22%3A%22_%3Ab23%22%2C%22spinrdf%3AvarName%22%3A%22action%22%7D%7D%2C%7B%22spinrdf%3Apredicate%22%3A%7B%22%40id%22%3A%22http%3A%2F%2Fgsi.dit.upm.es%2Fontologies%2Fewe%23hasParameter%22%7D%2C%22spinrdf%3Aobject%22%3A%7B%22%40id%22%3A%22_%3Ab37%22%2C%22spinrdf%3AvarName%22%3A%22iParam%22%7D%2C%22%40id%22%3A%22_%3Ab13%22%2C%22spinrdf%3Asubject%22%3A%7B%22%40id%22%3A%22_%3Ab32%22%2C%22spinrdf%3AvarName%22%3A%22action%22%7D%7D%2C%7B%22spinrdf%3Apredicate%22%3A%7B%22%40id%22%3A%22dcterms%3Atitle%22%7D%2C%22spinrdf%3Aobject%22%3A%22message%22%2C%22%40id%22%3A%22_%3Ab7%22%2C%22spinrdf%3Asubject%22%3A%7B%22%40id%22%3A%22_%3Ab8%22%2C%22spinrdf%3AvarName%22%3A%22iParam%22%7D%7D%2C%7B%22spinrdf%3Apredicate%22%3A%7B%22%40id%22%3A%22dcterms%3Avalue%22%7D%2C%22spinrdf%3Aobject%22%3A%7B%22%40id%22%3A%22_%3Ab0%22%2C%22spinrdf%3AvarName%22%3A%22message%22%7D%2C%22%40id%22%3A%22_%3Ab2%22%2C%22spinrdf%3Asubject%22%3A%7B%22%40id%22%3A%22_%3Ab3%22%2C%22spinrdf%3AvarName%22%3A%22iParam%22%7D%7D%2C%7B%22spinrdf%3Apredicate%22%3A%7B%22%40id%22%3A%22http%3A%2F%2Fgsi.dit.upm.es%2Fontologies%2Fewe%23providesAction%22%7D%2C%22spinrdf%3Aobject%22%3A%7B%22%40id%22%3A%22_%3Ab5%22%2C%22spinrdf%3AvarName%22%3A%22action%22%7D%2C%22%40id%22%3A%22_%3Ab4%22%2C%22spinrdf%3Asubject%22%3A%7B%22%40id%22%3A%22https%3
},
{
"cell_type": "code",
"collapsed": false,
"input": "from rdflib import Graph\ng = Graph()\ng.parse('example.rdf')\nnt = g.serialize(format='nt')",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": "#nt = open('example.nt','r').read()",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": "from pyld import jsonld\nimport json",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": "rdf = jsonld.from_rdf(nt)",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": "frame = {\n \"@context\": {\n \"foaf\": \"http://xmlns.com/foaf/0.1/\",\n \"owl\": \"http://www.w3.org/2002/07/owl#\",\n \"rdf\": \"http://www.w3.org/1999/02/22-rdf-syntax-ns#\",\n \"ewe\": \"http://gsi.dit.upm.es/ontologies/ewe/ns/\",\n \"xsd\": \"http://www.w3.org/2001/XMLSchema#\",\n \"rdfs\": \"http://www.w3.org/2000/01/rdf-schema#\",\n \"dcterms\": \"http://purl.org/dc/terms/\",\n \"tags\": \"http://www.holygoat.co.uk/owl/redwood/0.1/tags/\",\n \"spinrdf\": \"http://spinrdf.org/sp#\",\n \"homepage\": {\n \"@id\": \"http://schema.org/url\",\n \"@type\": \"@id\"\n },\n \"image\": {\n \"@id\": \"http://schema.org/image\",\n \"@type\": \"@id\"\n }\n },\n \"@type\": \"ewe:Rule\"\n}\nugly = jsonld.frame(rdf, frame)\nprint json.dumps(jsonld.compact(ugly, frame), indent=4, sort_keys=True)",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "{\n \"@context\": {\n \"dcterms\": \"http://purl.org/dc/terms/\", \n \"ewe\": \"http://gsi.dit.upm.es/ontologies/ewe/ns/\", \n \"foaf\": \"http://xmlns.com/foaf/0.1/\", \n \"homepage\": {\n \"@id\": \"http://schema.org/url\", \n \"@type\": \"@id\"\n }, \n \"image\": {\n \"@id\": \"http://schema.org/image\", \n \"@type\": \"@id\"\n }, \n \"owl\": \"http://www.w3.org/2002/07/owl#\", \n \"rdf\": \"http://www.w3.org/1999/02/22-rdf-syntax-ns#\", \n \"rdfs\": \"http://www.w3.org/2000/01/rdf-schema#\", \n \"spinrdf\": \"http://spinrdf.org/sp#\", \n \"tags\": \"http://www.holygoat.co.uk/owl/redwood/0.1/tags/\", \n \"xsd\": \"http://www.w3.org/2001/XMLSchema#\"\n }, \n \"@id\": \"http://gsi.dit.upm.es/ontologies/example#Rule_1\", \n \"@type\": \"ewe:Rule\", \n \"dcterms:description\": \"It sends the user a new chat message thought the GoogleTalk channel every time a new email is received. The content of the chat message specify the senders email address, so the user can decide if it is important or not.\", \n \"dcterms:title\": \"Inform of new emails\", \n \"ewe:executes\": {\n \"@id\": \"https://ifttt.com/channels/google_talk/actions/23\"\n }, \n \"ewe:hasCreator\": {\n \"@id\": \"http://gsi.dit.upm.es/ontologies/example#User_1\"\n }, \n \"ewe:timesUsed\": \"0\", \n \"ewe:triggeredBy\": {\n \"@id\": \"https://ifttt.com/channels/gmail/triggers/185\"\n }, \n \"http://spinrdf.org/spin#query\": {\n \"@id\": \"_:b16\", \n \"@type\": \"spinrdf:Construct\", \n \"spinrdf:templates\": {\n \"@list\": [\n {\n \"@id\": \"_:b17\", \n \"spinrdf:object\": {\n \"@id\": \"http://gsi.dit.upm.es/ontologies/ewe#Action\"\n }, \n \"spinrdf:predicate\": {\n \"@id\": \"rdf:type\"\n }, \n \"spinrdf:subject\": {\n \"@id\": \"_:b31\", \n \"spinrdf:varName\": \"action\"\n }\n }, \n {\n \"@id\": \"_:b18\", \n \"spinrdf:object\": \"New chat message\", \n \"spinrdf:predicate\": {\n \"@id\": \"dcterms:title\"\n }, \n \"spinrdf:subject\": {\n \"@id\": \"_:b38\", \n \"spinrdf:varName\": \"action\"\n }\n }, \n {\n \"@id\": \"_:b19\", \n \"spinrdf:object\": {\n \"@id\": \"_:b29\", \n \"spinrdf:varName\": \"iParam\"\n }, \n \"spinrdf:predicate\": {\n \"@id\": \"http://gsi.dit.upm.es/ontologies/ewe#hasParameter\"\n }, \n \"spinrdf:subject\": {\n \"@id\": \"_:b8\", \n \"spinrdf:varName\": \"action\"\n }\n }, \n {\n \"@id\": \"_:b0\", \n \"spinrdf:object\": \"message\", \n \"spinrdf:predicate\": {\n \"@id\": \"dcterms:title\"\n }, \n \"spinrdf:subject\": {\n \"@id\": \"_:b1\", \n \"spinrdf:varName\": \"iParam\"\n }\n }, \n {\n \"@id\": \"_:b20\", \n \"spinrdf:object\": {\n \"@id\": \"_:b34\", \n \"spinrdf:varName\": \"message\"\n }, \n \"spinrdf:predicate\": {\n \"@id\": \"dcterms:value\"\n }, \n
}
],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": "print json.dumps(ugly, indent=2)",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "{\n \"@context\": {\n \"foaf\": \"http://xmlns.com/foaf/0.1/\", \n \"owl\": \"http://www.w3.org/2002/07/owl#\", \n \"rdfs\": \"http://www.w3.org/2000/01/rdf-schema#\", \n \"image\": {\n \"@id\": \"http://schema.org/image\", \n \"@type\": \"@id\"\n }, \n \"tags\": \"http://www.holygoat.co.uk/owl/redwood/0.1/tags/\", \n \"ewe\": \"http://gsi.dit.upm.es/ontologies/ewe/ns/\", \n \"rdf\": \"http://www.w3.org/1999/02/22-rdf-syntax-ns#\", \n \"xsd\": \"http://www.w3.org/2001/XMLSchema#\", \n \"dcterms\": \"http://purl.org/dc/terms/\", \n \"homepage\": {\n \"@id\": \"http://schema.org/url\", \n \"@type\": \"@id\"\n }, \n \"spinrdf\": \"http://spinrdf.org/sp#\"\n }, \n \"@graph\": [\n {\n \"ewe:executes\": {\n \"@id\": \"https://ifttt.com/channels/google_talk/actions/23\"\n }, \n \"dcterms:title\": \"Inform of new emails\", \n \"ewe:timesUsed\": \"0\", \n \"ewe:triggeredBy\": {\n \"@id\": \"https://ifttt.com/channels/gmail/triggers/185\"\n }, \n \"ewe:hasCreator\": {\n \"@id\": \"http://gsi.dit.upm.es/ontologies/example#User_1\"\n }, \n \"dcterms:description\": \"It sends the user a new chat message thought the GoogleTalk channel every time a new email is received. The content of the chat message specify the senders email address, so the user can decide if it is important or not.\", \n \"http://spinrdf.org/spin#query\": {\n \"spinrdf:templates\": {\n \"@list\": [\n {\n \"spinrdf:predicate\": {\n \"@id\": \"rdf:type\"\n }, \n \"spinrdf:object\": {\n \"@id\": \"http://gsi.dit.upm.es/ontologies/ewe#Action\"\n }, \n \"@id\": \"_:b17\", \n \"spinrdf:subject\": {\n \"@id\": \"_:b31\", \n \"spinrdf:varName\": \"action\"\n }\n }, \n {\n \"spinrdf:predicate\": {\n \"@id\": \"dcterms:title\"\n }, \n \"spinrdf:object\": \"New chat message\", \n \"@id\": \"_:b18\", \n \"spinrdf:subject\": {\n \"@id\": \"_:b38\", \n \"spinrdf:varName\": \"action\"\n }\n }, \n {\n \"spinrdf:predicate\": {\n \"@id\": \"http://gsi.dit.upm.es/ontologies/ewe#hasParameter\"\n }, \n \"spinrdf:object\": {\n \"@id\": \"_:b29\", \n \"spinrdf:varName\": \"iParam\"\n }, \n \"@id\": \"_:b19\", \n \"spinrdf:subject\": {\n \"@id\": \"_:b8\", \n \"spinrdf:varName\": \"action\"\n }\n }, \n {\n \"spinrdf:predicate\": {\n \"@id\": \"dcterms:title\"\n }, \n \"spinrdf:object\": \"message\", \n \"@id\": \"_:b0\", \n \"spinrdf:subject\": {\n \"@id\": \"_:b1\", \n \"spinrdf:varName\": \"iParam\"\n }\n }, \n {\n \"spinrdf:predicate\": {\n \"@id\": \"dcterms:value\"\n }, \n \"spinrdf:object\": {\n \"@id\": \"_:b34\", \n \"spinrdf:varName\": \"message\"\n }, \n \"@id\": \"_:b20\", \n \"spinrdf:subject\": {\n \"@id\": \"_:b28\", \n \"spinrdf:varName\": \"iParam\"\n }\n }, \n {\n \"spinrdf:predicate\": {\n \"@id\": \"http://gsi.dit.upm.es/ontologies/ewe#providesAction\"\n }, \n \"spinrdf:object\": {\n \"@id\": \"_:b33\", \n \"spinrdf:varName\": \"action\"\n }, \n \"@id\": \"_:b21\", \n \"spinrdf:subject\": {\n \"@id\": \"https://ifttt.com/GoogleTalk\"\n
}
],
"prompt_number": 6
}
],
"metadata": {}
}
]
}