SPARQL: How to obtain label in available languages if first option is not available

江枫思渺然 提交于 2019-12-05 10:20:05
Joshua Taylor

First, prefer langMatches for checking language tags. This is especially important in your case, since you might want, for instance, a label in English, and langMatches(lang(?label), "en") will find a label with the tag "en", or "en-GB", or "en-US", etc. Those are regional variants for the language, and langMatches can help you find them.

Updated Solution based on comments

@svick noticed in the comments that the original solution ends up with a row for each element in the Cartesian product of the English names with the non-English names. You can avoid that by using a select distinct. But there's really a better way: just use the same variable in two optionals; the first checks for an English label, and the second checks for non-English labels. If the first succeeds, then the second never gets invoked. That is, just do:

select ?country ?label {
   ?country wdt:P31 wd:Q6256 
   optional { 
     ?country rdfs:label ?label
     filter langMatches(lang(?label), "en")
   }
   optional { 
     ?country rdfs:label ?label
   }
}

Other options



Original Solution with COALESCE

After that, though, coalesce will do what you want. It takes a number of arguments, and returns the first one that has a value. So, you can match the preferred language in an optional block, and any language in another, and coalesce the values:

select distinct ?country (coalesce(?enLabel, ?anyLabel) as ?label) {
   ?country wdt:P31 wd:Q6256 
   optional { 
     ?country rdfs:label ?enLabel
     filter langMatches(lang(?enLabel), "en")
   }
   optional { 
     ?country rdfs:label ?anyLabel
   }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!