SPARQL-查找具有最相似属性的对象 [英] SPARQL - Find objects with the most similar properties

查看:58
本文介绍了SPARQL-查找具有最相似属性的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们说有一个RDF数据库,每个人都有许多三元组来定义此人的朋友('person' x:hasFriend 'otherPerson'如此之多).如何找到最相似的朋友?我是SPARQL的新手,这似乎是一个非常复杂的查询.

Lets say there is a RDF DB of people and each of these people has many triples defining this person's friends (so many of 'person' x:hasFriend 'otherPerson'). How can I find people who have the most similar friends? I'm a novice at SPARQL and this seems like a really complex query.

基本上,结果是从最相似的朋友列表的人(到查询中指定的人)开始的人员列表,然后再向下到最不相似的朋友列表的人的列表.

Basically, the results would be a list of people starting from the ones with the most similar friends list (to the person specified in the query) and then going down the list to the people with the least similar friends list.

所以可以说我在此查询中搜索了person1,结果将类似于:

So lets say I search this query for person1, the results would be something like:

  1. person2-300个相同的朋友
  2. person30-245个相同的朋友
  3. person18-16个相同的朋友
  1. person2 - 300 of the same friends
  2. person30 - 245 of the same friends
  3. person18 - 16 of the same friends

推荐答案

如果您在我的回答中采用的方法是

If you adapt the approach in my answer to How to find similar content using SPARQL (of which this might be considered a duplicate), you'd end up with something like:

select ?otherPerson (count(?friend) as ?numFriends) where { 
  :person1 :hasFriend ?friend .           #-- person1 has a friend, ?friend .
  ?otherPerson :hasFriend ?friend .       #-- so does ?otherPerson .
}
group by ?otherPerson       #-- One result row per ?otherPerson, 
order by desc(?numFriends)  #-- ordered by number of common friends.

如果确实愿意,可以使用反向属性路径使查询模式更短:

If you really want to, you can use a reverse property path to make the query pattern a little shorter:

select ?otherPerson (count(?friend) as ?numFriends) where { 
  #-- some ?friend is a friend of both :person1 and ?otherPerson .
  ?friend ^:hasFriend :person1, ?otherPerson .
}
group by ?otherPerson       #-- One result row per ?otherPerson, 
order by desc(?numFriends)  #-- ordered by number of common friends.

这篇关于SPARQL-查找具有最相似属性的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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