Created
September 9, 2019 23:50
-
-
Save tonyfast/17588cb44a1cfda3d62bfa88cf7cac57 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
" with __import__('importnb').Notebook():\n", | |
" try:\n", | |
" from . import rdflib_patch\n", | |
" except:\n", | |
" import rdflib_patch\n", | |
" import rdflib, pandas, pydantic, jsonschema, json, networkx, itertools, requests, typing, collections, requests_cache, inspect, IPython, pyld.jsonld as jsonld, abc\n", | |
" from toolz.curried import *; from rdflib.namespace import OWL, RDF, RDFS, SKOS, DC, DCTERMS\n", | |
" requests_cache.install_cache('rdf')\n", | |
"__all__ = tuple(\"Graph CC RDFS HYDRA OWL XHTML RDF XS XSD SW\".split())" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"Graph = rdflib.ConjunctiveGraph()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"for cls in (RDF, RDFS, OWL, 'https://www.w3.org/ns/prov.ttl',\n", | |
" 'http://schema.org/version/latest/schema.ttl',\n", | |
" 'https://raw.githubusercontent.com/AKSW/RDB2RDF-Seminar/master/sparqlmap/eclipse/workspace/xturtle.core/xsd.ttl'): \n", | |
" Graph.parse(data=requests.get(str(cls)).text, format='ttl')\n", | |
" #__name__ == '__main__' and print(F\"{cls} namespace loaded.\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"Graph.parse(data=json.dumps(jsonld.expand(requests.get('https://www.w3.org/ns/hydra/core').json())), format='json-ld')\n", | |
"Graph.parse(data=requests.get('http://www.w3.org/2003/06/sw-vocab-status/ns#').text, format='xml')\n", | |
"g = Graph.parse(data=requests.get('https://creativecommons.org/schema.rdf').text, format='xml')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def split(object): ns, sep, pointer = object.rpartition('/#'['#' in object]); return ns+sep, pointer" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"namespaces = pipe(Graph, concat, filter(flip(isinstance)(rdflib.URIRef)), \n", | |
" filter(flip(str.startswith)('http')), \n", | |
" groupby(compose(first, split)))\n", | |
"\n", | |
"namespaces = pipe(namespaces, valmap(compose(list, map(compose(second, split)))),\n", | |
" valfilter(bool),\n", | |
" itemmap(lambda x: (rdflib.URIRef(x[0]), rdflib.namespace.ClosedNamespace(\n", | |
" x[0], x[1]\n", | |
" ))))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"CC = namespaces[rdflib.term.URIRef('http://creativecommons.org/ns#')]\n", | |
"RDFS = namespaces[rdflib.term.URIRef('http://www.w3.org/2000/01/rdf-schema#')]\n", | |
"HYDRA = namespaces[rdflib.term.URIRef('http://www.w3.org/ns/hydra/core#')]\n", | |
"OWL = namespaces[rdflib.term.URIRef('http://www.w3.org/2002/07/owl#')]\n", | |
"XHTML = namespaces[rdflib.term.URIRef('http://www.w3.org/1999/xhtml/vocab#')]\n", | |
"RDF = namespaces[rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#')]\n", | |
"XS = namespaces[rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#')]\n", | |
"XSD = namespaces[rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema-datatypes#')]\n", | |
"SW = namespaces[rdflib.term.URIRef('http://www.w3.org/2003/06/sw-vocab-status/ns#')]\n", | |
"SCHEMA = namespaces[rdflib.term.URIRef('http://schema.org/')]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def make(object, *mro): \n", | |
" cls = type(second(split(object)), mro, {})\n", | |
" cls.__doc__ = ''.join(Graph[object, RDFS.comment])\n", | |
" cls.type = object\n", | |
" return cls" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"cls = SCHEMA.ReactAction\n", | |
"\n", | |
"types = {RDFS.Resource: make(RDFS.Resource)}\n", | |
"types[RDFS.Class] = make(RDFS.Class, types[RDFS.Resource])\n", | |
"\n", | |
"def mro(cls, object=None):\n", | |
" if object == None: object = tuple()\n", | |
" last = set(object)\n", | |
" for parent in Graph[cls, RDF.type]+ Graph[cls, RDFS.subClassOf]:\n", | |
" if parent != cls:\n", | |
" if parent not in last: object += parent,\n", | |
" \n", | |
" for cls in set(object).difference(last): object = mro(cls, object)\n", | |
" return object" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 18, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def annotate(cls):\n", | |
" cls.__annotations__ = {\n", | |
" second(split(object)): new(object)\n", | |
" for object in Graph[:, RDFS.domain, cls.type] or Graph[:, SCHEMA.domainIncludes, cls.type]\n", | |
" }\n", | |
" for key in cls.__annotations__: getattr(cls, key, setattr(cls, key, None))\n", | |
" cls.__annotations__ = pipe(\n", | |
" cls, inspect.getmro, map(lambda x: getattr(x, '__annotations__', {})), lambda x: collections.ChainMap(*x),\n", | |
" dict\n", | |
" )\n", | |
" return cls\n", | |
" " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 22, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def new(cls):\n", | |
" todo = {cls: mro(cls)}\n", | |
" pipe(cls, mro, map(juxt(identity, mro)), dict, keyfilter(complement(types.__contains__)), todo.update)\n", | |
" \n", | |
" while todo:\n", | |
" for next in pipe(todo, valfilter(compose(all, map(types.__contains__)))):\n", | |
" types[next] = make(next, *map(types.get, sorted(todo.pop(next), key=list(types).index, reverse=True)))\n", | |
" return annotate(types[cls])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 26, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"(__main__.Thing, __main__.Class, __main__.Resource, object)" | |
] | |
}, | |
"execution_count": 26, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"new(SCHEMA.Thing).__mro__" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 25, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{'additionalType': __main__.additionalType,\n", | |
" 'alternateName': __main__.alternateName,\n", | |
" 'url': __main__.url,\n", | |
" 'identifier': __main__.identifier,\n", | |
" 'disambiguatingDescription': __main__.disambiguatingDescription,\n", | |
" 'sameAs': __main__.sameAs,\n", | |
" 'subjectOf': __main__.subjectOf,\n", | |
" 'image': __main__.image,\n", | |
" 'potentialAction': __main__.potentialAction,\n", | |
" 'name': __main__.name,\n", | |
" 'mainEntityOfPage': __main__.mainEntityOfPage,\n", | |
" 'description': __main__.description}" | |
] | |
}, | |
"execution_count": 25, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"new(SCHEMA.Thing).__annotations__" | |
] | |
}, | |
{ | |
"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.7.3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment