我还添加了下面的代码段。此查询将返回公司 uri 的名称、其名称和母公司。它正在与 DBpedia.org/sparql 一起使用,但不适用于 sparqlwrapper(不返回任何东西;)
query1 = """
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/property/>
PREFIX db: <http://dbpedia.org/resource/>
select distinct ?iri ?name ?parent (concat('[',group_concat(distinct ?location;separator=','),']') as ?location)
{
?iri a dbo:Company ;
rdfs:label ?label ;
foaf:name ?name
OPTIONAL { ?iri dbo:parentCompany ?parent.
filter (!isBlank(?parent)) }
OPTIONAL { ?iri dbo:location ?location.}
filter(regex(?name, "\\btata steel\\b","i" )) .
filter(regex(?label, "\\btata steel\\b","i" ))
}
GROUP BY ?iri ?name ?parent
"""
def get_country_description(query1):
sparql = SPARQLWrapper("http://dbpedia.org/sparql")
sparql.setReturnFormat('json')
sparql.setQuery(query1) # the previous query as a literal string
return sparql.query().convert()