SPARQL - 查找具有最相似属性的对象

3
假设有一个人的RDF数据库,每个人都有很多三元组来定义这个人的朋友(许多'person' x:hasFriend 'otherPerson')。如何找到拥有最相似朋友的人?我是SPARQL的新手,这似乎是一个非常复杂的查询。
基本上,结果将是一个人的列表,从具有最相似朋友列表的人开始(与查询中指定的人),然后向下列出拥有最不相似朋友列表的人。
所以,假设我搜索person1这个查询,结果将会是:
  1. person2 - 有300个相同的朋友
  2. person30 - 有245个相同的朋友
  3. person18 - 有16个相同的朋友
等等。

我认为如何使用SPARQL查找相似内容的答案可能会对此提供答案。答案中的最后一个SPARQL查询“使用共同属性的数量作为相似度的度量”。 - Joshua Taylor
1个回答

4

如果您采用我在如何使用SPARQL查找相似内容中的答案所述的方法(此问题可能被认为是重复的),您最终将得到以下结果:

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.

如果你真的想这样做,你可以使用反向属性路径使查询模式变得更简短:

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.

非常感谢。是的,我尝试过在Google上寻求帮助,但由于不知道该问什么,所以什么都没找到。 - Fabis
@Fabis 是的,有些东西并不总是非常难,但如果没有一个单一的术语来描述它们,那么它们就很难搜索。 - Joshua Taylor

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接