SPARQL - Find objects with the most similar properties

早过忘川 提交于 2019-12-12 18:17:01

问题


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.

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

  1. person2 - 300 of the same friends
  2. person30 - 245 of the same friends
  3. person18 - 16 of the same friends

etc.


回答1:


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.


来源:https://stackoverflow.com/questions/36887950/sparql-find-objects-with-the-most-similar-properties

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!