这是我的查询
PREFIX : <http://example.org/rs#>
select ?item (SUM(?similarity) as ?summedSimilarity)
(group_concat(distinct ?becauseOf ; separator = " , ") as ?reason) where
{
values ?x {:instance1}
{
?x ?p ?instance.
?item ?p ?instance.
?p :hasSimilarityValue ?similarity
bind (?p as ?becauseOf)
}
union
{
?x a ?class.
?item a ?class.
?class :hasSimilarityValue ?similarity
bind (?class as ?becauseOf)
}
filter (?x != ?item)
}
group by ?item
在我的第一bind
个子句中,我不仅要绑定变量?p
,还要绑定变量?instance
。另外,添加类似的文本that is why
。
因此第一个绑定应产生以下结果: ?p that is why ?instance
在SPARQL中有可能吗?
请不要在乎数据是否有意义,这只是一个查询,向您显示我的问题
如果我对您的理解正确,那么您只是在寻找concat函数。如前所述,您应该真正浏览SPARQL 1.1标准,至少浏览目录。您无需记住它,但是它将使您对可能发生的事情有一个了解,并向何处寻找一个想法。此外,如果您提供我们可以使用的示例数据,这将非常有帮助,因为它使数据更加清晰弄清楚你要做什么。标题的用语不是特别清楚,问题实际上并没有提供您要完成的目标的示例。只是因为我看过您过去的一些问题,才对您的目标有所了解。无论如何,这里有一些数据:
@prefix : <urn:ex:>
:p :hasSimilarity 0.3 .
:A :hasSimilarity 0.6 .
:a :p :b ; #-- is is related to :b
a :A . #-- and is an :A .
:c :p :b . #-- :c is also related to :b
:d a :A . #-- :d is also an :A .
:e :p :b ; #-- :e is related to :b
a :A . #-- and is also an :A .
这是查询及其结果。您只需使用concat将变量的str形式与适当的字符串连接起来,然后将结果绑定到变量。
prefix : <urn:ex:>
select ?item
(sum(?factor_) as ?factor)
(group_concat(distinct ?reason_; separator=", ") as ?reason)
{
values ?x { :a }
{ ?x ?p ?instance .
?item ?p ?instance .
?p :hasSimilarity ?factor_ .
bind(concat("has common ",str(?p)," value ",str(?instance)) as ?reason_) }
union
{ ?x a ?class.
?item a ?class.
?class :hasSimilarity ?factor_ .
bind(concat("has common class ",str(?class)) as ?reason_)
}
filter (?x != ?item)
}
group by ?item
-----------------------------------------------------------------------------------
| item | factor | reason |
===================================================================================
| :c | 0.3 | "has common urn:ex:p value urn:ex:b" |
| :d | 0.6 | "has common class urn:ex:A" |
| :e | 0.9 | "has common urn:ex:p value urn:ex:b, has common class urn:ex:A" |
-----------------------------------------------------------------------------------
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句