return 使用 dbpedia 查询的完整维基百科页面

return full wikipedia page for a query using dbpedia

我正在使用以下代码检索给定查询的消歧页面。

#disambiguation function
def disambiguation(name, sparql):
  query = "SELECT DISTINCT ?syn WHERE { { ?disPage dbpedia-owl:wikiPageDisambiguates <http://dbpedia.org/resource/"+name+"> . ?disPage dbpedia-owl:wikiPageDisambiguates ?syn . }  UNION {<http://dbpedia.org/resource/"+name+"> dbpedia-owl:wikiPageDisambiguates ?syn . } }"
  sparql.setQuery(query)
  sparql.setReturnFormat(JSON)  
  results_list = sparql.query().convert()
  return results_list

问题:

Is it possible to return the full wikipedia page for every element in the results_list?

简化查询

SELECT DISTINCT ?syn WHERE {
  { ?disPage dbpedia-owl:wikiPageDisambiguates <http://dbpedia.org/resource/"+name+"> .
    ?disPage dbpedia-owl:wikiPageDisambiguates ?syn . }
  UNION
  { <http://dbpedia.org/resource/"+name+"> dbpedia-owl:wikiPageDisambiguates ?syn . }
}

这个查询可以更简洁地写成

select distinct ?syn where {
  ?syn (dbpedia-owl:wikiPageDisambiguates|^dbpedia-owl:wikiPageDisambiguates)* dbpedia:name
}

此查询表示通过 dbpedia-owl:wikiPageDisambiguates 路径查找与 dbpedia:name 相关的所有内容任何方向的属性。

获取维基百科文章URL

I actually wanted to retrieve the whole wikipedia page. For example: When I find a name in a different language I want to Go to the corresponding wikipedia page and retrieve its corresponding page

如果您真的想检索页面(使用其他库或任何您拥有的库),那么您只需要获取维基百科文章 URL。这就是 foaf:isPrimaryTopicOf 属性 的值。例如,如果您查看 Johnny Cash 的 属性 值,您会看到

http://dbpedia.org/resource/Johnny_Cash foaf:isPrimaryTopicOf http://en.wikipedia.org/wiki/Johnny_Cash

基于此,听起来您希望查询更像:

select distinct ?page where {
  ?syn (dbpedia-owl:wikiPageDisambiguates|^dbpedia-owl:wikiPageDisambiguates)* dbpedia:name ;
       foaf:isPrimaryTopicOf ?page

}

那么 ?page 的每个值都应该是您可以下载的维基百科文章 URL。