【问题标题】:SPARQL - Find objects with the most similar propertiesSPARQL - 查找具有最相似属性的对象
【发布时间】:2016-04-27 11:04:44
【问题描述】:

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

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

假设我在此查询中搜索person1,结果将类似于:

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

等等

【问题讨论】:

标签: sparql rdf semantic-web linked-data


【解决方案1】:

如果你在我对How to find similar content using 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.

【讨论】:

  • 非常感谢。是的,我尝试用谷歌搜索寻求帮助,但我找不到任何东西,因为我不知道具体要问什么。
  • @Fabis 是的,其中一些东西并不总是超级难,但如果没有一个词来描述它们,它们可能真的很难搜索。
猜你喜欢
  • 1970-01-01
  • 2018-01-08
  • 1970-01-01
  • 2013-08-29
  • 1970-01-01
  • 1970-01-01
  • 2021-10-27
  • 1970-01-01
  • 2014-05-05
相关资源
最近更新 更多