Wikidata#

Wikidata offers access to many of the facts that are listed on Wikipedia as RDF triplets. In other words, Wikidata presents the contents of Wikipedia as structured data. WikiData’s SPARQL Endpoint can be found at https://query.wikidata.org/sparql

Wikidata does not make use of human-understandable labels. Objects, subjects and predicated are all represented as numeric codes, following either the letter ‘P’ or ‘W’. Named entities such as people, places, organizations and works of art have mostly been assigned a code starting with the letter ‘Q’. The properties all start with a ‘P’.

You can see an example below.

SELECT ?work  

WHERE {  
?work wdt:P170 wd:Q151803 . 
} 
Error: no endpoint defined

The property wdt:P170 was created to describe a “maker of this creative work or other object”. wd:Q151803 is the identifier that was assigned to Piet Mondriaan. The query shown in this example requests all the works of art created by the painter Piet Mondriaan. As you can see, the subject is represented as a variable named ?work.

When you run the code in Wikidata’s SPARQL endpoint, the result will initially consist of numbers only, so this may look slightly disappointing. The identifiers, consisting of random codes, are not highly informative in themselves. Wikidata offers a useful service, however, which you can use to generate human-readable labels for the codes assigned by Wikidata.

First, you need to add the following line to the query.

SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . } 

After wikibase:language, you can specify the language of the label. Next to “en”, which stands for English, you can also request labels in Dutch (“nl”), German (‘de”’) or French (“fr”).

Once you have added this specific SERVICE clause, Wikidata creates a second version of all the variables you specify in the WHERE section of the query. The name of this second version of the query will be the name you assigned yourself, with the suffix ‘Label’ as an addition. If you created a variable named ?place, for example, the Wikibase Label service will create a ?placeLabel variable. This variable will contain a textual label associated with the Wikidata identifier.

SELECT ?work  ?workLabel 

WHERE  
{   
?work wdt:P170 wd:Q151803 .  
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }  
} 

Wiki query services#

Since 2015, Wikidata has provided services that can help you build SPARQL queries. One service is called the query builder. It can be accessed via https://query.wikidata.org/querybuilder/. This tool offers a visual interface that can be used to build simple SPARQL queries. Although this service is limited in making SPARQL queries, it can be helpful if you do not have a lot of experience yet with building SPARQL queries.

Another service offered by WikiData is the query service accessible via https://query.wikidata.org/. The Wikidata Query Service offers opportunities for building SPARQL queries and downloading the requested data in various file formats (including JSON and CSV). It is also possible to view the data in different data visualizations, like a table, a bar chart or a timeline.

Exercises#

Exercise 3.1.#

Use the WikiData Query Builder to find artworks made by Leonardo da Vinci created after 1495.

Tip: Type in ‘Creator’ under property, and ‘Leonardo da Vinci’ under ‘value’. Next, click on “Add condition”. In a new condition type in “inception” under property and ‘1495’ under ‘value’. Next, run the query.

Exercise 3.2.#

If you cannot manage to build the query using the WikiData Query Builder, you can open the Query Service and copy and paste the query below in the central text field.

SELECT ?work ?date ?workLabel 

WHERE  
{   
?work wdt:P170 wd:Q762 .  
?work wdt:P571 ?date 
FILTER(?date >= "+1496-00-00T00:00:00Z"^^xsd:dateTime)
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }  
} 

Next, try to artist in this query to Michelangelo, who is identified on Wikidata as 'wd:Q5592', and also try to change the year into 1505.

Finally, find the “Download” button at the top right of the results screen and download the results of the query as a CSV file.

This new query should have the following form.

SELECT DISTINCT ?work ?date ?workLabel 

WHERE  
{   
?work wdt:P170 wd:Q5592 .  
?work wdt:P571 ?date 
FILTER(?date >= "+1505-00-00T00:00:00Z"^^xsd:dateTime)
LIMIT 1000
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }  
}