SPARQL:如果第一个选项不可用,如何获取可用语言的标签 [英] SPARQL: How to obtain label in available languages if first option is not available

查看:112
本文介绍了SPARQL:如果第一个选项不可用,如何获取可用语言的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果查询返回的Wikidata资源没有以我过滤的语言提供的可用标签,则获得一个空单元格.

If a Wikidata resource returned by my query has no available label in the language I filtered for I obtained an empty cell.

SELECT *
WHERE
{
    ?country wdt:P31 wd:Q6256.
    ?country rdfs:label ?country_name
        FILTER(LANG(?country_name) = 'jbo').
}

如果第一种语言失败,如何请求以任何一种可用语言返回标签?

How to request to have a label returned in one of any of the available languages if the first language fails?

推荐答案

首先,首选 langMatches 用于检查语言标签.这在您的情况下尤其重要,因为例如,您可能希望使用英文标签,并且 langMatches(lang(?label),"en")会找到带有标签"en"的标签","en-GB"或"en-US"等.这些是该语言的区域变体, langMatches 可以帮助您找到它们.

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.

@svick在注释中注意到,原始解决方案以英语名称与非英语名称的笛卡尔乘积中的每个元素结尾.您可以通过选择不同来避免这种情况.但是确实有更好的方法:只需在两个可选中使用相同的变量;第一个检查英文标签,第二个检查非英文标签.如果第一个成功,则第二个永远不会被调用.也就是说,只需:

@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
   }
}

其他选项

  • 如果需要进行任何汇总,则可能会在多值上下文中的 SPARQL过滤器语言中找到一些帮助
  • 如果在使用第一语言之后,您仍然对其余语言有偏好,则可以在 Sparql多语言数据中找到使用的技术压缩到一行很有帮助.
  • Other options

    • If you need to do any aggregation, you may find some help in SPARQL filter language if possible in multiple value context.
    • If, after the first language, you still have preferences on the remaining languages, you may find the technique used in Sparql multi lang data compression to one row helpful.
    • 不过,此后, 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
         }
      }
      

      这篇关于SPARQL:如果第一个选项不可用,如何获取可用语言的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