Search code examples
pythonsparqlsemantic-webdbpediasparqlwrapper

Sparql query on DbPedia return not repeated objects


I'm trying to execute this Sparql query on the DBPedia but it seems to ignore the DISTINCT:

SELECT DISTINCT ?source ?title ?content ?topic
            WHERE {
                ?source rdfs:label ?title ;
                <http://dbpedia.org/ontology/abstract> ?content ;
                foaf:isPrimaryTopicOf ?topic .
                ?title bif:contains "php" .
}

In fact if you try to run the query the result is something like this:

I am running the query from a python file with this code returning a json:

query_rdf = ""
query_rdf += '''
              SELECT DISTINCT ?source ?title ?content ?topic
                WHERE {
                        ?source rdfs:label ?title ;
                        <http://dbpedia.org/ontology/abstract> ?content ;
                        foaf:isPrimaryTopicOf ?topic .
                        ?title bif:contains "php" . 
                }
        '''      
__3store = "http://dbpedia.org/sparql"       
sparql = SPARQLWrapper (__3store,returnFormat="json")
sparql.setQuery(query_rdf)
result = sparql.query().convert()
print json.dumps(result, separators=(',',':'))

Solution

  • The DISTINCT keyword means that each row, as a whole, is distinct from the rest. In the results you're showing, I see labels and abstracts in English (en), Spanish (es), and Arabic (ar). If you have data like:

    :a rdfs:label "..."@en, "..."@es ;
       dbpedia-owl:abstract "..."@en, "..."@es ;
       foaf:isPrimaryTopicOf :aa .
    

    and you run a query

    select distinct ?source ?label ?abstract ?topic where {
      ?source rdfs:label ?label ;
              dbpedia-owl:abstract ?abstract ;
              foaf:isPrimaryTopicOf ?topic
    }
    

    you will get four rows in your results, because there are four distinct combinations:

    1 source × 2 labels × 2 abstracts × 1 topic = 4 rows

    If you want to restrict the label and abstract to a single language (e.g., English), then you just need to filter the results. E.g.:

    select distinct ?source ?label ?abstract ?topic where {
      ?source rdfs:label ?label ;
              dbpedia-owl:abstract ?abstract ;
              foaf:isPrimaryTopicOf ?topic .
              ?label bif:contains "php" . 
      filter( langMatches(lang(?label),"en")
              && langMatches(lang(?abstract),"en") )
    }
    

    SPARQL results