Gio Bact
Gio Bact

Reputation: 541

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=(',',':'))

Upvotes: 0

Views: 288

Answers (1)

Joshua Taylor
Joshua Taylor

Reputation: 85913

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

Upvotes: 2

Related Questions