Search code examples
sparqlwikidata

Empty Labels by querying with SPARQL in WikiData


Update: This question is linked to the other question via 1 because it is about the same output data.

I have got a list of 1200 geographic enties like cities, lakes oder mountains as strings. I would like to enrich these enties with the authority file WikiData ID. This works but as result I get sometimes more than one WikiDataID suggestion. I need to define the right one by the apperance of any country (it doesn't matter which one) in the Statements as a constraint.

As an example I tried the city Karlsruhe. For the string "Karlsruhe" I get five results. In this case I choose the ID Q1040 (https://www.wikidata.org/wiki/Q1040). Now I have got the problem that I don't know how to query the altLabel (Also konwn as) and the Label in Englisch, German and French. With the current code I get empty entries for the altLabel and the Labels although there are altLabels and Labels (see WikiData). I think I have just used the wrong order of the code lines. Can someone help me with the right order?

Thank you very much for your help!

Here is the query:

SELECT DISTINCT ?item ?itemLabel ?altLabel ?label_en ?label_de ? 
label_fr WHERE { 
?item rdfs:label "Karlsruhe"@de.
?item wdt:P17 [] .

optional {
?item skos:altLabel ?altLabel.
?item rdfs:label ?label_de FILTER((LANG(?label_de)) = "de") .
?item rdfs:label ?label_en FILTER((LANG(?label_en)) = "en") .
?item rdfs:label ?label_fr FILTER((LANG(?label_fr)) = "fr") .
FILTER(LANGMATCHES(LANG(?altLabel), ?item))
}
SERVICE wikibase:label { bd:serviceParam wikibase:language " 
[AUTO_LANGUAGE],de, en, fr". }
} 

Try it


Solution

  • This query worked for my project:

    PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
    PREFIX schema: <http://schema.org/>
    PREFIX wikibase: <http://wikiba.se/ontology#>
    PREFIX wd: <http://www.wikidata.org/entity/>
    PREFIX wdt: <http://www.wikidata.org/prop/direct/>
    
    SELECT DISTINCT  * 
    
    WHERE {
      ?item rdfs:label "Karlsruhe"@de.   
    
      ?item rdfs:label ?label_de.
      FILTER((LANG(?label_de)) = "de").
    
      SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE], de, en, fr". }
    
        bind(if(exists{?item wdt:P17 []}, "yes", "no") as ?country_)
    
      optional {
    
          ?item rdfs:label ?label_en.     
          FILTER((LANG(?label_en)) = "en").      
          ?item rdfs:label ?label_fr.  
          FILTER((LANG(?label_fr)) = "fr").
    
        ?item skos:altLabel ?altLabel_de.
        FILTER(LANGMATCHES(LANG(?altLabel_de), "de"))
        optional {
        ?item skos:altLabel ?altLabel_en.
        FILTER(LANGMATCHES(LANG(?altLabel_en), "en"))
          }
        optional {
        ?item skos:altLabel ?altLabel_fr.
        FILTER(LANGMATCHES(LANG(?altLabel_fr), "fr"))
          }
      }  
    } 
    order by ?item