匹配一些特定属性
听起来你想要的是类似于
select ?similarMovie ?genre ?director ?location ?actor where {
values ?movie { <http:
?genre ^:hasGenre ?movie, ?similarMovie .
?director ^:hasDirectory ?movie, ?similarMovie .
?location ^:hasLocation ?movie, ?similarMovie .
optional { ?actor ^:hasActor ?movie, ?similarMovie .
}
它使用反向路径符号^
和对象列表,使其比以下方式更短:
select ?similarMovie ?genre ?director ?location ?actor where {
values ?movie { <http://.../TheMatrix> }
?movie :hasGenre ?genre .
?movie :hasDirector ?director .
?movie :hasLocation ?location .
?similarMovie :hasGenre ?genre .
?similarMovie :hasDirector ?director .
?similarMovie :hasLocation ?location .
optional {
?movie :hasActor ?actor .
?similarMovie :hasActor ?actor .
}
}
例如,使用DBpedia,我们可以获取与《黑客帝国》有相同发行商和摄影师的其他电影:
select ?similar ?cinematographer ?distributor where {
values ?movie { dbpedia:The_Matrix }
?cinematographer ^dbpprop:cinematography ?movie, ?similar .
?distributor ^dbpprop:distributor ?movie, ?similar .
}
limit 10
SPARQL 结果
结果都在同一系列中,包括:The Matrix,The Matrix Reloaded,The Matrix Revolutions,The Matrix (franchise) 和 The Ultimate Matrix Collection。
至少匹配一定数量的属性
还可以查询具有至少某些相同属性的事物。两个事物需要具有多少个共同属性才应被视为相似显然是主观的,这取决于特定的数据,并需要进行一些实验。例如,我们可以使用以下查询请求DBpedia上与Matrix共有至少35个属性的电影:
select ?similar where {
values ?movie { dbpedia:The_Matrix }
?similar ?p ?o ; a dbpedia-owl:Film .
?movie ?p ?o .
}
group by ?similar ?movie
having count(?p) > 35
SPARQL结果
这提供了13部电影(包括《黑客帝国》系列中的其他电影):
- 刺客聂隐娘
- 黑客帝国
- 邮递员
- 绝密飞行
- 入侵
- 大破解
- 黑客帝国(系列电影)
- 黑客帝国2:重装上阵
- 时间机器1991
- 极限警探
- 黑客帝国3:矩阵革命
- 瘟疫危机
- 极速蜗牛
使用这种方法,您甚至可以将共同属性的数量作为相似性的度量方式。例如:
select ?similar (count(?p) as ?similarity) where {
values ?movie { dbpedia:The_Matrix }
?similar ?p ?o ; a dbpedia-owl:Film .
?movie ?p ?o .
}
group by ?similar ?movie
having count(?p) > 35
order by desc(?similarity)
SPARQL结果
The Matrix 206
The Matrix Revolutions 63
The Matrix Reloaded 60
The Matrix (franchise) 55
Demolition Man (film) 41
Speed Racer (film) 40
V for Vendetta (film) 38
The Invasion (film) 38
The Postman (film) 36
Executive Decision 36
Freejack 36
Exit Wounds 36
Outbreak (film) 36
?similar
的信息,所以它就像寻找?similar
的任何其他信息一样,就像我们要求?similar a dbpedia-owl:Film
一样,你可以添加rdfs:label ?label
(在这种情况下,您可能还想要filter langMatches(lang(?label),"en")
(或将“en”替换为其他适当的语言)。 - Joshua Taylor