【问题标题】:Getting dbpedia results with optional properties使用可选属性获取 dbpedia 结果
【发布时间】:2013-08-12 15:14:10
【问题描述】:

我正在构建一个应用程序,它显示有关给定文本的实体的信息。我正在使用 Python 的 sparqlwrapper 库来查询 DBpedia。当我获得Person 实体时,我正在使用以下代码:

def get_person_data(einfo):
    data = {}
    try:
        uri = einfo['disambiguated']['dbpedia']

        sparql = SPARQLWrapper("http://dbpedia.org/sparql")
        query = u"""
        SELECT ?birthDate, ?birthName, ?birthPlace
        WHERE { <%s>
                dbpprop:birthDate ?birthDate ;
                dbpprop:birthName ?birthName ;
                dbpprop:birthPlace ?birthPlace
        }
        """ % uri
        sparql.setQuery(query)
        sparql.setReturnFormat(JSON)
        results = sparql.query().convert()

此代码的问题在于,当 DBpedia 页面中缺少某个字段时,结果不会返回任何内容。很难知道给定类型的所有实体中都存在哪些属性,所以我想定义一些理想的属性,然后获取那些存在的属性。我尝试使用以下内容进行查询:

SELECT * WHERE {
  ?x rdfs:label "New York"@en.
  ?x dbpedia-owl:abstract ?abstract.
  OPTIONAL { 
  ?x dbpedia-owl:areaTotal ?areaTotal.
  ?x dbpprop:governor ?governor.
  ?x dbpprop:birthPlace ?birthPlace.
  }
  FILTER (LANG(?abstract) = 'en')
}

在这种情况下,纽约没有birthPlace,所以我最终只获得了abstract 信息。我也想得到areaTotalgovernor

【问题讨论】:

    标签: python sparql dbpedia


    【解决方案1】:

    整个 可选块要么匹配,要么不匹配。如果你想有选择地匹配几个不同的东西,你需要多个可选块,如

    SELECT * WHERE {
      ?x rdfs:label "New York"@en.
      ?x dbpedia-owl:abstract ?abstract.
      OPTIONAL { ?x dbpedia-owl:areaTotal ?areaTotal. }
      OPTIONAL { ?x dbpprop:governor ?governor. }
      OPTIONAL { ?x dbpprop:birthPlace ?birthPlace. }
      FILTER (LANG(?abstract) = 'en')
    }
    

    SPARQL results

    【讨论】:

      猜你喜欢
      • 2011-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多