Sparql - 如何通过选择按属性分组的对象的 MAX 值来构建 RDF? [英] Sparql - How to constuct RDF by selecting MAX values of objects grouped by properties?

查看:45
本文介绍了Sparql - 如何通过选择按属性分组的对象的 MAX 值来构建 RDF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,标题无关紧要,我是法国人,我不知道如何揭露我的问题.我认为最好的解释方式是举例.

sorry for the irrevelant title, I'm French and I didn't know how to expose my problem. I think the best way to explain it is with an example.

我有一些像这样的 RDF 集:

I have some RDF sets like this one:

prefix p: <http://localhost/rdf/>

p:set p:hasTitle p:val1 .
p:set p:hasTitle p:val2 .
p:set p:hasAuthor p:val3 .
p:set p:hasAuthor p:val4 .

p:val1 p:hasValue "Harry Peter" .
p:val1 p:hasScore 0.30 .

p:val2 p:hasValue "Harry Potter" .
p:val2 p:hasScore 0.90 .

p:val3 p:hasValue "J. K. Rowling".
p:val3 p:hasScore 0.90 .

p:val4 p:hasValue "Joanne Rowling" .
p:val4 p:hasScore 0.50 .

我想用 sparql 查询构建另一个图,其中只有每个不同属性的得分最高的值.在这个例子中,查询应该返回这个:

I want to construct another graph with a sparql query, with only the values with the best score for each distinct property. In this example, the query is supposed to return this:

prefix p: <http://localhost/rdf/>

p:set p:hasTitle p:val2 .
p:set p:hasAuthor p:val3 .

p:val2 p:hasValue "Harry Potter" .
p:val2 p:hasScore 0.90 .

p:val3 p:hasValue "J. K. Rowling" .
p:val3 p:hasScore 0.90 .

现在我已经尝试过这样的事情:

For now I have tried something like this:

PREFIX p:    <http://localhost/rdf/>

CONSTRUCT {
    p:root p:hasSameAsSet ?saSet .
    ?saSet ?prop ?bestVal .
    ?bestVal ?p ?v
} 

WHERE { 
    ?s p:hasSameAsSet ?saSet .
    ?saSet ?prop ?val .
    ?bestVal ?p ?v .
    ?bestVal p:hasQualityScore ?m
    {
        SELECT (MAX(?score) AS ?m)
        WHERE { 
            ?val p:hasQualityScore ?score 
        } GROUP BY ?prop
    }    
}

我发现了 Sparql,但我知道我遗漏了重要的东西.我希望有人可以帮助我,非常感谢!如果我的问题不清楚,我可以尝试更好地解释它.不要担心你的答案,我更擅长阅读而不是写作;)

I'm discovering Sparql and I know I'm missing important things. I hope someone can help me, thank you very much ! If my question isn't clear, I can try to explain it better. Don't worry for your answers, I'm better at reading than writing ;)

推荐答案

AKSW 的评论很到位.您的查询非常接近,但首先执行子查询,因此它需要足够的信息来正确分组,并且您还需要从其中投影变量,以便您与外部查询结果进行连接.

AKSW's comment is spot on. Your query is very close as is, but the subquery is executed first, so it needs enough information get the grouping right, and you also need to project the variables from within it that will let you do the join with the outer query results.

例如,这样的查询会为您提供每个属性的最大值:

E.g., a query like this gets you the maximal value for each property:

  select ?prop (max(?val_) as ?val) {
    ?sub ?prop ?val_
  }
  group by ?prop

然后你只需要将它嵌套在一个外部查询中,该查询为每个属性和最大值找到拥有它的主题:

Then you just need to nest that within an outer query that finds, for each property and max value, the subject that had it:

select ?sub ?prop ?val {
    ?sub ?prop ?val
    { select ?prop (max(?val_) as ?val) {
        ?sub ?prop ?val_
      }
      group by ?prop
    }
}

(请注意,?sub 可能有多个具有最大值的值.)向外部查询添加您需要的任何额外信息,然后将其转换为结构 查询应该不难,考虑到您在现有查询中已经获得的内容.

(Note that there could be multiple values of ?sub that have the maximal value.) Adding any extra information that you need to the outer query and then turning it into a constuct query shouldn't be hard, given what you've already got in your existing query.

这篇关于Sparql - 如何通过选择按属性分组的对象的 MAX 值来构建 RDF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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