User:Beat Estermann/WD Hackdays 2019/Session 1
Refresher SPARQL Queries edit
Simple Query edit
- Wikidata Query Service: From the "Cats" example to the list of all museums... taking into account all the sub-classes of "museum".
- Usage example – Property Dashboard Template: Wikidata:WikiProject Heritage institutions/Statistics
Extracting a Specific Dataset edit
- Get all the museums in Switzerland (P17 - country; Q39 - Switzerland).
- For each entry, include if possible the label (L), the description (D), the image (P18), the coordinate location (P625), the founding or opening date (P571), the name of the director (P1037).
- Solution: https://w.wiki/5uR - play around with the different output types!
- Include not only museums, but also archives and libraries.
- Solution: https://w.wiki/5uT
Exploring the Wikidata Ontology in a Given Domain edit
- Use the Wikidata Ontology Explorer (examples: enter Q7075 for the class "library"; P4646 for the property "representation of")
- Get all the subclasses of heritage institutions in several languages:
SELECT ?item
(group_concat(distinct ?superClassLabel_en;separator="; ") as ?superClassLabels_en) #Concatenate the values in order not to get several rows per item.
(replace(group_concat(distinct ?superClass;separator="; "), "http://www.wikidata.org/entity/", "") as ?superClasses) #Strip the path in order to get only the Q-number.
?Label_en
(group_concat(distinct ?Alias_en;separator="; ") as ?Aliases_en) #Concatenate the values in order not to get several rows per item.
?Label_de
(group_concat(distinct ?Alias_de;separator="; ") as ?Aliases_de)
?Label_fr
(group_concat(distinct ?Alias_fr;separator="; ") as ?Aliases_fr)
?Label_it
(group_concat(distinct ?Alias_it;separator="; ") as ?Aliases_it)
?Label_es
(group_concat(distinct ?Alias_es;separator="; ") as ?Aliases_es)
?Label_ru
(group_concat(distinct ?Alias_ru;separator="; ") as ?Aliases_ru)
?Description_en
?Description_de
?Description_fr
?Description_it
?Description_es
?Description_ru
WHERE {
{?item wdt:P279+ wd:Q33506} UNION {?item wdt:P279+ wd:Q166118} UNION {?item wdt:P279+ wd:Q7075}.
OPTIONAL { ?item rdfs:label ?Label_en . FILTER (lang(?Label_en) = "en") }
OPTIONAL { ?item rdfs:label ?Label_de . FILTER (lang(?Label_de) = "de") }
OPTIONAL { ?item rdfs:label ?Label_fr . FILTER (lang(?Label_fr) = "fr") }
OPTIONAL { ?item rdfs:label ?Label_it . FILTER (lang(?Label_it) = "it") }
OPTIONAL { ?item rdfs:label ?Label_es . FILTER (lang(?Label_es) = "es") }
OPTIONAL { ?item rdfs:label ?Label_ru . FILTER (lang(?Label_ru) = "ru") }
OPTIONAL { ?item skos:altLabel ?Alias_en . FILTER (lang(?Alias_en) = "en") }
OPTIONAL { ?item skos:altLabel ?Alias_de . FILTER (lang(?Alias_de) = "de") }
OPTIONAL { ?item skos:altLabel ?Alias_fr . FILTER (lang(?Alias_fr) = "fr") }
OPTIONAL { ?item skos:altLabel ?Alias_it . FILTER (lang(?Alias_it) = "it") }
OPTIONAL { ?item skos:altLabel ?Alias_es . FILTER (lang(?Alias_es) = "es") }
OPTIONAL { ?item skos:altLabel ?Alias_ru . FILTER (lang(?Alias_ru) = "ru") }
OPTIONAL { ?item schema:description ?Description_en . FILTER (lang(?Description_en) = "en") }
OPTIONAL { ?item schema:description ?Description_de . FILTER (lang(?Description_de) = "de") }
OPTIONAL { ?item schema:description ?Description_fr . FILTER (lang(?Description_fr) = "fr") }
OPTIONAL { ?item schema:description ?Description_it . FILTER (lang(?Description_it) = "it") }
OPTIONAL { ?item schema:description ?Description_es . FILTER (lang(?Description_es) = "es") }
OPTIONAL { ?item schema:description ?Description_ru . FILTER (lang(?Description_ru) = "ru") }
OPTIONAL { ?item wdt:P279 ?superClass }
OPTIONAL { ?item wdt:P279/rdfs:label ?superClassLabel_en . FILTER (lang(?superClassLabel_en) = "en") }
}
group by ?item #List all the variables for which the values are not concatenated!
?Label_en ?Description_en
?Label_de ?Description_de
?Label_fr ?Description_fr
?Label_it ?Description_it
?Label_es ?Description_es
?Label_ru ?Description_ru
Managing Data Completeness edit
- See the example of the Property Dashboard Template: Wikidata:WikiProject Heritage institutions/Statistics
- Museums in Switzerland lacking coordinate location:
SELECT ?museum ?museumLabel ?coordinate_location
WHERE {
?museum wdt:P31/wdt:P279* wd:Q33506.
?museum wdt:P17 wd:Q39.
FILTER(NOT EXISTS { ?museum wdt:P625 ?coordinate_location. })
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
- See also the Listeria Tool.
Tackling Statements without References edit
- Items for museums in Switzerland containing statements that are not properly referenced, detailing the statements in question.
SELECT ?item ?itemLabel ?property ?propertyLabel ?statement
WITH {
SELECT DISTINCT ?item WHERE {
?item wdt:P31/wdt:P279* wd:Q33506;
wdt:P17 wd:Q39.
}
} AS %items
WITH {
SELECT ?item ?itemLabel WHERE {
INCLUDE %items.
SERVICE wikibase:label { bd:serviceParam wikibase:language "de,fr,it,en". }
}
} AS %itemLabels
WITH {
SELECT ?item ?property ?statement WHERE {
INCLUDE %items.
?property wikibase:claim ?p.
?item ?p ?statement.
MINUS {
# TODO check what counts as good reference
?statement prov:wasDerivedFrom [
pr:P248|pr:P854 ?goodReference
].
}
# TODO expand list of properties that don’t need good references:
FILTER(?p NOT IN (p:P373, #commons category
p:P856, #official website
p:P18, #image
p:P227, #identifier with weblink
p:P214, #identifier with weblink
p:P269, #identifier with weblink
p:P2427, #identifier with weblink
p:P213, #identifier with weblink
p:P4678, #identifier with weblink
p:P3153, #identifier with weblink
p:P902 #identifier with weblink
))
}
} AS %statements
WITH {
SELECT DISTINCT ?property WHERE {
INCLUDE %statements.
}
} AS %properties
WITH {
SELECT ?property ?propertyLabel WHERE {
INCLUDE %properties.
SERVICE wikibase:label { bd:serviceParam wikibase:language "de,fr,it,en". }
}
} AS %propertyLabels
WHERE {
INCLUDE %statements.
INCLUDE %itemLabels.
INCLUDE %propertyLabels.
} ORDER BY ?item
Tackling Data Modelling Issues edit
- See the maintenance tasks on the following page: Wikidata:WikiProject Performing arts/Data structure/Data modelling issues
Inspecting Contradictory Statements edit
Skip for now.