【问题标题】:SPARQL for direct dbpedia resource OR wikiPageRedirects用于直接 dbpedia 资源或 wikiPageRedirects 的 SPARQL
【发布时间】:2015-07-03 15:31:36
【问题描述】:

我正在尝试按国家/地区名称从 dbpedia 查询数据。我希望它找到它是否有该国家的资源或通过它在 wikiPageRedirects 中的存在。这是一个工作版本:

PREFIX res: <http://dbpedia.org/resource/>
PREFIX ont: <http://dbpedia.org/ontology/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

SELECT ?country ?capital ?label 
WHERE {
    { res:Dominion_of_Canada ont:capital ?capital .
    ?capital rdfs:label ?label }
UNION
    { res:Dominion_of_Canada ont:wikiPageRedirects ?country .
    ?country ont:capital ?capital .
    ?capital rdfs:label ?label }

FILTER (lang(?label) = "en") 
} 

我想(如果可能的话)排除 ?country。是否可以将资源分配给变量,使 SPARQL 查询如下所示?

SELECT ?country ?capital ?label
WHERE {
{ ?country EXISTS res:Dominion_of_Canada } # to get the idea across
UNION
{ res:Dominion_of_Canada ont:wikiPageRedirects ?country }

?country ont:capital ?capital .
?capital rdfs:label ?label .
FILTER (lang(?label) = "en")
}

与以往一样,速度也很重要。如果资源存在,那么最好跳过在 wikiPageRedirects 上的搜索。

【问题讨论】:

  • stackoverflow.com/questions/23871225/… 的答案有帮助吗?
  • 谢谢,约书亚。查询的那部分已经在工作。我在上面的主要问题中添加了更多细节。
  • 我在那里看不到查询...,但将代码作为问题更新发布通常更容易。
  • “如果该资源存在,我希望它使用 res:France 作为变量 ?country。” 关于“如果该资源存在”的部分并不完全定义明确。 URI res:France 存在,它只是一个常量值,就像一个数字存在,或者一个字符串存在一样。我认为您的意思是(如果我有误解,请纠正我),如果 res:France 具有您要查找的属性,您想使用它,但除此之外,请使用 res:France 重定向到的页面。我认为我最初链接到的问题可能会有所帮助。
  • 嘿,你反应快!我的编辑现已完成。我希望这更清楚。在这一点上,我不关心任何品质的存在——只是找到我正在寻找的实际 dbpedia 资源。上面的查询有效,但我想知道是否可以通过这种方式分解 ?capital 和 ?label 行。

标签: sparql dbpedia


【解决方案1】:

检查资源是否“存在”有点模糊,因为 IRI 只是常量数据。问题实际上是 DBpedia 是否包含有关特定资源的任何三元组。在您的情况下,您想知道它是否重定向到其他任何东西,或者它是否具有自己的属性。 dbpedia:France dbpedia-owl:wikiPageRedirects* ?country 形式的属性路径实际上可能是最好的方法。如果没有重定向链接,那么 ?countrydbpedia:France,如果有,那么 ?country 是重定向的值. “检查”的唯一方法是寻找那些三元组。我认为这意味着您最终会得到这样的结果(类似于我对another question involving redirects 的回答中显示的内容):

select ?country ?anthem ?author {
  #-- The only way to really "check" that the resource 
  #-- "exists" and is not a redirect, is by checking 
  #-- whether it has any redirect links.  If it doesn't,
  #-- then ?country is dbpedia-owl:France, like you want
  #-- and if it does, then then you want to follow them.
  dbpedia:France dbpedia-owl:wikiPageRedirects* ?country .

  #-- I'm using anthem and author here because 
  #-- it doesn't look like there was reliable information
  #-- about the capital.
  ?country dbpedia-owl:anthem ?anthem .                  
  ?anthem dbpprop:author ?author .
}

SPARQL results

【讨论】:

  • 该死的!这样做了,并且大大简化了查询。 * 代表什么? (你是对的 - 其他答案是相似的,虽然我没有看到它是如何工作的)
  • @Bonjiro * 是 SPARQL 1.1 中支持的 property paths 之一。如果存在从 a 到 b 的 path 包含零次或多次出现的 p,则模式 a p* b 匹配。如果路径长度为零,则意味着 a 和 b 必须相同。例如,x hasChild* ?y 会将?y 绑定到x 以及x 的所有后代。
  • 非常好,非常有用。非常感谢你,约书亚!
【解决方案2】:

这个呢?

PREFIX dbr: <http://dbpedia.org/resource/>

select dbr:France ?capital ?label where {
  {dbr:France a dbpedia-owl:Country.
   dbr:France dbpedia-owl:capital ?capital .
   ?capital rdfs:label ?label .
  }

 union {dbr:France dbpedia-owl:wikiPageRedirects ?redirectPage.
        ?redirectPage dbpedia-owl:capital ?capital.
        ?capital rdfs:label ?label .     
 }
}

Result

【讨论】:

  • 嗨,阿里。我澄清了这个问题。问题是是否可以通过将资源分配给变量(如果存在)来排除 ?country 部分。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多