Querying and Exploring Wikidata

Nikolai Janakiev @njanakiev

Wikidata is a Knowledge Graph

Knowledge Graph

Semantic Web and Linked Data

  • Linking data sets on the Web
  • Extension of World Wide Web and a W3C standard
  • 1,229 datasets with 16,125 links (as of June 2018)

Timeline

  • 2001 Semantic Web (Tim Berners-Lee)
  • 2006 Linked Data (Tim Berners-Lee)
  • 2007 DBPedia
  • 2007 Freebase (part of Google's Knowledge Graph)

Wikipedia Wikidata Link

Wikidata Data Model

Subject Predicate Object

Querying Wikidata with SPARQL

Wikidata Query

All Goats in Wikidata

SELECT ?item ?itemLabel WHERE {
  ?item wdt:P31 wd:Q2934.
  SERVICE wikibase:label { 
    bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". 
  }
}

Query link

Example Queries

Query Wikidata API with Python

In [1]:
import requests

url = 'https://query.wikidata.org/sparql'
query = """
SELECT ?item ?itemLabel WHERE {
  ?item wdt:P31 wd:Q2934.
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
"""

response = requests.get(url, params = {'format': 'json', 'query': query})
data = response.json()

data['results']['bindings'][:5]
Out[1]:
[{'item': {'type': 'uri', 'value': 'http://www.wikidata.org/entity/Q151345'},
  'itemLabel': {'type': 'literal',
   'value': 'Billygoat Hennes',
   'xml:lang': 'en'}},
 {'item': {'type': 'uri', 'value': 'http://www.wikidata.org/entity/Q3569037'},
  'itemLabel': {'type': 'literal',
   'value': 'William Windsor',
   'xml:lang': 'en'}},
 {'item': {'type': 'uri', 'value': 'http://www.wikidata.org/entity/Q5738485'},
  'itemLabel': {'type': 'literal',
   'value': 'Goat of the Spanish Legion',
   'xml:lang': 'en'}},
 {'item': {'type': 'uri', 'value': 'http://www.wikidata.org/entity/Q24287064'},
  'itemLabel': {'type': 'literal', 'value': 'Taffy', 'xml:lang': 'en'}},
 {'item': {'type': 'uri', 'value': 'http://www.wikidata.org/entity/Q41239734'},
  'itemLabel': {'type': 'literal',
   'value': 'Lance Corporal Shenkin III',
   'xml:lang': 'en'}}]

Where Do Mayors Come From?