假设有一个人的 RDF 数据库,每个人都有许多三元组来定义这个人的朋友(这么多'person' x:hasFriend 'otherPerson'
)。如何找到拥有最相似朋友的人?我是 SPARQL 的新手,这似乎是一个非常复杂的查询。
基本上,结果将是一个人员列表,从具有最相似朋友列表的人(到查询中指定的人)开始,然后沿着列表向下到具有最不相似朋友列表的人。
假设我搜索此查询person1
,结果会是这样的:
-
person2
- 300 个相同的朋友
-
person30
- 245 个相同的朋友
-
person18
- 16 个相同的朋友
etc.
如果您采用我的回答中的方法如何使用 SPARQL 查找相似内容 https://stackoverflow.com/questions/21290186/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.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)