SPARQL 选择可选语言

发布于 2024-12-12 06:34:28 字数 541 浏览 0 评论 0原文

我有一些如下所示的三元组:

test:thing rdfs:label "Non-Language Label"
test:thing rdfs:label "English Label"@en
test:thing rdfs:label "French Label"@fr

我想形成一个 sparql 查询,为我提供“非语言标签”和“法语标签”(如果存在)。

我试过了,但不起作用:

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
SELECT ?label ?preferredLabel
WHERE {
   test:thing rdfs:label ?label 
   OPTIONAL {
     test:thing rdfs:label ?preferredLabel . 
     FILTER (regex(str(?preferredLabel), '(^|\\\\W)fr', 'i'))
   }
}

提前致谢!

I have some triples that look like this:

test:thing rdfs:label "Non-Language Label"
test:thing rdfs:label "English Label"@en
test:thing rdfs:label "French Label"@fr

I'd like to form a sparql query that gives me the "Non-Language Label" AND the "French Label", if any exists.

I tried this and it's not working:

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
SELECT ?label ?preferredLabel
WHERE {
   test:thing rdfs:label ?label 
   OPTIONAL {
     test:thing rdfs:label ?preferredLabel . 
     FILTER (regex(str(?preferredLabel), '(^|\\\\W)fr', 'i'))
   }
}

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

痴骨ら 2024-12-19 06:34:28

我根本不明白为什么你需要OPTIONAL在这里。 Jan 的查询失败,因为外部模式和可选之间没有共享变量,因此您尝试计算 test:thing 的每个标签与每个非/法国标签 test 的叉积:thing 可能很大以及查询处理器失败的原因。

你只是想要类似下面的东西,除非我误解了你的问题

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
SELECT ?label
WHERE 
{
   test:thing rdfs:label ?label 
   FILTER(LANG(?label) = "" || LANGMATCHES(LANG(?label), "fr"))
}

如果你想要单独的两个标签那么你可以这样做:

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
SELECT ?label ?preferredLabel
WHERE 
{
  {
   test:thing rdfs:label ?label . FILTER(LANG(?label) = "")
  }
  UNION
  {
   test:thing rdfs:label ?preferredLabel . FILTER(LANGMATCHES(LANG(?label), "fr"))
  }
}

I don't see why you need OPTIONAL here at all. Jan's query is failing because there is no shared variable between the outer pattern and the optional so you are trying to calculate the cross product of every label for test:thing with every non/french labelled test:thing which may be huge and why the query processor is failing.

You simply want something like the following unless I've misunderstood your question

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
SELECT ?label
WHERE 
{
   test:thing rdfs:label ?label 
   FILTER(LANG(?label) = "" || LANGMATCHES(LANG(?label), "fr"))
}

If you want the two labels separately then you could do something like:

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
SELECT ?label ?preferredLabel
WHERE 
{
  {
   test:thing rdfs:label ?label . FILTER(LANG(?label) = "")
  }
  UNION
  {
   test:thing rdfs:label ?preferredLabel . FILTER(LANGMATCHES(LANG(?label), "fr"))
  }
}
请远离我 2024-12-19 06:34:28

检查文字语言的最简单方法是使用 lang() 函数。使用它,您的查询可以写为:

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
PREFIX test: <http://test#> 
SELECT ?label ?preferredLabel 
WHERE { 
   test:thing rdfs:label ?label 
   OPTIONAL { 
     test:thing rdfs:label ?preferredLabel . 
     FILTER (lang(?preferredLabel) = "" || lang(?preferredLabel) = "fr") 
   } 
}

The easiest way to check the language of literals is to use the lang() function. Using this, your query can be written as:

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
PREFIX test: <http://test#> 
SELECT ?label ?preferredLabel 
WHERE { 
   test:thing rdfs:label ?label 
   OPTIONAL { 
     test:thing rdfs:label ?preferredLabel . 
     FILTER (lang(?preferredLabel) = "" || lang(?preferredLabel) = "fr") 
   } 
}
丑疤怪 2024-12-19 06:34:28
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
SELECT ?uri ?label ?preferredLabel
WHERE 
{
  {
   ?uri rdfs:label ?label . FILTER(LANG(?label) = "" && regex(str(?label), '(^|\\\\W)fr', 'i'))
  }
  UNION
  {
   ?uri rdfs:label ?preferredLabel . FILTER(LANG(?preferredLabel) = "fr" && regex(str(?preferredLabel), '(^|\\\\W)fr', 'i'))
  }
}
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
SELECT ?uri ?label ?preferredLabel
WHERE 
{
  {
   ?uri rdfs:label ?label . FILTER(LANG(?label) = "" && regex(str(?label), '(^|\\\\W)fr', 'i'))
  }
  UNION
  {
   ?uri rdfs:label ?preferredLabel . FILTER(LANG(?preferredLabel) = "fr" && regex(str(?preferredLabel), '(^|\\\\W)fr', 'i'))
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文