2

I would like to query on DBPedia using Java. Below is my code and it does not return corrrect result.I want to get Abstract part from [http://dbpedia.org/page/Ibuprofen page and label name. but it returns only http://dbpedia.org/resource/Ibuprofen 11 times. If possible could you tell me where is the mistake? This is my code:

import org.apache.jena.query.ParameterizedSparqlString;
import org.apache.jena.query.QueryExecution;
import org.apache.jena.query.QueryExecutionFactory;
import org.apache.jena.query.ResultSet;
import org.apache.jena.query.ResultSetFormatter;
import org.apache.jena.rdf.model.Literal;
import org.apache.jena.rdf.model.ResourceFactory;

public class JavaDBPediaExample {

    public static void main(String[] args) {
        ParameterizedSparqlString qs = new ParameterizedSparqlString(""
                + "prefix rdfs:    <http://www.w3.org/2000/01/rdf-schema#>\n"
                + "PREFIX dbo:     <http://dbpedia.org/ontology/>"
                + "\n"
                + "select ?resource where {\n"
                + "  ?resource rdfs:label ?label.\n"
                + "  ?resource dbo:abstract ?abstract.\n"
                + "}");

        Literal ibuprofen = ResourceFactory.createLangLiteral("Ibuprofen", "en");
        qs.setParam("label", ibuprofen);

        QueryExecution exec = QueryExecutionFactory.sparqlService("http://dbpedia.org/sparql", qs.asQuery());

        ResultSet results = exec.execSelect();

        while (results.hasNext()) {

            System.out.println(results.next().get("resource"));
        }

        ResultSetFormatter.out(results);
    }
}
developer
  • 25
  • 1
  • 11
  • 1
    If you want to get the abstract, then of course you have to select the variable `?abstract` in the query. And you have to call `get` on this variable, i.e. `get("abstract")` or even better `getLiteral("abstract")`. Looks like you copied the code from somewhere? I'm asking because because I was wondering that you didn't see the problem by yourself. – UninformedUser Jun 14 '16 at 04:51
  • Note that the URI for the resource is `http://dbpedia.org/resource/Ibuprofen` with "resource", not "page". In a web browser, you get automatically redirected to the "page" version, but the "resource" version is the actual resource. – Joshua Taylor Jun 14 '16 at 12:09
  • Yes the code was actually from another source, only the query was mine – developer Jun 14 '16 at 23:30

1 Answers1

7

You have multiple results because there are multiple language variants in DBPedia. Work out which language you want and change the filter below accordingly. You can include the label pattern in the query as well instead ok doing it programmatically. As per ASKW's comment, you also haven't bound the abstract variable to the result.

Basically your code should looks something like this:

public static void main(String[] args) {
        ParameterizedSparqlString qs = new ParameterizedSparqlString(""
                + "prefix rdfs:    <http://www.w3.org/2000/01/rdf-schema#>\n"
                + "PREFIX dbo:     <http://dbpedia.org/ontology/>"
                + "\n"
                + "select distinct ?resource ?abstract where {\n"
                + "  ?resource rdfs:label 'Ibuprofen'@en.\n"
                + "  ?resource dbo:abstract ?abstract.\n"
                + "  FILTER (lang(?abstract) = 'en')}");


        QueryExecution exec = QueryExecutionFactory.sparqlService("http://dbpedia.org/sparql", qs.asQuery());

        ResultSet results = exec.execSelect();

        while (results.hasNext()) {

            System.out.println(results.next().get("abstract").toString());
        }

        ResultSetFormatter.out(results);
    }
William Greenly
  • 3,914
  • 20
  • 18