SPARQL: Querying Wikidata labels for more than one language - sparql

I am trying to get labels in multiple languages from Wikidata's SPARQL endpoint. The following example is given here:
SELECT ?country ?country_EN ?country_DE ?country_FR
WHERE {
?country wdt:P31 wd:Q185441. # member state of the European Union
SERVICE wikibase:label { bd:serviceParam wikibase:language "en".
?country rdfs:label ?country_EN.
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "de".
?country rdfs:label ?country_DE.
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "fr".
?country rdfs:label ?country_FR.
}
}
Try it here
However, this returns the following error:
Unknown error: there can be only one "run last" join in any group
Is there a solution to get labels in more than one language?

rdfs:label can be used directly without the wikibase:label service:
SELECT ?country ?country_en ?country_de ?country_fr
WHERE {
?country wdt:P31 wd:Q185441. # member state of the European Union
OPTIONAL {?country rdfs:label ?country_en filter (lang(?country_en) = "en")}.
OPTIONAL {?country rdfs:label ?country_de filter (lang(?country_de) = "de")}.
OPTIONAL {?country rdfs:label ?country_fr filter (lang(?country_fr) = "fr")}.
}
Try it here

The label service optimizer adds a hint:Prior hint:runLast true hint to the label service unless there’s another explicit hint:
LabelServiceUtils.getLabelServiceNodes(op).forEach(service -> {
if (service.getProperty(QueryHints.RUN_LAST) != null ||
service.getProperty(QueryHints.RUN_FIRST) != null) {
return;
}
service.setProperty(QueryHints.RUN_LAST, TRUE);
});
One should just add hint:Prior hint:runLast false to all the label service invocations after the first one.
Your query should be:
SELECT ?country ?country_EN ?country_DE ?country_FR
WHERE {
?country wdt:P463 wd:Q458. # member state of the European Union
SERVICE wikibase:label { bd:serviceParam wikibase:language "en".
?country rdfs:label ?country_EN.
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "de".
?country rdfs:label ?country_DE.
} hint:Prior hint:runLast false.
SERVICE wikibase:label { bd:serviceParam wikibase:language "fr".
?country rdfs:label ?country_FR.
} hint:Prior hint:runLast false.
}
Try it!
Obviously, it is possible to fetch labels in multiple languages using regular SPARQL, and this is less verbose. However the label service provides language fallbacks, including the final one to Q-id's.
Source:
https://phabricator.wikimedia.org/T175840

Related

Query for EU universities from Wikidata times out

