Skip to content

Instantly share code, notes, and snippets.

@criscod
Last active October 20, 2017 13:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save criscod/f095e3444b14a2e16ec8ea2f35ce2d34 to your computer and use it in GitHub Desktop.
Save criscod/f095e3444b14a2e16ec8ea2f35ce2d34 to your computer and use it in GitHub Desktop.
DINACON - Querying Wikidata

Intro

You can take a look at Wikidata and check how it describes Bern: https://www.wikidata.org/wiki/Q70

You will find a lot of documentation about Wikidata: https://www.wikidata.org/wiki/Help:Contents

Here are some quick facts and links (slides): https://docs.google.com/presentation/d/1zFZuhdEp6KPUlT_fzb0kJFSyXkRksxw5dMN0iVNZwA0/edit?usp=sharing

We recently organised a Wikidata workshop in Zurich. You will find lots of useful materials in our event page: https://www.wikidata.org/wiki/Wikidata:Events/Wikidata_Zurich

You need an index of all properties in Wikidata? Here it is: https://www.wikidata.org/wiki/Wikidata:List_of_properties/all

SPARQL queries

Wikidata has an official SPARQL endpoint where you can execute your queries and obtain results in various formats.

Try it yourself here: https://query.wikidata.org/

Documentation

Here you will find many resources to learn about SPARQL and the Wikidata Query Service: https://www.wikidata.org/wiki/Wikidata:SPARQL_query_service/Wikidata_Query_Help

https://www.mediawiki.org/wiki/Wikidata_query_service/User_Manual

You can find tons of queries written by members of the Wikidata community here: https://www.wikidata.org/wiki/Wikidata:SPARQL_query_service/queries/examples

Query logs can be found as part of the USEWOD data set: http://usewod.org/

Watch out! There are some queries that (given their nature) are technically problematic: https://www.mediawiki.org/wiki/Wikidata_query_service/Problematic_queries

You can see all the ways to access the data in Wikidata at: https://www.wikidata.org/wiki/Wikidata:Data_access

Example Queries to be Showcased at DINACON

Queries to understand what is in Wikidata:

What is the hierarchy around Person in Wikidata?

SELECT (COUNT(DISTINCT ?class) AS ?classes) WHERE { ?class wdt:P279 wd:Q5. }

Note: To obtain the complete count or set of classes, properties and instances per classes in Wikidata, we recommend you access the Wikidata Browser SQUID: https://tools.wmflabs.org/sqid/

e.g. classes: https://tools.wmflabs.org/sqid/#/browse?type=classes

Or you use the Wikidata Toolkit (in Java) to get such descriptive statistics from a Wikidata dump: https://www.mediawiki.org/wiki/Wikidata_Toolkit

How many properties are used to describe Swiss Cantons in Wikidata?

SELECT (COUNT(DISTINCT ?property) AS ?properties) WHERE { ?item wdt:P31 wd:Q23058 .      ?item ?property ?value}

Queries about Switzerland:

How many Swiss cities does Wikidata contain per Canton?

SELECT ?canton (COUNT(DISTINCT ?item) AS ?count) WHERE { ?item wdt:P31 wd:Q515 . ?item wdt:P131 ?canton . ?canton wdt:P31 wd:Q23058 . } GROUP BY ?canton

How is Bern classified or typed in Wikidata?

SELECT ?typeLabel ?type WHERE { wd:Q70 wdt:P31 ?type . SERVICE wikibase:label { bd:serviceParam wikibase:language "en" } }

Get all the data about Bern

SELECT ?prop ?value WHERE { wd:Q70 ?prop ?value . SERVICE wikibase:label { bd:serviceParam wikibase:language "en" } }

Mountains in Switzerland by height range displayed in a map distinguishing colours

SELECT ?item ?itemLabel ?coord ?height ?layer ?image WHERE { ?item wdt:P31 wd:Q8502. # a mountain ?item wdt:P625 ?coord. ?item wdt:P17 wd:Q39. # in Switzerland ?item wdt:P2044 ?height . BIND( IF(?height < 1000, "<1000 metres", IF(?height < 2000, "1000 - 2000 metres", IF(?height < 3000, "2000 - 3000 metres", IF(?height < 4000, "3000 - 4000 metres", "> 4000 metres")))) AS ?layer). OPTIONAL {?item wdt:P18 ?image.} SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } }

Queries for computer science fans! :)

Universities of main programming language authors

SELECT ?lang ?langLabel ?human ?humanLabel ?educatedat ?educatedatLabel ?coords { ?lang wdt:P31/wdt:P279* wd:Q9143 . ?human wdt:P31 wd:Q5 . { ?lang wdt:P287 ?human } UNION { ?lang wdt:P170 ?human } UNION { ?lang wdt:P943 ?human } UNION { ?lang wdt:P178 ?human } . ?human wdt:P69 ?educatedat . ?educatedat wdt:P625 ?coords . SERVICE wikibase:label { bd:serviceParam wikibase:language "en,fr" } }LIMIT 100

source: https://www.wikidata.org/wiki/Wikidata:SPARQL_query_service/queries/examples#Computer_Science_and_Technology

Timeline of space probes #Timeline of space probes #defaultView:Timeline SELECT ?item ?itemLabel ?launchdate (SAMPLE(?image) AS ?image) WHERE { ?item wdt:P31 wd:Q26529 . ?item wdt:P619 ?launchdate . SERVICE wikibase:label { bd:serviceParam wikibase:language "en" } OPTIONAL { ?item wdt:P18 ?image } } GROUP BY ?item ?itemLabel ?launchdate

source: https://www.wikidata.org/wiki/Wikidata:SPARQL_query_service/queries/examples#Timeline_of_space_probes

Queries to discover how you can help Wikidata:

Train stations in Bern that do not have an image in Wikimedia Commons

SELECT DISTINCT ?trainstation ?trainstationLabel WHERE { ?trainstation wdt:P131 wd:Q70 . ?trainstation wdt:P31 wd:Q55488 . OPTIONAL{ ?trainstation wdt:P18 ?image .} FILTER(!BOUND(?image)) SERVICE wikibase:label { bd:serviceParam wikibase:language "de" } }

Take a picture and share it in Wikimedia Commons!

You can also look for people without date of birth etc.

Check completeness with ReCoin in e.g.: https://www.wikidata.org/wiki/Q937

Write your input here https://etherpad.wikimedia.org/p/dinacon_wikidata


Cristina Sarasua University of Zurich, Department of Informatics, DDIS Binzmühlestrasse 14 CH-8050 Zürich

Web site: http://www.ifi.uzh.ch/en/ddis/people.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment