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:
person2
- 300 of the same friends
person30
- 245 of the same friends
person18
- 16 of the same friends
etc.
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.