如何使用SPARQL查找类似的内容

Kri*_*ian 5 rdf semantic-web sparql linkedmdb

我正在尝试使用SPARQL识别事物之间的概念重叠.

以电影为例(LinkedMDB数据),如果我有一部电影,"The 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 . )
}
Run Code Online (Sandbox Code Playgroud)

SPARQL中有什么东西可以让我检查不同节点之间属性的重叠吗?或者必须像我提议的那样手动完成吗?

Jos*_*lor 11

匹配一些特定的属性

这听起来像你要求的东西

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 .
}
Run Code Online (Sandbox Code Playgroud)

它使用向后路径表示法^和对象列表使其比以下更短:

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 .
  }
}
Run Code Online (Sandbox Code Playgroud)

例如,使用DBpedia,我们可以获得与The Matrix具有相同经销商和电影摄影师的其他电影:

select ?similar ?cinematographer ?distributor where {
  values ?movie { dbpedia:The_Matrix }
  ?cinematographer ^dbpprop:cinematography ?movie, ?similar .
  ?distributor ^dbpprop:distributor ?movie, ?similar .
}
limit 10
Run Code Online (Sandbox Code Playgroud)

SPARQL结果

结果都在同一个特许经营范围内; 你得到:黑客帝国,矩阵重装上阵,矩阵革命,黑客帝国(特许经营)和终极矩阵收藏.

匹配至少一些属性

也可以要求至少具有一些共同属性的东西.两件事物在被认为是相似之前需要有多少属性,这显然是主观的,取决于具体的数据,需要一些实验.例如,我们可以要求DBpedia上的Films具有至少35个与Matrix相同的属性,并带有如下查询:

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
Run Code Online (Sandbox Code Playgroud)

SPARQL结果

这给了13部电影(包括黑客和其他电影在特许经营中):

  • V for Vendetta(电影)
  • 矩阵
  • 邮差(电影)
  • 行政决定
  • 入侵(电影)
  • 拆迁人(电影)
  • 黑客帝国(特许经营)
  • 重新加载矩阵
  • Freejack
  • 退出伤口
  • 矩阵革命
  • 爆发(电影)
  • 速度赛车(电影)

使用这种方法,您甚至可以使用常用属性的数量作为相似性的度量.例如:

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)
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)