使用 SPARQL 提取包含特定子串的三元组 [英] Extract triples containing particular substring using SPARQL

查看:58
本文介绍了使用 SPARQL 提取包含特定子串的三元组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提取一个包含单词alice"的三元组.我使用的查询是:

I want to extract a triple which contains word say "alice" in its subject. The query I used was:

SELECT ?s ?p ?o  WHERE { ?s ?p ?o .FILTER regex(?s, \"alice\") .}

这不会给我任何结果,尽管有一个满足这个约束的三元组.

This doesn't give me any results inspite of have a triple which satisfies this constraint.

另一方面,当我使用相同的查询提取在其对象中包含单词 brillant 的三元组时,它仅返回 2 个可能匹配项中的一个.

On the other hand when I use the same query to extract a triple which contains a word brillant in its object .It returns only one of the 2 possible matches.

使用的查询是:

SELECT ?s ?p ?o  WHERE { ?s ?p ?o .FILTER regex(?o, \"brillant\") .}

请让我知道我哪里出错了以及这种行为的原因是什么.

Please let me know where am I going wrong and what is the reason for this behaviour.

推荐答案

我假设引号周围的转义只是复制和粘贴的残余.regex 的第一个参数必须是文字,但文字不能是RDF 中三元组的主题,因此您的数据不应该与此模式匹配.但是,您可能拥有的是其 URI 包含字符串alice"的主题,您可以使用 str 函数.例如,

I'll assume that the escapes around the quotation marks are just a remnant from copying and pasting. The first argument to regex must be a literal, but literals cannot be the subjects of triples in RDF, so it's not true that you have data that should match this pattern. What you might have, though, is subjects whose URI contains the string "alice", and you can get the string representation of the URI using the str function. E.g.,

SELECT ?s ?p ?o  WHERE { ?s ?p ?o .FILTER regex(str(?s), "alice") .}

为了说明,让我们使用两个值 "string contains example" 并像您在原始查询中所做的那样进行过滤:

To illustrate, let's use the two values <http://example.org> and "string containing example" and filter as you did in your original query:

select ?x where {
  values ?x { <http://example.org> "string containing example" }
  filter( regex(?x, "exam" ))
}

-------------------------------
| x                           |
===============================
| "string containing example" |
-------------------------------

我们只得到 "string contains example" 因为另一个值不是字符串,所以不是 regex 的合适参数.但是,如果我们添加对 str 的调用,那么正则表达式将考虑 URI 的字符串表示形式:

We only got "string containing example" because the other value wasn't a string, and so wasn't a suitable argument to regex. However, if we add the call to str, then it's the string representation of the URI that regex will consider:

select ?x where {
  values ?x { <http://example.org> "string containing example" }
  filter( regex(str(?x), "exam" ))
}

-------------------------------
| x                           |
===============================
| <http://example.org>        |
| "string containing example" |
-------------------------------

这篇关于使用 SPARQL 提取包含特定子串的三元组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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