【问题标题】:SPARQL query against DBPedia using Java使用 Java 对 DBPedia 进行 SPARQL 查询
【发布时间】:2016-06-14 01:40:52
【问题描述】:

我想使用 Java 查询 DBPedia。下面是我的代码,它没有返回正确的结果。我想从 [http://dbpedia.org/page/Ibuprofen 页面和标签名称中获取抽象部分。但它只返回http://dbpedia.org/resource/Ibuprofen 11 次。如果可能的话,你能告诉我错误在哪里吗?这是我的代码:

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);
    }
}

【问题讨论】:

  • 如果要获取摘要,那么当然要在查询中选择变量?abstract。你必须在这个变量上调用get,即get("abstract"),或者更好的getLiteral("abstract")。看起来你从某个地方复制了代码?我问是因为我想知道您自己没有发现问题。
  • 请注意,资源的 URI 是 http://dbpedia.org/resource/Ibuprofen,带有“resource”,而不是“page”。在网络浏览器中,您会自动重定向到“页面”版本,但“资源”版本是实际资源。
  • 是的,代码实际上来自另一个来源,只有查询是我的

标签: eclipse sparql semantic-web dbpedia apache-jena


【解决方案1】:

您有多个结果,因为 DBPedia 中有多种语言变体。找出您想要的语言并相应地更改下面的过滤器。您也可以在查询中包含标签模式,而不是通过编程方式进行。根据 ASKW 的评论,您还没有将抽象变量绑定到结果。

基本上你的代码应该是这样的:

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);
    }

【讨论】:

  • 好答案。另外——正如@JoshuaTaylor 经常说的——推荐使用SPARQL 的LANGMATCHES 函数。
  • 是的,我理解我的错误。谢谢大家的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多