SPARQL: Querying Wikidata labels for more than one language
Asked Answered
H

2

4

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?

Hecto answered 5/3, 2018 at 20:15 Comment(0)
H
6

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

Hecto answered 5/3, 2018 at 20:50 Comment(4)
FYI, this doesn't provide Q-id fallbacks.Handknit
What is "Q-id fallback"?Hecto
For example, Lithuanian label for Q29999 is missing. The label service would return "Q29999".Handknit
SPARQL 1.1 COALESCESapphira
H
6

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:

Handknit answered 6/3, 2018 at 11:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.