Search code examples
sparqlwikipediawikidata

Wikidata Returning Multiple Results For Id


I am trying to get the label of a wikidata item using its id. My sparql looks like:

SELECT ?label
WHERE
{
  wd:Q245068 rdfs:label ?label .
  FILTER (langMatches( lang(?label), "EN" ) )
}

When I run this, I get 3 results that are all "comedian".

dupes

Why does this return multiple results for the same language/id?


Solution

  • Yes, that's funny...

    The reason is obvious if you just check the item

    comedy labels

    "English" means three different things (here labeled in German, the international language of high comedy): Canadian, British, and American English. You get all three.

    Here's how to avoid it:

    SELECT DISTINCT ?label
    WHERE {
        wd:Q245068 rdfs:label ?label .
        FILTER (langMatches( lang(?label), "EN" ) )
    }
    

    Or, use a more specific language code:

    FILTER (langMatches( lang(?label), "EN-GB" ) )
    

    But that runs the risk of not returning any label if it isn't set in the particular variety you've chosen. You can get around that, but really at the end you are just reimplementing the standard service that exists for exactly this purpose:

    # just ad "Label" to get the label in the SELECT
    SELECT ?item ?itemLabel
    WHERE {
        ?item wet:P31 Q5. 
    
        # WITH THIS SERVICE 
        SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
    }