Wikidata SPARQL endpoint in Mathematica#
by Vishank Patel
See the Wikidata Welcome Page, Wikidata Query Service/User Manual and Mathematica WikidataData documentation.
These recipe examples were tested on February 10, 2022.
1. Basic Wikidata request#
Mathematica has a built-in function “WikidataData” to request information from WikiData’s SPARQL endpoint. Arguments can be in the form of Mathematica entities or external identifiers, which are unique for every listed object.
Mathematica Entities#
Mathematica entities can be queried using Mathematica’s free form input (“Ctrl” + “ =” , then typing the name in normally). As we are in a jupyter notebook, we will need to use the Entity function:
Entity["Planet", "Earth"]["Radius"]
External identifiers#
Wikidata#
External identifiers from Wikidata can be selected using the WikidataSearch command.
WikidataSearch["Earth"]
As you can see, we get a list of outputs from Wikidata. To extract the first element, we use the shorthand notation for the part command:
WikidataSearch["Earth"][[1]]
To extract information about the object, we query WikidataData using the identifier
WikidataData[WikidataSearch["Earth"][[1]], "Radius"]
WikidataData’s syntax is generally the following:
WikidataData[item, {property1, property2, …}]
To check all the available properties for an entity, we can run the following command:
WikidataData[WikidataSearch["Nick Saban"][[1]], "Properties"]
If the properties requested are more specialized, they can be inputted by searching for the property object on Wikidata.
WikidataData[
WikidataSearch["Nick Saban"][[1]],
WikidataSearch["Property" -> "Award received"][[1]]]
Other Identifiers#
Wikidata can also accept input from other identifier systems such as ISBN, DOI, PubChemCompoundID and many more:
WikidataData[
ExternalIdentifier["PubChemCompoundID", "123591"],
WikidataSearch["Property" -> "Chemical structure"][[1]]]
2. Example Use-cases#
Entity classes behave like a collection of entities. Defining classes allows us to add constrains to our search and get specific useful data.
Retrieving melting points of elements#
WikidataData[
{Entity["Element","Aluminium"],Entity["Element","Lead"],Entity["Element","Tungsten"]},
WikidataSearch["Property"->"melting point"][[1]],"Association"]
To be more specific, we can obtain the melting points of elements with density from 7 to 8 grams/cm^3
WikidataData[
EntityList[
EntityClass[
"Element",
{"Density" -> Quantity[Interval[{7, 8}], "Grams"/"Centimeters"^3]}]],
WikidataSearch["Property" -> "melting point"][[1]], "Association"]
NOTE : In Mathematica, the EntityClass above can be easily constructed using Mathematica’s free-form input. For instance, typing “earliest discovered elements” in the orange box(“Ctrl”+”=”) gives the following output:
WikidataData[
EntityList[
EntityClass[
"Element", {EntityProperty["Element", "DiscoveryDate"] -> TakeLargest[5]}]],
WikidataSearch["Property" -> "discoverer"][[1]], "Dataset"]
Population distribution of Alabama counties#
populationData =
WikidataData[
EntityList[
Entity["AdministrativeDivision", {"Alabama", "UnitedStates"}]["Subdivisions"]],
WikidataSearch["Property" -> "population"][[1]],
"Association"];
ReverseSortBy[populationData, N] // Shallow
Publication dates of books written by Newton#
WikidataData[
EntityClass[
WikidataSearch["Written work"][[1]],
{WikidataSearch["Property" -> "Author"][[1]] -> Entity["Person", "IsaacNewton"]}],
WikidataSearch["Property" -> "publication date"][[1]],
"Dataset"]
SI unit symbols and their measured physical quantities#
WikidataData[
EntityClass[WikidataSearch["SI base Unit"][[1]], {}],
{WikidataSearch["Property" -> "unit symbol"][[1]],
WikidataSearch["Property" -> "Measured physical quantity"][[1]]},
"Dataset"]
3. SPARQL Query Syntax in Mathematica#
Importing the necessary packages:
Needs["GraphStore`"]
It is better to write the queries in the WikiData Query Service (WDQS) at https://query.wikidata.org/ given the autocompletion functionality.
WikiData SPARQL tutorial: https://www.wikidata.org/wiki/Wikidata:SPARQL_tutorial (It is amazing!)
Once the query is written, we can run it in Mathematica as follows:
US Renewable Energy Farms#
endpoint = "https://query.wikidata.org/sparql";
WindQuery = "SELECT ?powerplant ?pos ?powerplantLabel
WHERE
{
?powerplant wdt:P17 wd:Q30;
wdt:P31 wd:Q194356;
wdt:P625 ?pos;
SERVICE wikibase:label { bd:serviceParam wikibase:language \"[AUTO_LANGUAGE],en \". }
}";
WindFarmData = SPARQLExecute[endpoint, WindQuery];
WindFarmData //Shallow
GeoListPlot["pos" /. WindFarmData]
SolarQuery = "SELECT ?powerplant ?pos ?powerplantLabel
WHERE
{
?powerplant wdt:P17 wd:Q30;
wdt:P31 wd:Q1003207;
wdt:P625 ?pos;
SERVICE wikibase:label { bd:serviceParam wikibase:language \"[AUTO_LANGUAGE],en\". }
}";
SolarFarmData = SPARQLExecute[endpoint, SolarQuery];
SolarFarmData // Shallow
GeoListPlot["pos" /. SolarFarmData]