SPARQL/SERVICE/Label

From Wikibooks, open books for an open world
Jump to: navigation, search

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]

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 {
     ?country wdt:P31 wd:Q185441. # member state of the European Union
     OPTIONAL{ ?country wdt:P36 ?capital. }
     SERVICE wikibase:label { bd:serviceParam wikibase:language "fr". }
}

Try it!

In this example WDQS automatically creates the labels ?countryLabel, ?capitalLabel, ?capitalDescription and ?capitalAltLabel.

Manual Label SERVICE[edit]

In the manual mode, you explicitly bind the label variables within the service call, but WDQS will still provide language resolution and fallback. 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.
          wd:Q321 rdf:label          ?q321Label.
     }
   }

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 {
     ?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!

Manual Label service is mandatory for using labels in some SPARQL functions like

Languages[edit]

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]

mw:Wikidata query service/User Manual