Open in CoCalc

SPARQL about van Gogh -- on CoCalc!

We are going to use some the DBPedia SPARQL endpoint to do some queries related to Vincent van Gogh.

First we define the endpoint and the preferred language for labels

In [1]:
%endpoint http://dbpedia.org/sparql %lang en # This is optional, it would increase the log level. # The default logfile (unless changed upon kernel installation) is [TMPDIR]/sparqlkernel.log, # where [TMPDIR] is the platform default temporal directory %log debug
Endpoint set to: http://dbpedia.org/sparql
Label preferred languages: ['en']
Logging set to DEBUG

Now let's find out the entity URI for van Gogh in DBPedia. We search entities that are persons and whose label contains van Gogh (case insensitive).

Note that the DBPedia endpoint has a set of predefined namespace prefixes that we can use without the need to define them in the query, such as rdfs: or foaf: so we could remove them from the query without problem.

In [2]:
%format json %display table PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT ?person ?name WHERE { ?person a foaf:Person . ?person rdfs:label ?name FILTER regex(?name,"van gogh","i") FILTER langMatches(lang(?name),"en") } LIMIT 20

It is clear from the result that we want the http://dbpedia.org/resource/Vincent_van_Gogh DBPedia entity (we can click on the link for confirmation, and it will lead us to a DBPedia web page describing the resource).

Fact finding

Now that we found his URI, let's search for places, dates and people related with van Gogh.

In this query we also set the table to show the data type for each result

In [4]:
%format json %display table withtypes # We might have more than one triple pointing to the same object, so we group by object # and take one arbitrary predicate SELECT (SAMPLE(?pred) AS ?prop) ?value WHERE { # Places and dates { dbr:Vincent_van_Gogh ?pred ?value . { ?pred rdfs:range xsd:date } UNION { ?pred rdfs:range dbo:Place } } # People van Gogh relates to UNION { dbr:Vincent_van_Gogh ?pred ?value . ?value a foaf:Person } # People related to van Gogh UNION { ?value ?pred dbr:Vincent_van_Gogh . ?value a foaf:Person } } GROUP BY ?value ORDER BY ?prop
Return format: JSON
Display: table
Show Types: on
proptype valuetype
http://dbpedia.org/ontology/birthDateuri 1853-03-30typed-literal
http://dbpedia.org/ontology/birthDateuri 1853-3-30typed-literal
http://dbpedia.org/ontology/birthPlaceuri http://dbpedia.org/resource/Zunderturi
http://dbpedia.org/ontology/deathDateuri 1890-07-29typed-literal
http://dbpedia.org/ontology/deathDateuri 1890-7-29typed-literal
http://dbpedia.org/ontology/deathPlaceuri http://dbpedia.org/resource/Auvers-sur-Oiseuri
http://dbpedia.org/ontology/deathPlaceuri http://dbpedia.org/resource/Auberge_Ravouxuri
http://dbpedia.org/ontology/wikiPageWikiLinkuri http://dbpedia.org/resource/Rob_Liefelduri
http://dbpedia.org/ontology/wikiPageWikiLinkuri http://dbpedia.org/resource/Harold_Hechturi
http://dbpedia.org/ontology/wikiPageWikiLinkuri http://dbpedia.org/resource/Stephen_Emmeruri
http://dbpedia.org/ontology/wikiPageWikiLinkuri http://dbpedia.org/resource/Luke_Fildesuri
http://dbpedia.org/ontology/wikiPageWikiLinkuri http://dbpedia.org/resource/Peter_Lindberghuri
http://dbpedia.org/ontology/wikiPageWikiLinkuri http://dbpedia.org/resource/Einojuhani_Rautavaarauri
http://dbpedia.org/ontology/wikiPageWikiLinkuri http://dbpedia.org/resource/Claude_Moneturi
http://dbpedia.org/ontology/wikiPageWikiLinkuri http://dbpedia.org/resource/Lazarus_of_Bethanyuri
http://dbpedia.org/ontology/wikiPageWikiLinkuri http://dbpedia.org/resource/Chen_Ke_(artist)uri
http://dbpedia.org/ontology/wikiPageWikiLinkuri http://dbpedia.org/resource/William_Hood_Dunwoodyuri
http://dbpedia.org/ontology/wikiPageWikiLinkuri http://dbpedia.org/resource/Casimir_Lefaucheuxuri
http://dbpedia.org/ontology/wikiPageWikiLinkuri http://dbpedia.org/resource/Abraham_Diepraamuri
http://dbpedia.org/ontology/wikiPageWikiLinkuri http://dbpedia.org/resource/Trento_Longarettiuri
Total: 295, Shown: 20

Find a painting

Now we search for a van Gogh painting whose title contains "night"

