-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add named graphs to SPARQL interface. Add SPARQL diagnostics
- Loading branch information
Showing
2 changed files
with
290 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,285 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Using the SPARQL Slurper to build graphs\n", | ||
"The following example uses a Wikidata ShEx definition to construct the minimal conforming graph from WikiData. The graph (`permagraph`) is then displayed\n", | ||
"\n", | ||
"Details:\n", | ||
"* -ss - use the SparqlSlurper\n", | ||
"* -sq - a query to find the first 100 items that are instances of wd:Q7397\n", | ||
"* http://query.wikidata.org/sparql -- Wikidata SPARQL endpoint\n", | ||
"* https://raw.githubusercontent.com/shexSpec/schemas/master/Wikidata/DigitalPreservation/SWIB/software.shex - ShEx\n", | ||
"* --stopafter 1 -- only process one node (testing)\n", | ||
"* -ps -- Print SPARQL queries" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from pyshex.shex_evaluator import evaluate_cli as shexeval\n", | ||
"from pyshex import shex_evaluator\n", | ||
"from sparql_slurper import SlurpyGraph" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"SPARQL:\n", | ||
"\n", | ||
"PREFIX wdt: <http://www.wikidata.org/prop/direct/>\n", | ||
"PREFIX wd: <http://www.wikidata.org/entity/>\n", | ||
"\n", | ||
"SELECT ?item WHERE { ?item (wdt:P31/wdt:P279*) wd:Q7397.} LIMIT 100\n", | ||
"\n", | ||
"SPARQL: (SELECT ?s ?p ?o {<http://www.wikidata.org/entity/Q2374> ?p ?o}) (0.32 secs) - 177 triples\n", | ||
"Errors:\n", | ||
" Focus: http://www.wikidata.org/entity/Q2374\n", | ||
" Start: #wikidata-software\n", | ||
" Reason: Testing <http://www.wikidata.org/entity/Q2374> against shape #wikidata-software\n", | ||
" Triples:\n", | ||
" <http://www.wikidata.org/entity/Q2374> p:P178 <http://www.wikidata.org/entity/statement/q2374-04C1F298-5F3D-4220-8714-B0C4587A0282> .\n", | ||
" <http://www.wikidata.org/entity/Q2374> p:P178 <http://www.wikidata.org/entity/statement/q2374-66140209-9732-4C37-BFC4-BDB02A5D0803> .\n", | ||
" <http://www.wikidata.org/entity/Q2374> p:P178 <http://www.wikidata.org/entity/statement/q2374-E713580E-6144-484C-9A86-42B3D7829ED3> .\n", | ||
" 3 triples exceeds max {1,1}\n", | ||
"\n", | ||
"RESULT Graph:\n", | ||
"@prefix ns1: <http://www.w3.org/2004/02/skos/core#> .\n", | ||
"@prefix ns2: <http://wikiba.se/ontology#> .\n", | ||
"@prefix ns3: <http://www.wikidata.org/prop/direct-normalized/> .\n", | ||
"@prefix p: <http://www.wikidata.org/prop/> .\n", | ||
"@prefix pq: <http://www.wikidata.org/prop/qualifier/> .\n", | ||
"@prefix pr: <http://www.wikidata.org/prop/reference/> .\n", | ||
"@prefix prov: <http://www.w3.org/ns/prov#> .\n", | ||
"@prefix prv: <http://www.wikidata.org/prop/reference/value/> .\n", | ||
"@prefix ps: <http://www.wikidata.org/prop/statement/> .\n", | ||
"@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .\n", | ||
"@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n", | ||
"@prefix schema: <http://schema.org/> .\n", | ||
"@prefix wd: <https://www.wikidata.org/wiki/> .\n", | ||
"@prefix wdt: <http://www.wikidata.org/prop/direct/> .\n", | ||
"@prefix xml: <http://www.w3.org/XML/1998/namespace> .\n", | ||
"@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .\n", | ||
"\n", | ||
"<http://www.wikidata.org/entity/Q2374> rdfs:label \"Civilization III\",\n", | ||
" \"Sid Meier's Civilization III\",\n", | ||
" \"تمدن ۳\",\n", | ||
" \"சிவிலைசேசன் மூன்று\",\n", | ||
" \"シヴィライゼーションIII\",\n", | ||
" \"文明III\",\n", | ||
" \"문명 III\" ;\n", | ||
" schema:dateModified \"2019-02-10T10:31:28+00:00\"^^xsd:dateTime ;\n", | ||
" schema:description \"2001 video game\",\n", | ||
" \"2001 videopeli\",\n", | ||
" \"Computerspiel\",\n", | ||
" \"computerspel van Firaxis Games\",\n", | ||
" \"datorspel från 2001\",\n", | ||
" \"gra wideo\",\n", | ||
" \"jeu vidéo\",\n", | ||
" \"joc video din 2001\",\n", | ||
" \"jogo eletrônico\",\n", | ||
" \"jogo eletrônico de 2001\",\n", | ||
" \"tahová strategická počítačová hra\",\n", | ||
" \"turn-based strategy video game\",\n", | ||
" \"videogioco strategico\",\n", | ||
" \"videoigra\",\n", | ||
" \"videojoc de 2001\",\n", | ||
" \"videojuego de estrategia por turnos ambientado en la construcción de imperios\",\n", | ||
" \"videojáték\",\n", | ||
" \"videospēle\",\n", | ||
" \"βιντεοπαιχνίδι του 2001\",\n", | ||
" \"видео-игра\",\n", | ||
" \"відеогра 2001 року\",\n", | ||
" \"компьютерная игра 2001 года\",\n", | ||
" \"电脑游戏\" ;\n", | ||
" schema:version 855058870 ;\n", | ||
" ns2:identifiers 10 ;\n", | ||
" ns2:sitelinks 27 ;\n", | ||
" ns2:statements 50 ;\n", | ||
" ns2:timestamp \"2019-02-10T10:31:36+00:00\"^^xsd:dateTime ;\n", | ||
" ns1:altLabel \"CIV 3\",\n", | ||
" \"CIV III\",\n", | ||
" \"Civ 3\",\n", | ||
" \"Civ3\",\n", | ||
" \"Civilization 3\",\n", | ||
" \"Sid Meier's Civilization III\",\n", | ||
" \"Sid Meier’s Civilization III\",\n", | ||
" \"Цивилизация III\",\n", | ||
" \"Цивілізація 3\" ;\n", | ||
" p:P1072 <http://www.wikidata.org/entity/statement/Q2374-3207a91f-4331-df8d-aaa3-218a543e2fde>,\n", | ||
" <http://www.wikidata.org/entity/statement/Q2374-9213212a-4e43-82e7-a80c-a12b30b5981d> ;\n", | ||
" p:P1073 <http://www.wikidata.org/entity/statement/Q2374-4600383a-4182-073c-8a6f-d7afad4719d8>,\n", | ||
" <http://www.wikidata.org/entity/statement/Q2374-d979a7b9-4e35-4760-29ca-446fcd31a3e1> ;\n", | ||
" p:P123 <http://www.wikidata.org/entity/statement/Q2374-7BA07BAB-BBBC-4A31-80BE-B4DBC51F880A>,\n", | ||
" <http://www.wikidata.org/entity/statement/q2374-74F7A23E-B22F-453E-B308-D8A802C4E9C8>,\n", | ||
" <http://www.wikidata.org/entity/statement/q2374-CA921CB5-04F5-4B39-A6E8-F9C316FDF1A7>,\n", | ||
" <http://www.wikidata.org/entity/statement/q2374-CA960D7C-21FD-4797-83CD-3CA8D70D658F> ;\n", | ||
" p:P136 <http://www.wikidata.org/entity/statement/Q2374-253BA882-87CE-468B-A3DC-B3D75F8BA608>,\n", | ||
" <http://www.wikidata.org/entity/statement/Q2374-6dfd4af1-4ae3-a5fe-e871-848a98932cf5>,\n", | ||
" <http://www.wikidata.org/entity/statement/Q2374-9F4BBFD4-D296-485C-A2D0-4F579BD32272>,\n", | ||
" <http://www.wikidata.org/entity/statement/q2374-6155E97A-DBA3-4DA6-BD16-9CCFC672695C>,\n", | ||
" <http://www.wikidata.org/entity/statement/q2374-E5C4ED7A-FCFE-4F1B-AF1A-58DFBC8DFF8C> ;\n", | ||
" p:P138 <http://www.wikidata.org/entity/statement/Q2374-42919e13-40e2-f17a-7f10-0390ae1469dc> ;\n", | ||
" p:P1476 <http://www.wikidata.org/entity/statement/Q2374-4f1f739f-4fa0-1413-f68d-a854b4e2b10a> ;\n", | ||
" p:P1712 <http://www.wikidata.org/entity/statement/Q2374-b45613f5-418a-b6ee-9733-9b11fc58c2c5> ;\n", | ||
" p:P1733 <http://www.wikidata.org/entity/statement/Q2374-b9420b21-4f85-a012-033c-dd6856ca63b5> ;\n", | ||
" p:P178 <http://www.wikidata.org/entity/statement/q2374-04C1F298-5F3D-4220-8714-B0C4587A0282>,\n", | ||
" <http://www.wikidata.org/entity/statement/q2374-66140209-9732-4C37-BFC4-BDB02A5D0803>,\n", | ||
" <http://www.wikidata.org/entity/statement/q2374-E713580E-6144-484C-9A86-42B3D7829ED3> ;\n", | ||
" p:P179 <http://www.wikidata.org/entity/statement/q2374-425EAD2F-7D43-4101-B33B-D6D7CF22AC19> ;\n", | ||
" p:P1933 <http://www.wikidata.org/entity/statement/Q2374-C2D682C5-5582-4273-83E0-E0DCBA34EBD4> ;\n", | ||
" p:P287 <http://www.wikidata.org/entity/statement/Q2374-404E4A0C-903E-4348-91A1-8B378B4B0AF2>,\n", | ||
" <http://www.wikidata.org/entity/statement/Q2374-f3cd363f-4041-4e73-645b-059dfe9a735a> ;\n", | ||
" p:P31 <http://www.wikidata.org/entity/statement/q2374-C160DF60-C748-4207-B56B-C46EA90003EE> ;\n", | ||
" p:P345 <http://www.wikidata.org/entity/statement/Q2374-77A652CB-F006-4D9C-9F27-59BF9F5882E1> ;\n", | ||
" p:P348 <http://www.wikidata.org/entity/statement/Q2374-C7AB2169-2E9A-488E-AB36-591790B96996> ;\n", | ||
" p:P3984 <http://www.wikidata.org/entity/statement/Q2374-c0c7b27b-4cb7-260a-0db7-4fb2ef8d0776> ;\n", | ||
" p:P400 <http://www.wikidata.org/entity/statement/Q2374-EB3D5380-EC39-4528-9417-9C4CBD125FDB>,\n", | ||
" <http://www.wikidata.org/entity/statement/q2374-3970c8fb-4139-6452-e5f1-c90eef0c2cdf> ;\n", | ||
" p:P404 <http://www.wikidata.org/entity/statement/q2374-1CAA47C8-482A-4C28-8078-05636FBD9CF4>,\n", | ||
" <http://www.wikidata.org/entity/statement/q2374-34E775D5-88C3-4513-858D-CE01DCE42798> ;\n", | ||
" p:P407 <http://www.wikidata.org/entity/statement/Q2374-C9E50C55-EB49-450B-A2ED-9EFD32ADF6DE> ;\n", | ||
" p:P437 <http://www.wikidata.org/entity/statement/Q2374-2DFA500D-21D9-4748-A5A7-CB57CC54E332>,\n", | ||
" <http://www.wikidata.org/entity/statement/Q2374-d12bda6a-4382-2c53-658d-64f557fd505c> ;\n", | ||
" p:P4467 <http://www.wikidata.org/entity/statement/Q2374-a2e171c0-49a4-7445-5747-2f978003c910> ;\n", | ||
" p:P479 <http://www.wikidata.org/entity/statement/Q2374-79c3887a-4b41-dc07-6e62-7dae438d97fc>,\n", | ||
" <http://www.wikidata.org/entity/statement/Q2374-f27dc9b6-40ee-b343-ba6a-41c121d216e6> ;\n", | ||
" p:P495 <http://www.wikidata.org/entity/statement/Q2374-4b6efcdd-487c-b148-b84e-64b31f9c24a7> ;\n", | ||
" p:P5247 <http://www.wikidata.org/entity/statement/Q2374-1537c200-4c11-44db-0b98-e2c1dc107e01> ;\n", | ||
" p:P527 <http://www.wikidata.org/entity/statement/Q2374-3b89c125-4a86-3982-eadb-736cb004b116>,\n", | ||
" <http://www.wikidata.org/entity/statement/Q2374-9500f0ed-4254-d808-2741-9cf4a1c29050> ;\n", | ||
" p:P57 <http://www.wikidata.org/entity/statement/Q2374-84179DF9-165B-49BD-BBF8-E7CBDFCB7AAD> ;\n", | ||
" p:P577 <http://www.wikidata.org/entity/statement/Q2374-65964a78-4118-284e-916b-7ad483d29cf1>,\n", | ||
" <http://www.wikidata.org/entity/statement/Q2374-91C6C335-A8C2-4A55-B37E-F3BBC888AAB7> ;\n", | ||
" p:P5794 <http://www.wikidata.org/entity/statement/Q2374-76cd64e6-450d-cd7f-7289-b7698f5af7c6> ;\n", | ||
" p:P6337 <http://www.wikidata.org/entity/statement/Q2374-2A621FA9-6D9E-4495-A964-FC22EABD8C12> ;\n", | ||
" p:P646 <http://www.wikidata.org/entity/statement/Q2374-A1EED53E-942A-4CD3-9145-98A04A1B5119> ;\n", | ||
" p:P856 <http://www.wikidata.org/entity/statement/Q2374-6F2AE23E-12BD-418B-BBC0-34A5B0C2DEF4> ;\n", | ||
" p:P86 <http://www.wikidata.org/entity/statement/Q2374-93CE4466-DE8C-4CC2-B3F7-9D67B8AE26B8> ;\n", | ||
" ns3:P646 <http://g.co/kg/m/01tl18> ;\n", | ||
" wdt:P1072 <http://www.wikidata.org/entity/Q27979546>,\n", | ||
" <http://www.wikidata.org/entity/Q27979555> ;\n", | ||
" wdt:P1073 <http://www.wikidata.org/entity/Q27979546>,\n", | ||
" <http://www.wikidata.org/entity/Q27979555> ;\n", | ||
" wdt:P123 <http://www.wikidata.org/entity/Q1782700>,\n", | ||
" <http://www.wikidata.org/entity/Q207922>,\n", | ||
" <http://www.wikidata.org/entity/Q3123991>,\n", | ||
" <http://www.wikidata.org/entity/Q738135> ;\n", | ||
" wdt:P136 <http://www.wikidata.org/entity/Q1610017>,\n", | ||
" <http://www.wikidata.org/entity/Q2176159>,\n", | ||
" <http://www.wikidata.org/entity/Q3139142>,\n", | ||
" <http://www.wikidata.org/entity/Q472055>,\n", | ||
" <http://www.wikidata.org/entity/Q603555> ;\n", | ||
" wdt:P138 <http://www.wikidata.org/entity/Q92616> ;\n", | ||
" wdt:P1476 \"Sid Meier's Civilization III\" ;\n", | ||
" wdt:P1712 \"game/pc/sid-meiers-civilization-iii\" ;\n", | ||
" wdt:P1733 \"3910\" ;\n", | ||
" wdt:P178 <http://www.wikidata.org/entity/Q3123991>,\n", | ||
" <http://www.wikidata.org/entity/Q738135>,\n", | ||
" <http://www.wikidata.org/entity/Q959759> ;\n", | ||
" wdt:P179 <http://www.wikidata.org/entity/Q1868663> ;\n", | ||
" wdt:P1933 \"sid-meiers-civilization-iii\" ;\n", | ||
" wdt:P287 <http://www.wikidata.org/entity/Q5586418>,\n", | ||
" <http://www.wikidata.org/entity/Q9284930> ;\n", | ||
" wdt:P31 <http://www.wikidata.org/entity/Q7889> ;\n", | ||
" wdt:P345 \"tt0299682\" ;\n", | ||
" wdt:P348 \"1.29f\" ;\n", | ||
" wdt:P3984 \"civ3\" ;\n", | ||
" wdt:P400 <http://www.wikidata.org/entity/Q1406>,\n", | ||
" <http://www.wikidata.org/entity/Q14116> ;\n", | ||
" wdt:P404 <http://www.wikidata.org/entity/Q208850>,\n", | ||
" <http://www.wikidata.org/entity/Q6895044> ;\n", | ||
" wdt:P407 <http://www.wikidata.org/entity/Q1860> ;\n", | ||
" wdt:P437 <http://www.wikidata.org/entity/Q337535>,\n", | ||
" <http://www.wikidata.org/entity/Q7982> ;\n", | ||
" wdt:P4467 \"Sid Meier's Civilization III\" ;\n", | ||
" wdt:P479 <http://www.wikidata.org/entity/Q250>,\n", | ||
" <http://www.wikidata.org/entity/Q7987> ;\n", | ||
" wdt:P495 <http://www.wikidata.org/entity/Q30> ;\n", | ||
" wdt:P5247 \"3030-1594\" ;\n", | ||
" wdt:P527 <http://www.wikidata.org/entity/Q2633010>,\n", | ||
" <http://www.wikidata.org/entity/Q548085> ;\n", | ||
" wdt:P57 <http://www.wikidata.org/entity/Q92616> ;\n", | ||
" wdt:P577 \"2001-10-30T00:00:00+00:00\"^^xsd:dateTime,\n", | ||
" \"2006-10-25T00:00:00+00:00\"^^xsd:dateTime ;\n", | ||
" wdt:P5794 \"sid-meier-s-civilization-iii\" ;\n", | ||
" wdt:P6337 \"Civilization_III\" ;\n", | ||
" wdt:P646 \"/m/01tl18\" ;\n", | ||
" wdt:P856 <http://www.civ3.com/> ;\n", | ||
" wdt:P86 <http://www.wikidata.org/entity/Q7357913> .\n", | ||
"\n", | ||
"\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"permagraph = None\n", | ||
"\n", | ||
"def persistent_slurper(rdf: str) -> SlurpyGraph:\n", | ||
" global permagraph\n", | ||
" permagraph = SlurpyGraph(rdf)\n", | ||
" return permagraph\n", | ||
"\n", | ||
"shex_evaluator.SlurpyGraph = persistent_slurper\n", | ||
"\n", | ||
"sparql_query = \"\"\"\n", | ||
"PREFIX wdt: <http://www.wikidata.org/prop/direct/>\n", | ||
"PREFIX wd: <http://www.wikidata.org/entity/>\n", | ||
"\n", | ||
"SELECT ?item WHERE { ?item (wdt:P31/wdt:P279*) wd:Q7397.} LIMIT 100\n", | ||
"\"\"\"\n", | ||
"\n", | ||
"\n", | ||
"x = [\"-ss\",\n", | ||
" \"-sq\", sparql_query,\n", | ||
" \"http://query.wikidata.org/sparql\",\n", | ||
" \"https://raw.githubusercontent.com/shexSpec/schemas/master/Wikidata/DigitalPreservation/SWIB/software.shex\",\n", | ||
" \"--stopafter\", \"1\",\n", | ||
" \"-ps\"]\n", | ||
"shexeval(x)\n", | ||
"print()\n", | ||
"print(\"RESULT Graph:\")\n", | ||
"print(permagraph.serialize(format=\"turtle\").decode())\n" | ||
] | ||
}, | ||
{ | ||
"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.2" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> | ||
PREFIX dbgraph: <https://w3id.org/data2services/graph/biolink/drugbank> | ||
PREFIX drugbank: <http://identifiers.org/drugbank:> | ||
|
||
SELECT ?g ?s ?p ?o {graph dbgraph: {?s rdf:type ?o}} |