Asking the Wikidata service for all universities works. But when restricted to only EU universities, this query times out.
WHERE {
?item wdt:P31/wdt:P279* wd:Q3918 ;
wdt:P17 ?country.
?country wdt:P463 wd:Q458 .
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
Any idea?
Strangely, using FILTER EXISTS is returns the result (although quite slow).
SELECT ?item ?itemLabel ?countryLabel ?country
WHERE {
?item wdt:P31/wdt:P279* wd:Q3918 ;
wdt:P17 ?country.
FILTER EXISTS { ?country wdt:P463 wd:Q458 }
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
Here's a link to the second query.

Path matching inside a VALUES clause

I'm trying to perform path matching inside a VALUES clause in sparql in order to match all instances and subclasses of both battles and sieges in wikidata. The following request repeatedly times out.
SELECT DISTINCT ?battle ?battleLabel WHERE {
{
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
VALUES ?type {wd:Q178561 wd:Q188055} ?battle (wdt:P31/wdt:P279*) ?type .
?battle rdfs:label ?queryByTitle.
FILTER(REGEX(?queryByTitle, "saratoga", "i"))
}
}
It seems that VALUES, esp. in conjunction with /, confuses the Blazegraph's query optimizer in that case.
Use UNION instead of VALUES:
SELECT DISTINCT ?battle ?battleLabel WHERE {
{ ?battle wdt:P31/wdt:P279* wd:Q178561 }
UNION
{ ?battle wdt:P31/wdt:P279* wd:Q188055 }
?battle rdfs:label ?queryByTitle.
FILTER(REGEX(?queryByTitle, "saratoga", "i"))
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" }
}
Alternatively, disable the optimizer and specify explicit order:
SELECT DISTINCT ?battle ?battleLabel WHERE {
hint:Query hint:optimizer "None" .
VALUES ?type {wd:Q178561 wd:Q188055}
?subtype wdt:P279* ?type .
?battle wdt:P31 ?subtype .
?battle rdfs:label ?queryByTitle.
FILTER(REGEX(?queryByTitle, "saratoga", "i"))
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" }
}

Find people whose name start with a particular letter with sparql Wikidata?

This is my query but I don't get any results for records starting with "n"
SELECT ?item ?itemLabel
WHERE
{
?item wdt:P31/wdt:P279* wd:Q5.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
FILTER regex (?itemLabel, "^(n)").
}

Wikidata Query Names of city in all languages

I query a citiy with its population from wikidata. With the language service i get he label in the given language.
But i would like to get the cityname in all languages or at least in multiple languages at once . I tried to pass * as language but i don't get all citynames returned.
Do I have to make the query for each language once ?
This is my query
SELECT DISTINCT ?city ?cityLabel ?population
WHERE
{
?city wdt:P31/wdt:P279* wd:Q515 .
?city wdt:P1566 "2950157" .
?city wdt:P1082 ?population .
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}
I also tried to just set to languages like this
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
bd:serviceparam wikibase:language "de" .
}
but it returns
Unknown error: Expected a variable in the object position to which to
bind the language.
The error is telling you that you have to bind the German tag to a variable (?cityGe) in the selection process.
SELECT DISTINCT ?city ?cityLabel ?cityGe ?population
WHERE
{
?city wdt:P31/wdt:P279* wd:Q515 .
?city wdt:P1566 "2950157" .
?city wdt:P1082 ?population .
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
bd:serviceParam wikibase:language "de" .
}
}
However, this does not solve your problem because this is only a fallback mechanism. If it cannot find English then it gives you German label.There are some examples here.
This can be simplified as follows with only one variable:
SERVICE wikibase:label { bd:serviceParam wikibase:language "en,de" }
However, as AKSW points out, you can use rdfs:label for your problem:
SELECT DISTINCT ?city ?label ?population
WHERE
{
?city wdt:P31/wdt:P279* wd:Q515 .
?city wdt:P1566 "2950157" .
?city wdt:P1082 ?population .
?city rdfs:label ?label
filter(lang(?label) = 'de' || lang(?label) = 'en')
}

Wikidata: list every physical objects

I'm trying to get the name of the all physical things (tangible concepts) Wikidata knows about (objects, places, countries, etc), or in other words everything non-abstract.
There are examples close to what I need, but with only a depth of one: all the things that are instances of phone.
I found this example that searches with more depth and I modified the start point to entity:
#Children of Genghis Khan
#added before 2016-10
#defaultView:Graph
PREFIX gas: <http://www.bigdata.com/rdf/gas#>
SELECT ?item ?itemLabel ?pic ?linkTo
WHERE
{
SERVICE gas:service {
gas:program gas:gasClass "com.bigdata.rdf.graph.analytics.SSSP" ;
gas:in wd:Q35120 ;
gas:traversalDirection "Forward" ;
gas:out ?item ;
gas:out1 ?depth ;
gas:maxIterations 4 ;
gas:linkType wdt:279 .
}
OPTIONAL { ?item wdt:P40 ?linkTo }
OPTIONAL { ?item wdt:P18 ?pic }
SERVICE wikibase:label {bd:serviceParam wikibase:language "en" }
}
I still get no results.
As mentionned in a comment, your question is too broad and you'll end up with too much answers
You look for the A that are instance of a subclass of B
so the query you point to is the right one
SELECT DISTINCT ?item
WHERE {
?item wdt:P31/wdt:P279* wd:Q35120
}
the problem is the size
wd:Q35120 has a lot of subclasses
You can check that this way
SELECT ?a ?aLabel WHERE { ?a wdt:P279 wd:Q35120.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
SELECT ?a ?aLabel WHERE { ?a wdt:P279/wdt:P279? wd:Q35120.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
SELECT ?a ?aLabel WHERE { ?a wdt:P279/wdt:P279?/wdt:P279? wd:Q35120.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
And so on : you'll see that there are already 40'000+ at forth level which is huge
You could also huge this nice tool to have a more precise view
https://tools.wmflabs.org/bambots/WikidataClasses.php?id=Q35120&lang=en