0

I am going to build a topic hierarchy tree using SPARQL query extracted from DBpedia. How can I filtering no definition concepts from my sparql query?

My currently code is below:

from SPARQLWrapper import SPARQLWrapper, N3
from rdflib.extras.external_graph_libs import rdflib_to_networkx_graph
from pprint import pprint
from rdflib import Graph
import networkx as nx
import copy

# Main concept 
concept = '<http://dbpedia.org/resource/Category:Machine_learning>'

sparql = SPARQLWrapper("http://dbpedia.org/sparql")

sparql.setQuery(f"""
CONSTRUCT {{ ?child skos:broader {concept} .}} 
WHERE {{ ?child skos:broader {concept} .}}
""")

sparql.setReturnFormat(N3)
results = sparql.query().convert()
g_child = Graph()
g_child.parse(data=results, format="n3")

sparql.setQuery(f"""
CONSTRUCT {{ ?child skos:broader {concept} .
             ?grandchild skos:broader ?child .}} 
WHERE {{ ?child skos:broader {concept} .
         ?grandchild skos:broader ?child .}}
""")

sparql.setReturnFormat(N3)
results = sparql.query().convert()
g_grandchild = Graph()
g_grandchild.parse(data=results, format="n3")

I used to try:

sparql.setQuery(f"""
CONSTRUCT {{ ?child skos:broader {concept} .}} 
WHERE {{ ?child skos:broader {concept} .
        FILTER(LANG(?definition) = 'en')}}
""")

sparql.setReturnFormat(N3)
results = sparql.query().convert()
g_child = Graph()
g_child.parse(data=results, format="n3")

and

sparql.setQuery(f"""
CONSTRUCT {{ ?child skos:broader {concept} .
             ?grandchild skos:broader ?child .}} 
WHERE {{ ?child skos:broader {concept} .
         ?grandchild skos:broader ?child .
         FILTER(LANG(?definition) = 'en') }}
""")

sparql.setReturnFormat(N3)
results = sparql.query().convert()
g_grandchild = Graph()
g_grandchild.parse(data=results, format="n3")

However, the graph turns to empty.

Thank you in advance!

BBQ
  • 23
  • 4
  • I don't understand what you're doing here. The variable `?definition` isn't bound to anything, so what should the filter do? so of course it's empty as the filter always evaluates to `false` – UninformedUser Oct 29 '20 at 10:38
  • and then, what is a *"no definition concept"*? – UninformedUser Oct 29 '20 at 10:39
  • @UninformedUser Thank you for your comment! My filter might go wrong way but I cant figure out the correct one. The "no definition concept" means the concept has definition in DBpedia. For example, for the concept "England" its pages in DBpedia has its definition :[link](http://dbpedia.org/page/England) . However, for the concept "Electronic_waste_in_Europe" : [link](http://dbpedia.org/page/Electronic_waste_in_Europe) . Both of them existed in the Entity of concept [link](http://dbpedia.org/page/Category:Electronic_waste_in_Europe) , [link](http://dbpedia.org/page/Category:England) – BBQ Oct 29 '20 at 12:49
  • still not sure what you're asking. Some are DBpedia categories, the other are DBpedia resources. Now, you want to filter what? – UninformedUser Oct 29 '20 at 13:12
  • @UninformedUser Thank you for your reply. I want to filter those concept which do not has definition in DBpedia like [link](http://dbpedia.org/page/Electronic_waste_in_Europe) . It is just an example which is not the result of the sparql query above. – BBQ Oct 29 '20 at 13:43
  • then you have to create the resource URI from the concept URI as both are obviously different: `WHERE { ?child skos:broader BIND (iri(concat("http://dbpedia.org/resource/", strafter(str(?child), str("http://dbpedia.org/resource/Category:")))) as ?newChildIRI) FILTER EXISTS {?newChildIRI ?p ?o} } ` – UninformedUser Oct 29 '20 at 14:36

0 Answers0