使用可选属性获取dbpedia结果 [英] Getting dbpedia results with optional properties
问题描述
我正在构建一个显示有关给定文本实体信息的应用程序。我正在使用sparqlwrapper库供Python查询DBpedia。当我得到一个 Person
实体时,我使用以下代码:
I'm building an app which shows information about entities of a given text. I'm using a sparqlwrapper library for Python to query DBpedia. I'm using the following code when I get a Person
entity:
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页面中缺少字段时,结果将不返回任何内容。很难知道给定类型的所有实体中都存在哪些属性,因此,我想定义一些所需的属性,然后将其显示出来。我试过用类似这样的查询:
The problem with this code is that when a field is missing in the DBpedia page, the results return nothing. It's difficult to know which properties are present in all the entities of a given type, so I'd like to define some desirable properties and then get the ones present. I tried querying with something like:
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
,所以我最终只得到了摘要
信息。我也想获得 areaTotal
和州长
。
In this case, New York doesn't have a birthPlace
, so I end up getting only the abstract
information. I'd like to get areaTotal
and governor
too.
推荐答案
entire 可选块匹配或不匹配。如果您想可选地匹配一些不同的东西,则需要多个可选块,例如
The entire optional block either matches or it doesn't. If you want to optionally match a few different things, you need multiple optional blocks, as in
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')
}
这篇关于使用可选属性获取dbpedia结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!