CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for Testing 18.04.

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

| Download
Project: Testing 18.04
Views: 568
Kernel: SPARQL

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 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

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: <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

%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

Find a painting

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

%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

%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") }
{ "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

%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) }
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: <http://www.w3.org/2000/01/rdf-schema#> PREFIX foaf: <http://xmlns.com/foaf/0.1/> DESCRIBE dbr:Starry_Night_Over_the_Rhone
Image in a Jupyter notebook

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: <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 . }
Image in a Jupyter notebook
Version: 1.1 (2016-08-10)
Author: Paulo Villegas