SPARQL session at the Wikimedia Prague Pre-Hackathon

SPARQL Session at Prague Pre-hackathon

Am at the Prague pre-hackathon and the guys from Wikipedia UK and Wikipedia Austria gave a session on SPARQL queries. It seems to be a very cool way to get results from Wikidata and visualize it. Adam Shorland and Tobias Schönberg took this session for all the Wikimedia Commons App team in Wikimedia Czech Republic office.

They gave us an introduction to get started with SPARQL queries. SPARQL is an RDF query language, that is, a semantic query language for databases, able to retrieve and manipulate data stored in Resource Description Framework (RDF) format.

Here are a few sample queries that can give you a feeling of how it works.

Query to see a list of world heritage sites

SELECT ?item ?itemLabel ?coord ?image { ?item wdt:P1435 wd:Q9259 . ?item wdt:P17 ?country . ?item wdt:P625 ?coord . ?item wdt:P18 ?image SERVICE wikibase:label {bd:serviceParam wikibase:language "en"} }

See the results here. Here’s a sample output.

List of world heritage sites

Query to show a list of nearby places without any images

The Android Wikimedia Commons app uses this query to get a list of nearby places which don’t have an image on Wikimedia Commons. Anyone is welcome to contribute to the Android app.

SELECT
(SAMPLE(?location) as ?location)
?item
(SAMPLE(COALESCE(?item_label_preferred_language, ?item_label_any_language)) as ?label)
(SAMPLE(?classId) as ?class)
(SAMPLE(COALESCE(?class_label_preferred_language, ?class_label_any_language, "?")) as ?class_label)
(SAMPLE(COALESCE(?icon0, ?icon1)) as ?icon)
(SAMPLE(COALESCE(?emoji0, ?emoji1)) as ?emoji)
?wikipediaArticle
?commonsArticle
WHERE {
# Around given location...
SERVICE wikibase:around {
?item wdt:P625 ?location.
bd:serviceParam wikibase:center "Point(${LONG} ${LAT})"^^geo:wktLiteral.
bd:serviceParam wikibase:radius "${RAD}" . # Radius in kilometers.
}

# ... and without an image.
MINUS {?item wdt:P18 []}

# Get the label in the preferred language of the user, or any other language if no label is available in that language.
OPTIONAL {?item rdfs:label ?item_label_preferred_language. FILTER (lang(?item_label_preferred_language) = "${LANG}")}
OPTIONAL {?item rdfs:label ?item_label_any_language}

# Get the class label in the preferred language of the user, or any other language if no label is available in that language.
OPTIONAL {
?item p:P31/ps:P31 ?classId.
OPTIONAL {?classId rdfs:label ?class_label_preferred_language. FILTER (lang(?class_label_preferred_language) = "${LANG}")}
OPTIONAL {?classId rdfs:label ?class_label_any_language}

# Get icon
OPTIONAL { ?classId wdt:P2910 ?icon0. }
OPTIONAL { ?classId wdt:P279*/wdt:P2910 ?icon1. }
# Get emoji
OPTIONAL { ?classId wdt:P487 ?emoji0. }
OPTIONAL { ?classId wdt:P279*/wdt:P487 ?emoji1. }
OPTIONAL {
?sitelink schema:about ?item .
?sitelink schema:inLanguage "en"
}
OPTIONAL {
?wikipediaArticle schema:about ?item ;
schema:isPartOf <https://en.wikipedia.org/> .
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
}

OPTIONAL {
?commonsArticle schema:about ?item ;
schema:isPartOf <https://commons.wikimedia.org/> .
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
}
}
}
GROUP BY ?item ?wikipediaArticle ?commonsArticle

See the results here. Here’s a sample output.



Originally published at www.maskaravivek.com on May 13, 2017.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Bitnami