How do I get the unit of a value from Wikidata using SPARQL? - sparql

I'm new to SPARQL and I'm trying to get the boiling points of a chemical from Wikidata. However, when I try this query on ethanol as an example, I get two numbers, one being the boiling point in Fahrenheit and the other in Celsius, but without being able to tell which is which.
I'd ideally like to filter to only return the Celsius value, but I'd also be happy if I could just get the unit for each value and then I can sort it out later (that's what I've tried so far, as a first step).
I've tried this:
SELECT
?temperature ?temperatureLabel ?unitLabel
WHERE {
wd:Q153 p:P2102 ?statement.
?statement ps:P2102 [
wikibase:quantityAmount ?temperature;
wikibase:quantityUnit ?unit;
].
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
and this:
SELECT
?temperature ?temperatureLabel ?unitLabel
WHERE {
wd:Q153 p:P2102 ?statement.
?statement ps:P2102 ?temperature.
?temperature wikibase:quantityUnit ?unit.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
...amongst other things, but receive no matching results. I've been banging my head against the wall with this for hours now, would really appreciate any help.

ps: links a statement to a simple value. For quantities, this means a value without unit, lower bound and upper bound.
To get the unit, you first need to access the value node from a statement with psv:. From the value node you can then get the unit and the amount with wikibase:quantityUnit and wikibase:quantityAmount.
SELECT ?temperature ?unitLabel WHERE {
wd:Q153 p:P2102 ?statement .
?statement psv:P2102 ?valuenode.
?valuenode wikibase:quantityAmount ?temperature.
?valuenode wikibase:quantityUnit ?unit.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}

Related

How to get *all* superclasses of a Wikidata entity with SPARQL?

I am interested in visualizing the Wikidata class hierarchy to create graphs like
I know how I can get direct superclasses of a Wikidata entity. For this I use SPARQL code like:
SELECT ?item ?itemLabel
WHERE
{
wd:Q125977 wdt:P279 ?item.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
where wdt:P279 denotes the subclass of-property.
However, this direct method requires many single requests to the Wikidata API.
How is it possible to get the same information with a single SPARQL query?
(Note that the example graph above only shows an abbreviated version. The final desired graph of all superclasses is 13 levels deep and has 69 nodes which means 68 single requests, see this jupyter notebook if interested.)
You could use a query like this to create your taxonomy (with labels) as triples directly.
CONSTRUCT {
?item1 wdt:P279 ?item2.
?item1 rdfs:label ?item1Label.
?item2 rdfs:label ?item2Label.
}
WHERE {
SELECT ?item1 ?item2 ?item1Label ?item2Label
WHERE {
wd:Q125977 (wdt:P279*) ?item1, ?item2.
FILTER(EXISTS { ?item1 wdt:P279 ?item2. })
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
}
I think you need a query like the following:
SELECT ?class ?classLabel ?superclass ?superclassLabel
WHERE
{
wd:Q125977 wdt:P279* ?class.
?class wdt:P279 ?superclass.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
where wdt:P279* is a zero-or-more path connecting a class with (a superclass of) one of its superclasses.
This will generate a mapping "class->superclass" containing all what you need for building the graph that you illustrated.

Querying distance units in Sparql - Wikidata example

I would like to know the simplest way to handle units of distance in Sparql. In this Wikidata example I would like to select all the mountains over 8000m; however when I run the text below it also includes all of the mountains over 8000ft. Is it possible to specify that the units I am interested is meters, and for the mountains that are specified in feet, is a conversion to meters necessary?
#Mountains Over 8000m
SELECT ?mountainLabel ?height
WHERE
{
?mountain wdt:P31 wd:Q8502.
?mountain wdt:P2044 ?height.
FILTER(8000 <= ?height)
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en, de". }
}
The psn namespace prefix is used to normalize units of measure. Your query can look like this:
SELECT ?mountainLabel ?height
WHERE
{
?mountain wdt:P31 wd:Q8502.
?mountain p:P2044/psn:P2044/wikibase:quantityAmount ?height .
FILTER(8000 <= ?height)
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en, de". }
}

How to filter wikidata labels in concept search?

I am using the following code to get the wikidata labels for the given concept (e.g., network analysis).
SELECT ?item {
VALUES ?searchTerm { "network analysis" }
SERVICE wikibase:mwapi {
bd:serviceParam wikibase:api "EntitySearch".
bd:serviceParam wikibase:endpoint "www.wikidata.org".
bd:serviceParam wikibase:limit 3 .
bd:serviceParam mwapi:search ?searchTerm.
bd:serviceParam mwapi:language "en".
?item wikibase:apiOutputItem mwapi:item.
?num wikibase:apiOrdinal true.
}
?item (wdt:P279|wdt:P31) ?type
}
ORDER BY ?searchTerm ?num
This returns the following wikidata labels.
wd:Q618079 --> related to electronics
wd:Q4417999 --> related to graph theory (computer science)
wd:Q60640547 --> related to scholary article
I would like to get the wikidata labels that are only related to computer science (i.e. wd:Q4417999 in the above example).
In DBpedia I ran the below query to identify if a word is in computer science.
sparql.setQuery(" ASK { dbc:Network_analysis skos:broader{1,7} dbc:Computer_science } ")
Is it possible to do the same in wikidata (i.e. check if computer science is an ancestor of the given concept and returns only that wikidata label).
If there is a better solution than performing ancestoral search please do suggest me.
I am happy to provide more details if needed.
The sparql query mentioned below solved my issue.
SELECT DISTINCT ?item {
VALUES ?searchTerm { "network analysis"}
SERVICE wikibase:mwapi {
bd:serviceParam wikibase:api "EntitySearch".
bd:serviceParam wikibase:endpoint "www.wikidata.org".
bd:serviceParam wikibase:limit 3 .
bd:serviceParam mwapi:search ?searchTerm.
bd:serviceParam mwapi:language "en".
?item wikibase:apiOutputItem mwapi:item.
?num wikibase:apiOrdinal true.
}
?item (wdt:P279|wdt:P31) ?type
filter exists {?type wdt:P279*/wdt:P361* wd:Q21198}
}
ORDER BY ?searchTerm ?num

Querying wikidata for "property constraint"

TL;DR
How to query (sparql) about properties of a property?
Or..
So as part of my project I need to find the properties in wikidata that have any time constraint, to be specific both "start time" and "end time".
I tried this query:
SELECT DISTINCT ?prop WHERE {
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
?person wdt:P31 wd:Q5.
?person ?prop ?statement.
?statement pq:P580 ?starttime.
?statement pq:P582 ?endtime.
}
LIMIT 200
**yeah the properties should be related to humans
Anyway, I do get some good results like:
http://www.wikidata.org/prop/P26
http://www.wikidata.org/prop/P39
But I also get some other properties that definitely wrong.
so, basically what i'm trying to do is to get a list of properties that has the property constraint (P2302) of- allowed qualifiers constraint (Q21510851) with Start time (P580) and End Time (P582)
is that even possible:
I tried some queries like:
SELECT DISTINCT ?property ?propertyLabel ?propertyDescription ?subpTypeOf ?subpTypeOfLabel
WHERE
{
?property rdf:type wikibase:Property .
?property wdt:P2302 ?subpTypeOf.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
}
but does not get the results I wanted.
is it even possible to query this kind of stuff?
Thanks
Qualifiers are used on property pages too. Your second query should be:
SELECT DISTINCT ?prop ?propLabel {
?prop p:P2302 [ ps:P2302 wd:Q21510851 ; pq:P2306 wd:P580, wd:P582 ] ;
p:P2302 [ ps:P2302 wd:Q21503250 ; pq:P2308 wd:Q5 ; pq:P2309 wd:Q21503252 ] .
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" }
} ORDER BY ASC(xsd:integer(strafter(str(?prop), concat(str(wd:), "P"))))
Try it!
Your first query is correct, but note that this is an 'as-is' query. For example, wd:P410 does not have respective constraints, but look at wd:Q83855.

Filter by type in Wikidata

This SPARQL request looks for all cities called "Berlin" in Wikidata:
SELECT DISTINCT ?item ?itemLabel ?itemDescription WHERE {
?type (a | wdt:P279) wd:Q515. # Sub-type of city
?item wdt:P31 ?type.
?item rdfs:label "Berlin"#en.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
PROBLEM: It returns zero result.
Meanwhile, the request below correctly finds Q64 (capital and city-state of Germany), but it also returns a lot of other things called Berlin, so I want to filter on cities (then in a future phase I will order these cities by population, but that is outside the scope of this question):
SELECT DISTINCT ?item ?itemLabel ?itemDescription WHERE {
?item rdfs:label "Berlin"#en.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
Note: My code for getting instances of subclasses of city (Berlin is a big city which is subclass of city) seems to work correctly, as illustrated by the results of this query.
It was a Wikidata bug.
According to Wikidata's Jura1, it was a bug in Wikidata caused by someone's experiments with "preferred rank".
Discussion at https://www.wikidata.org/wiki/Wikidata:Project_chat/Archive/2016/09#P31_inconsistency
The bug has been fixed just now.
You can only query for data that is contained in the dataset.
If you try an alternative of your query
SELECT DISTINCT ?item ?itemLabel ?itemDescription ?type1 ?type2 WHERE {
?item rdfs:label "Berlin"#en.
optional{?item rdf:type ?type1 }
optional{?item wdt:P279 ?type2 }
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
it returns no types, neither connected by rdf:type nor wdt:P279.
If you have a look at the entity of the capital and city state Berlin, you can see that there is information about "instance of", but this property is supposed to be https://www.wikidata.org/wiki/Property:P31. And none of them links to wd:Q515, I'm wondering from where you got this idea.
But to be honest, I don't know that much about Wikidata and to me, it's not clear why no rdf:type is used, but a common pattern for RDF datasets is to use
?s rdf:type/rdfs:subClassOf* SUPER_CLASS .
if we assume that there is rdf:type information available.
If you check the types wd:Q64 is an instance of
SELECT DISTINCT ?type ?typeLabel WHERE {
wd:Q64 (a | wdt:P31) ?type.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
ORDER BY ?item
None of them are City (wd:Q515) or a sub-class of it.
Looks like a data issue. Perhaps you should contact Wikidata.