## page was renamed from WikidataCheatSheet ## page was renamed from Datasets/WikidataExplanations 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 Standard prefixes can be found [[https://www.mediawiki.org/wiki/Wikibase/Indexing/RDF_Dump_Format#Prefixes_used|here]] <> = 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 '''' or ''''. == Ranking by number of sitelinks == 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 [[https://goo.gl/QhjjuL|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 [[https://query.wikidata.org|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 ([[https://www.wikidata.org/wiki/Q90|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: {{{ PREFIX wd: PREFIX wikibase: PREFIX rdfs: 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 {{{ "Angela Dorothea Merkel"@de }}} There are also some other predicates, which provide alternative names, see [[https://stackoverflow.com/questions/40593452/how-to-retrieve-aliases-from-wikidata|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:1449|skos:altLabel|rdfs:label) ?alias . FILTER(lang(?alias) = "de" || lang(?alias) = "en") } }}} The predicates ''wdt:P735'' and ''wdt::P734'' yield the ''given name'' and ''family name'', respectively. Note that the objects of the predicates are entities, not literals. The ? after the (...) means that sequences of length 0 or 1 of these predicates are considered (that is using one of the predicates or not). With * one would get all sequences of length >= 0. For the query above, this would give the same result. With + one would get all sequences of length >= 1. For the query above, this would give fewer results, because we would not get the names of only ''wd:Q567''. The predicates ''wdt:1449'' and ''skos:altLabel'' and ''rdfs:label'' yield the ''nicknames'' and ''alternative names'' and ''unique (per language)'' label, respectively. The objects for these predicates are literals. Note that the literals from ''skos:altLabel'' are in general not a superset of the literals from ''rdfs:label''. Note that ''skos'' stands for ''simple knowledge organisation system''. The ''simple'' is obviously a joke, given that the full URI is http://www.w3.org/2004/02/skos/core#altLabel . = Statements and value-qualifier tuples for an entity = For example, all (population, point in time) pairs for Paris (Q90): {{{ PREFIX p: PREFIX ps: PREFIX pq: 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) }}} = Problems with the Wikidata schema = = Query: City, country, population = 1. Many cities in Wikidata are not "instance of" (wdt:P31) "city" (wd:Q515), but "big city" (wd:Q4905452), which in turn is a "subclass of" (wdt:P279) city. Many of the example queries in Wikidata therefore use the following triple to specify that a variable is a city: {{{ ?city wdt:P31/wdt:P279* wd:Q515 }}} 2. Unlike Freebase, Wikidata has a property "country" (wdt:P17) to relate items (in particular: cities) to their countries. There is also "contains administrative territorial entity" (wdt:P150), but it is much less complete: the first query below has a result with ''10,560,170'' rows, the second query has a result with only ''21,344'' rows. {{{ PREFIX wdt: SELECT ?city ?country WHERE { ?city wdt:P17 ?country . } }}} {{{ PREFIX wdt: PREFIX wd: SELECT ?city ?country { ?city wdt:P31/wdt:P279* wd:Q515 . ?country wdt:P31 wd:Q6256 . ?city wdt:P17 ?country } }}}