AD Research Wiki:

This page provides various explanations that are useful when working the Wikidata and its peculiar schema.

A good SPARQL tutorial specifically for Wikidata and its particular schema can be found here: https://www.wikidata.org/wiki/Wikidata:SPARQL_tutorial

Ranking of entities

For efficiency reasons, both numbers below should be precomputed and than the precomputed numbers added to the .nt file. For example, with new predicates <num-sitelinks> or <num-triples>.

A sitelink is a Wikipedia page that is about a certain Wikidata entity. The number of sitelinks is a good proxy for the popularity of an entity. For example, to get all German cities and their population ordered by the number of sitelinks, one can write the following. If you run the query on WDQS, note how in the top results there is strong correlation to population size.

A practical challenge with this approach is that the schema:about triples are not part of the truthy excerpt of the Wikidata dataset. That is, one has to download the whole dataset, which is many times larger than the truthy version. It's not an actual problem, just more work.

SELECT ?label ?population (COUNT(?label) AS ?popularity) WHERE {
  ?city wdt:P31 wd:Q515 .          # ?city "instance of" "city" 
  ?city wdt:P17 wd:Q183 .          # ?city "country" "Germany" 
  ?city wdt:P1082 ?population .    # ?city "population" ?population
  ?city rdfs:label ?label .  
  ?sitelink schema:about ?city
  FILTER(LANG(?label) = "en")
}
GROUP BY ?label ?population
ORDER BY DESC(?popularity)
LIMIT 100

Ranking by number of triples

A simpler way to rank entities and which also works with the truthy data, is to order entities by the number of triples in which they are used as subject. This works well for some queries (like the following), but not so well for others (because there are quite a lot of relatively "unimportant" entities, but which have a lot of "phony" triples; TODO: give an example).

SELECT ?label ?population (COUNT(?label) AS ?popularity) WHERE {
  ?city wdt:P31 wd:Q515 .
  ?city wdt:P17 wd:Q183 .
  ?city wdt:P1082 ?population .
  ?city rdfs:label ?label .
  ?city ?p ?o .  
  FILTER(LANG(?label) = "en")
}
GROUP BY ?label ?population
ORDER BY DESC(?popularity)
LIMIT 100

Obtaining names

Names for entities

The Wikidata Query Service (WDQS) has an automatic mechanism for obtaining the single best name for entity variables in the query. It does not work for predicate names, however. The mechanism is invoked by adding this line to the SPARQL query:

SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }

To obtain a single name of an entity "manually" (= without the mechanism above), one can add the following two lines to a query (Q90 is Paris). Note that without the language filter, one gets one name for each languages, for which a name has been specified for that entity in Wikidata (for popular entities, these are usually very many):

wd:Q90 rdfs:label ?label .
FILTER(LANG(?label) = "en") 

Names for predicates

Obtaining the name for a predicate is a bit more complicated. Here is how to obtain all predicates and their English name for an entity (again Q90), sorted by the number of triples with that predicate and that entity as subject:

SELECT ?p (COUNT(?o) as ?count) (SAMPLE(?label) as ?pname) WHERE {
  wd:Q90 ?p ?o .
  ?x wikibase:claim ?p .
  ?x rdfs:label ?label .
  FILTER(LANG(?label) = "en")
}
GROUP BY ?p
ORDER BY DESC(?count)
LIMIT 100

There are a number of other wikibase: predicates that relate the different types of predicates to teach other

wd:P47 wikibase:claim              p:P47
wd:P47 wikibase:directClaim        wdt:P47
wd:P47 wikibase:novalue            wdno:P47
wd:P47 wikibase:qualifier          pq:P47
wd:P47 wikibase:qualifierValue     pqv:P47
wd:P47 wikibase:reference          pr:P47
wd:P47 wikibase:referenceValue     prv:P47
wd:P47 wikibase:statementProperty  ps:P47
wd:P47 wikibase:statementValue     psv:P47

Aliases of an entity

In the .nt file, Aliases are expressed as follows

<http://www.wikidata.org/entity/Q567> <http://www.w3.org/2004/02/skos/core#altLabel> "Angela Dorothea Merkel"@de

There are also some other predicates, which provide alternative names, see this question on Stackoverflow. To get all aliases of an entity according to that list write something like this:

SELECT ?alias WHERE {
  wd:Q567 (wdt:P735|wdt:P734|wdt:P1449|skos:altLabel)* ?alias .
  FILTER(lang(?alias) = "de" || lang(?alias) = "en")
}

The * after the (...) means that arbitrary sequences these relations are considered, including not using any relations. In the example above, this gives results for wd:Q567 skos:altLabel (given name + labels), wd:P734 skos:altLabel (family name + labels), wd:Q1449 skos:altLabel (nicknames + labels). If one uses rdfs:label instead of skos:altLabel one only gets the one label (per language) for each entity.

Note that skos stands for simple knowledge organisation system. The simple is obivously a joke.

Statements and value-qualifier tuples for an entity

For example, all (population, point in time) pairs for Paris (Q90):

PREFIX p: <http://www.wikidata.org/prop/>
PREFIX ps: <http://www.wikidata.org/prop/statement/>
PREFIX pq: <http://www.wikidata.org/prop/qualifier/>
SELECT ?value ?time WHERE {
  wd:Q90 p:P1082 ?statement .  # P1082 = population
  ?statement ps:P1082 ?value . # P1082 = population
  ?statement pq:P585 ?time     # P585 = point in time
}
ORDER BY DESC(?time)

AD Research Wiki: HowTos/WikidataCheatSheet (last edited 2018-08-07 16:55:56 by Hannah Bast)