In [4]:
%format json %display table PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT DISTINCT (?picture) ?name WHERE { ?picture ?p1 dbr:Vincent_van_Gogh . ?picture rdfs:label ?name . FILTER regex(?name,"night", "i") FILTER langMatches(lang(?name),"en") }

Just to show another possibility, we repeat the same query but indicating raw display format

In [5]:
%display raw PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT DISTINCT (?picture) ?name WHERE { ?picture ?p1 dbr:Vincent_van_Gogh . ?picture rdfs:label ?name . FILTER regex(?name,"night", "i") FILTER langMatches(lang(?name),"en") }
Display: raw
{ "head": { "link": [], "vars": [ "picture", "name" ] }, "results": { "bindings": [ { "name": { "type": "literal", "value": "Café Terrace at Night", "xml:lang": "en" }, "picture": { "type": "uri", "value": "http://dbpedia.org/resource/Café_Terrace_at_Night" } }, { "name": { "type": "literal", "value": "Dodge MacKnight", "xml:lang": "en" }, "picture": { "type": "uri", "value": "http://dbpedia.org/resource/Dodge_MacKnight" } }, { "name": { "type": "literal", "value": "Midnight in Paris", "xml:lang": "en" }, "picture": { "type": "uri", "value": "http://dbpedia.org/resource/Midnight_in_Paris" } }, { "name": { "type": "literal", "value": "Night in paintings (Western art)", "xml:lang": "en" }, "picture": { "type": "uri", "value": "http://dbpedia.org/resource/Night_in_paintings_(Western_art)" } }, { "name": { "type": "literal", "value": "White House at Night", "xml:lang": "en" }, "picture": { "type": "uri", "value": "http://dbpedia.org/resource/White_House_at_Night" } }, { "name": { "type": "literal", "value": "The Night Café", "xml:lang": "en" }, "picture": { "type": "uri", "value": "http://dbpedia.org/resource/The_Night_Café" } }, { "name": { "type": "literal", "value": "Starry Night Over the Rhone", "xml:lang": "en" }, "picture": { "type": "uri", "value": "http://dbpedia.org/resource/Starry_Night_Over_the_Rhone" } }, { "name": { "type": "literal", "value": "The Starry Night", "xml:lang": "en" }, "picture": { "type": "uri", "value": "http://dbpedia.org/resource/The_Starry_Night" } } ], "distinct": false, "ordered": true } }

Describe a painting

Now we find out all that DBPedia knows about one of those paintings, Starry Night Over the Rhone

In [6]:
%format n3 %display table %lang en %show all PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX foaf: <http://xmlns.com/foaf/0.1/> CONSTRUCT { ?s1 ?p1 dbr:Starry_Night_Over_the_Rhone . dbr:Starry_Night_Over_the_Rhone ?p2 ?o1 . } WHERE { { ?s1 ?p1 dbr:Starry_Night_Over_the_Rhone } UNION { dbr:Starry_Night_Over_the_Rhone ?p2 ?o1 } FILTER ( ?p1 != owl:sameAs && ?p2 != owl:sameAs) }
Return format: N3
Display: table
Label preferred languages: ['en']
Result maximum size: unlimited
WARNING: Some output was deleted.

Similar data could be obtained by asking the endpoint to DESCRIBE the resource. Let's do this, but for a twist let's ask the kernel to draw a graph with the results (for this to work, Graphviz should be installed in the system).

We generate a PNG here. Note that svg is a much better format than png, if the browser supports it (better quality, and hyperlinked nodes).

In [11]:
%format n3 %display diagram svg %lang en PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX foaf: <http://xmlns.com/foaf/0.1/> DESCRIBE dbr:Starry_Night_Over_the_Rhone
Return format: N3
Display: svg
Label preferred languages: ['en']

Locate paintings

Finally, to print out a slightly more complex graph, we will construct one with all of van Gogh's paintings, together with their location and the country they are in.

This one we render as SVG; this has the said advantage of better quality (being a vector format). Plus nodes and edges can contain hyperlinks, so when they are URIs they point to the full URL (note that, unless the withliterals option is used, all represented nodes will be URIs)

In [12]:
%format n3 %display diagram svg PREFIX wd: <http://www.wikidata.org/entity/> CONSTRUCT { ?painting dbp:museum ?museum . ?painting dct:subject ?subject . ?museum dbo:location ?location . ?location dbo:country ?country . } WHERE { ?painting dbp:artist dbr:Vincent_van_Gogh . ?painting a wd:Q386724 . ?painting a dbo:Artwork . ?painting dbp:museum ?museum . ?museum dbo:location ?location . ?location dbo:country ?country . }
Return format: N3
Display: svg
Version: 1.1 (2016-08-10)
Author: Paulo Villegas
In [ ]: