SPARQL/SERVICE - Label
You can fetch the label, alias, or description of entities you query, with language fallback, using the specialized service with the URI <http://wikiba.se/ontology#label>. The service is very helpful when you want to retrieve labels, as it reduces the complexity of SPARQL queries that you would otherwise need to achieve the same effect.
The service can be used in one of the two modes: manual and automatic.
Automatic Label SERVICE
[edit | edit source]In automatic mode, you only need to specify the service template, e.g.:
PREFIX wikibase: <http://wikiba.se/ontology#>
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
and WDQS will automatically generate labels as follows:
- If an unbound variable in
SELECT
is named?NAMELabel
, then WDQS produces the label (rdfs:label
) for the entity in variable?NAME
. - If an unbound variable in
SELECT
is named?NAMEAltLabel
, then WDQS produces the alias (skos:altLabel
) for the entity in variable?NAME
. - If an unbound variable in
SELECT
is named?NAMEDescription
, then WDQS produces the description (schema:description
) for the entity in variable?NAME
.
In each case, the variable in ?NAME
should be bound, otherwise the service fails.
Example, showing the list of EU country names and capitals in french. For demonstation also Description and AltLabel are shown
SELECT ?country ?countryLabel ?capitalLabel ?capitalDescription ?capitalAltLabel
WHERE {
wd:Q458 wdt:P150 ?country. # European Union contains administrative territorial entity
OPTIONAL{ ?country wdt:P36 ?capital. }
SERVICE wikibase:label { bd:serviceParam wikibase:language "fr". }
}
In this example WDQS automatically creates the labels ?countryLabel
, ?capitalLabel
, ?capitalDescription
and ?capitalAltLabel
.
Manual Label SERVICE
[edit | edit source]In the manual mode, you explicitly bind the label variables within the service call, but WDQS will still provide language resolution and fallback.
Manual Label service is mandatory for using labels in some SPARQL functions like
GROUP_CONCAT
- Aggregate functions
MIN
,MAX
,SUM
orAVG
Example:
SELECT *
WHERE {
SERVICE wikibase:label { bd:serviceParam wikibase:language "fr,de,en".
wd:Q123 rdfs:label ?q123Label.
wd:Q123 skos:altLabel ?q123Alt.
wd:Q123 schema:description ?q123Desc.
}
}
This will consider labels and descriptions in French, German and English, and if none are available, will use the Q-id as the label.
Manual labels can also be used to list labels in more languages, for instance European countries in English, German and French
SELECT ?country ?country_EN ?country_DE ?country_FR
WHERE {
wd:Q458 wdt:P150 ?country. # European Union contains administrative territorial entity
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.
}
PS: hint:Prior hint:runLast false.
is added to prevent error: "there can be only one "run last" join in any group". [1]
It is also possible to write the above query by using rdfs:label
directly without the wikibase:label
SERVICE
:
SELECT ?country ?country_EN ?country_DE ?country_FR
WHERE {
wd:Q458 wdt:P150 ?country. # European Union contains administrative territorial entity
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")}.
}
Languages
[edit | edit source]You can specify a list of languages to be used as fallback in case a label does not exist in a language
You specify your preferred language(s) for the label with one or more of bd:serviceParam wikibase:language "language-code"
triples. Each string can contain one or more language codes, separated by commas. WDQS considers languages in the order in which you specify them. If no label is available in any of the specified languages, the Q-id of the entity (without any prefix) is its label.
SERVICE wikibase:label { bd:serviceParam wikibase:language "fr,de,en" }
The Wikidata Query Service website auto-magically replaces [AUTO_LANGUAGE]
with the language code of current user's interface. For example, if the user's UI is in French, the SPARQL's code bd:serviceParam wikibase:language "[AUTO_LANGUAGE],de,en"
will be converted to bd:serviceParam wikibase:language "fr,de,en"
before being sent to the query service.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],de,en" }
References
[edit | edit source]mw:Wikidata query service/User Manual