Created
August 4, 2020 13:26
-
-
Save datadavev/994e6a39bded38b75f4e46e88cd70850 to your computer and use it in GitHub Desktop.
Normalizing sschema.org http-https namespace rdflib
This file contains hidden or 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": [ | |
"import rdflib\n", | |
"\n", | |
"gsrc = \"\"\"{\n", | |
" \"@context\":\"http://schema.org/\",\n", | |
" \"@id\":\"/http-https-test\",\n", | |
" \"@type\":\"Thing\",\n", | |
" \"description\":\"http://schema.org/ is the start of this description\"\n", | |
"}\n", | |
"\"\"\"\n", | |
"\n", | |
"def loadTest():\n", | |
" gr = rdflib.ConjunctiveGraph()\n", | |
" gr.parse(data=gsrc, format=\"json-ld\")\n", | |
" return gr\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[\n", | |
" {\n", | |
" \"@id\": \"file:///http-https-test\",\n", | |
" \"@type\": [\n", | |
" \"http://schema.org/Thing\"\n", | |
" ],\n", | |
" \"http://schema.org/description\": [\n", | |
" {\n", | |
" \"@value\": \"http://schema.org/ is the start of this description\"\n", | |
" }\n", | |
" ]\n", | |
" }\n", | |
"]\n" | |
] | |
} | |
], | |
"source": [ | |
"g = loadTest()\n", | |
"print(g.serialize(format=\"json-ld\", indent=2).decode())" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"https://schema.org/ is the start of this description does not look like a valid URI, trying to serialize this will break.\n" | |
] | |
}, | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[\n", | |
" {\n", | |
" \"@id\": \"file:///http-https-test\",\n", | |
" \"@type\": [\n", | |
" \"https://schema.org/Thing\"\n", | |
" ],\n", | |
" \"http://schema.org/description\": [\n", | |
" {\n", | |
" \"@id\": \"https://schema.org/ is the start of this description\"\n", | |
" }\n", | |
" ],\n", | |
" \"https://schema.org/description\": [\n", | |
" {\n", | |
" \"@id\": \"http://schema.org/description\"\n", | |
" }\n", | |
" ]\n", | |
" }\n", | |
"]\n" | |
] | |
} | |
], | |
"source": [ | |
"for s, p, o in g.triples(None):\n", | |
" if str(s).startswith(\"http://schema.org\"):\n", | |
" g.remove((s, p, o))\n", | |
" g.add((rdflib.URIRef(str(s).replace(\"http\", \"https\")), p, o))\n", | |
" \n", | |
" if str(p).startswith(\"http://schema.org\"):\n", | |
" g.remove((s, p, o))\n", | |
" g.add((s, rdflib.URIRef(str(p).replace(\"http\", \"https\")), p, o))\n", | |
"\n", | |
" if str(o).startswith(\"http://schema.org\"):\n", | |
" g.remove((s, p, o))\n", | |
" g.add((s, p, rdflib.URIRef(str(o).replace(\"http\", \"https\"))))\n", | |
"\n", | |
"print(g.serialize(format=\"json-ld\", indent=2).decode()) " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[\n", | |
" {\n", | |
" \"@id\": \"file:///http-https-test\",\n", | |
" \"@type\": [\n", | |
" \"https://schema.org/Thing\"\n", | |
" ],\n", | |
" \"https://schema.org/description\": [\n", | |
" {\n", | |
" \"@value\": \"http://schema.org/ is the start of this description\"\n", | |
" }\n", | |
" ]\n", | |
" }\n", | |
"]\n" | |
] | |
} | |
], | |
"source": [ | |
"g = loadTest()\n", | |
"\n", | |
"for s, p, o in g.triples(None):\n", | |
" changed = False\n", | |
" new_s = s\n", | |
" if str(s).startswith(\"http://schema.org\"):\n", | |
" new_s = rdflib.URIRef(str(s).replace(\"http\", \"https\"))\n", | |
" changed = True\n", | |
" new_p = p\n", | |
" if str(p).startswith(\"http://schema.org\"):\n", | |
" new_p = rdflib.URIRef(str(p).replace(\"http\", \"https\"))\n", | |
" changed = True\n", | |
" new_o = o\n", | |
" if isinstance(o, rdflib.URIRef):\n", | |
" if str(o).startswith(\"http://schema.org\"):\n", | |
" new_o = rdflib.URIRef(str(o).replace(\"http\", \"https\"))\n", | |
" changed = True\n", | |
" if changed:\n", | |
" g.remove((s,p,o))\n", | |
" g.add((new_s, new_p, new_o))\n", | |
"\n", | |
"print(g.serialize(format=\"json-ld\", indent=2).decode()) " | |
] | |
}, | |
{ | |
"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.8.3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment