NLAnaconda
NLAnaconda

Reputation: 1605

Wikidata SPARQL filter field values by language

So I have this SparQL query that gets the information of the United States dollar

SELECT 
?currency ?page_title ?currencyLabel ?currencyIso4217 
(GROUP_CONCAT(distinct ?shortName; separator = ', ') as ?shortNames)  
    {  
        ?currency wdt:P498 ?currencyIso4217 .  
        OPTIONAL {?currency wdt:P1813 ?shortName} .    
        ?article schema:about ?currency ; schema:isPartOf <https://en.wikipedia.org/>; schema:name ?page_title    
            SERVICE wikibase:label { bd:serviceParam wikibase:language 'nl' }  
            {
                BIND(wd:Q4917 AS ?currency).  
                OPTIONAL { 
                     SERVICE wikibase:label 
                             { 
                               ?shortName rdfs:label ?shortNameLabel . 
                               bd:serviceParam wikibase:language 'nl'  
                             }  
                         } 
             } 
      }  
group by ?currency ?currencyLabel ?currencyIso4217 ?page_title

Try it here

This outputs: enter image description here

Now when I look at the WikiData page of the USD, and then to the "short names" section I see this:

Wikidata image

They all have a language property between brackets. Now my question: what I want in my query is to have only the "English" version of the short name in it and filter out all the other languages. How can I do that?

Upvotes: 0

Views: 572

Answers (1)

Karl Amort
Karl Amort

Reputation: 16394

Your query is strangely convoluted and I believe you have been led down that path due to a long-standing bug with GROUP_CONCAT in such situations (at least I seem to remember something like that–can't quite look it up right now).

Anyway, here is something that I believe works for you purposes, at least in theory. Practically, there just aren't enough "short names" for it to matter.

SELECT DISTINCT ?currency ?page_title ?currencyLabel ?currencyIso4217 ?shortName WHERE 
  {  
    ?currency wdt:P498 ?currencyIso4217 .  
    ?article schema:about ?currency ; schema:isPartOf <https://en.wikipedia.org/>; schema:name ?page_title .          
    
    OPTIONAL {
      ?currency wdt:P1813 ?shortName .
      FILTER(LANG(?shortName) = "en") .
    }    
    SERVICE wikibase:label { bd:serviceParam wikibase:language 'en' }  
  }

Link to query

You'll notice that I cut out a whole of stuff. But I think the one idea that you've been grasping for is just the line with FILTER. And maybe that LANG(?string) gets you the language.

Upvotes: 0

Related Questions