如何使用SPARQL查找相似内容

8

我正在考虑使用SPARQL来确定事物之间的概念重叠。

以电影为例(LinkedMDB数据),如果我有一部电影,“The Matrix”,并且我的目标是列出类似于那部电影的电影,我可能会开始执行以下操作:

  • The Matrix
    • 获取类型
    • 获取演员
    • 获取导演
    • 获取位置

然后,使用我在Matrix中识别的属性,我将查询具有这些属性的事物(伪查询)。

SELECT movie, genre, director, location, actors
WHERE {
  genre is action or sci-fi .

  director are the Wachowski brothers .

  location is set in a big city .

  OPTIONAL( actors were in the matrix . )
}

在SPARQL中是否有一些方法可以检查不同节点之间属性的重叠?还是必须像我提出的那样手动完成?

2个回答

13

匹配一些特定属性

听起来你想要的是类似于

select ?similarMovie ?genre ?director ?location ?actor where { 
  values ?movie { <http://.../TheMatrix> }
  ?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

太好了。虽然我还在学习,但您是否有其他使用SPARQL解决同样问题的方法? - Kristian
1
@Kristian 原始问题有点模糊,但我认为如果您已经有一部电影,并且正在寻找其他具有某些给定属性“同意”的电影,则这可能是解决方法。如果您想要更加通用,例如,“找到至少有5个共同属性的其他电影,但我不关心这些属性是什么”,您也可以做到。 - Joshua Taylor
@Kristian 我更新了我的回答,并附上了使用该方法的示例。 - Joshua Taylor
谢谢Joshua,你真是太棒了。顺便说一下,那个第二个答案太酷了。 - Kristian
@Kristian 是的;你要寻找关于?similar的信息,所以它就像寻找?similar的任何其他信息一样,就像我们要求?similar a dbpedia-owl:Film一样,你可以添加rdfs:label ?label(在这种情况下,您可能还想要filter langMatches(lang(?label),"en")(或将“en”替换为其他适当的语言)。 - Joshua Taylor
显示剩余2条评论

0

随着DBpedia中的新前缀,Joshua Taylor的答案将是:

select ?similar (count(?p) as ?similarity) where { 
  values ?movie { dbr:The_Matrix }
  ?similar ?p ?o ; a dbo:Film .
  ?movie   ?p ?o .
}
group by ?similar ?movie
having (count(?p) > 35)
order by desc(?similarity)

SPARQL结果


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