0

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=(',',':'))
Gio Bact
  • 541
  • 1
  • 7
  • 23
  • What results are you *trying* to get? The query is working as it should. If there are, e.g., 2 labels (one in each of two different languages) and 2 abstracts (one in each of two different languages) then there are 4 distinct combinations of label and abstract. – Joshua Taylor Feb 10 '15 at 16:40

1 Answers1

2

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

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
  • Yes, but it's the same resource. How can I specify for example to only show results in english, with _@en_ – Gio Bact Feb 10 '15 at 16:37
  • Yeah man!! Exactly what I wanted to do, but I'm very noob with sparql and dbpedia. Thank you very much! – Gio Bact Feb 10 '15 at 16:51