Sharedsparql-vanGogh.ipynbOpen in CoCalc
Author: Harald Schilly
Views : 4

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

%endpoint %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:
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.

%format json %display table PREFIX rdfs: <> PREFIX foaf: <> 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 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

%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 1853-03-30typed-literal 1853-3-30typed-literal 1890-07-29typed-literal 1890-7-29typed-literal
Total: 295, Shown: 20

Find a painting

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

%format json %display table PREFIX rdfs: <> PREFIX foaf: <> 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

%display raw PREFIX rdfs: <> PREFIX foaf: <> 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": "é_Terrace_at_Night" } }, { "name": { "type": "literal", "value": "Dodge MacKnight", "xml:lang": "en" }, "picture": { "type": "uri", "value": "" } }, { "name": { "type": "literal", "value": "Midnight in Paris", "xml:lang": "en" }, "picture": { "type": "uri", "value": "" } }, { "name": { "type": "literal", "value": "Night in paintings (Western art)", "xml:lang": "en" }, "picture": { "type": "uri", "value": "" } }, { "name": { "type": "literal", "value": "White House at Night", "xml:lang": "en" }, "picture": { "type": "uri", "value": "" } }, { "name": { "type": "literal", "value": "The Night Café", "xml:lang": "en" }, "picture": { "type": "uri", "value": "é" } }, { "name": { "type": "literal", "value": "Starry Night Over the Rhone", "xml:lang": "en" }, "picture": { "type": "uri", "value": "" } }, { "name": { "type": "literal", "value": "The Starry Night", "xml:lang": "en" }, "picture": { "type": "uri", "value": "" } } ], "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

%format n3 %display table %lang en %show all PREFIX rdfs: <> PREFIX foaf: <> 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).

%format n3 %display diagram svg %lang en PREFIX rdfs: <> PREFIX foaf: <> 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)

%format n3 %display diagram svg PREFIX wd: <> 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