One of the modules in my first semester of the master’s program I’m in is Knowledge Models, for which I’m taking the subject “Knowledge Graphs”. At the beginning it seemed like an intimidating subject, but now it’s both still intimidating and really interesting as well (I prefer not to underestimate subjects until I’ve passed them XD).
As I’m working with knowledge graphs and RDFs, I’ve learned to use SPARQL for writing queries (i.e. writing commands to retrieve information from the graphs you have). On top of that, the people teaching this subject have worked on Wikidata, so many of the assignments of this subject include using the Wikidata query service, which can be so much fun once you get the grasp of it (this is for all of you out there who love reading Wikipedia articles and find fun facts or correlations).
Let’s take a look at some examples of how you can use this query service (you can find more information in the documentation here):
- The first query gives us a list of famous dogs that have a Wikipedia article. You can go to the Wikidata query service and paste the code below, then run it by pressing the “Play” button.
SELECT ?item ?itemLabel
WHERE {
?item wdt:P31 wd:Q144 .
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
}
- This example is a query that gives us a map with the location of all cities in the world that are named “Bergen”.
#defaultView:Map
SELECT DISTINCT ?item ?label ?article ?coordinates
WHERE {
?item rdfs:label "Bergen"@en.
?article schema:about ?item;
schema:isPartOf [ wikibase:wikiGroup "wikipedia" ].
OPTIONAL {
?item wdt:P625 ?coordinates.
}
}
ORDER BY ?item
The result of the query looks like this:
- A last example for today is another map, but this time it has a pin on any city that is the location of formation of a boy band.
#defaultView:Map
SELECT DISTINCT ?boyband ?boybandLabel ?location ?locationLabel ?coord
WHERE {
?boyband wdt:P31 wd:Q5741069;
wdt:P740 ?location.
?location wdt:P625 ?coord.
SERVICE wikibase:label {bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en".}
}
Check out the result here:
Hope this article gave you a bit of an insight of how using the WIkidata query service can be amazing to pull out random facts, let this be your next tool to explore the internet!