How to get Wikidata labels by ID using SPARQL?

☆樱花仙子☆ 提交于 2019-12-06 01:27:13

问题


How can I get the labels in English or any other language in Wikidata by ID using SPARQL endpoint?


回答1:


Suppose wd:Q146190 is your wikidata entity ID

get the label in a specific language of your specific entity ID:

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
PREFIX wd: <http://www.wikidata.org/entity/> 
SELECT  *
WHERE {
        wd:Q146190 rdfs:label ?label .
        FILTER (langMatches( lang(?label), "EN" ) )
      } 
LIMIT 1
  • live example in english

  • live example in german

get all the labels in any language of your specific entity ID:

SELECT * WHERE {
  wd:Q146190 rdfs:label ?label 
}

here the link to the live try press on play to run the query then you could download a full JSON and get such a rensponse ..only a trunk copied here:

{
    "head": {
        "vars": [
            "label"
        ]
    },
    "results": {
        "bindings": [
            {
                "label": {
                    "xml:lang": "ar",
                    "type": "literal",
                    "value": "دوار الشمس الدرني"
                }
            },
            {
                "label": {
                    "xml:lang": "az",
                    "type": "literal",
                    "value": "Kökyumrulu günəbaxan"
                }
            },
          ..etc,etc.

get labels for each entity ID in a response resultset with multiple id

in this case you should use the Label service

  • example without labels

  • example with labels for each entity in the specified language: english

SELECT ?p ?pLabel ?w ?wLabel WHERE {
   wd:Q30 p:P6/ps:P6 ?p .
   ?p wdt:P26 ?w .
   SERVICE wikibase:label {
    bd:serviceParam wikibase:language "en" .
   }
 }

to use this service add Label to the variable ( ie: for ?p label you must use ?pLabel then add

   SERVICE wikibase:label {
    bd:serviceParam wikibase:language "en" .
   }  

to the WHERE block



来源:https://stackoverflow.com/questions/40268148/how-to-get-wikidata-labels-by-id-using-sparql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!