I am using the following query to get dbpedia categories (i.e. skos:broader|dct:subject
) of a given dbpedia URI.
all_urls = ['http://dbpedia.org/resource/Machine_learning', 'http://dbpedia.org/resource/Category:Machine_learning']
for url in all_urls:
print("------")
print(url)
print("------")
sparql.setQuery("""
SELECT * WHERE {<"""
+url+
""">skos:broader|dct:subject ?resource
}
""")
sparql.setReturnFormat(JSON)
results = sparql.query().convert()
for result in results["results"]["bindings"]:
print('resource ---- ', result['resource']['value'])
The output is:
------
http://dbpedia.org/resource/Machine_learning
------
resource ---- http://dbpedia.org/resource/Category:Cybernetics
resource ---- http://dbpedia.org/resource/Category:Learning
resource ---- http://dbpedia.org/resource/Category:Machine_learning
------
http://dbpedia.org/resource/Category:Machine_learning
------
resource ---- http://dbpedia.org/resource/Category:Artificial_intelligence
resource ---- http://dbpedia.org/resource/Category:Learning
Now I want to check the distance of each category in the output to the top level of dbpedia's category system (according to my current understanding dbc:Contents
is the top element in the skos:broader and dct:subject hierarchy). Is it possible to do it in sparql?
I am happy to provide more details if needed.