使用VALUES使用Sparql查询DBpedia [英] Query DBpedia with Sparql using VALUES

查看:230
本文介绍了使用VALUES使用Sparql查询DBpedia的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在Java中运行此代码时,出现错误,我认为该错误是由于字符串值"而发生的.我不确定要添加它,但是我是从我在本网站上问的上一个问题的答案中得到这个想法的,

When I run this code in java I am getting error, I think the error occurred because of "String values". I am not sure about adding it but I got this idea from my previous question's answer which I asked in this site Query DBpedia to get abstract for different inputs

    public static void DbpediaResultSparql() { 
    String values = "New York";
    String service = "http://dbpedia.org/sparql";

    String sparqlQueryString2 = "PREFIX  rdfs: <http://www.w3.org/2000/01/rdf-schema#>"+
           "PREFIX  dbpedia-owl: <http://dbpedia.org/ontology/>"+
           "PREFIX  dbpedia: <http://dbpedia.org/resource/>"+

               "SELECT DISTINCT  ?abstract"+
               "WHERE"+
                 "{ _:b0 rdfs:label ?name ."+
                   "_:b0 dbpedia-owl:abstract ?abstract"+
                   "FILTER langMatches(lang(?abstract), 'en')"+
                  "?name { " + values +" @en }"+
                "}" ;                 

    Query query = QueryFactory.create(sparqlQueryString2);
    ARQ.getContext().setTrue(ARQ.useSAX);
    // Executing SPARQL Query and pointing to the DBpedia SPARQL Endpoint
    QueryExecution qexec = QueryExecutionFactory.sparqlService(
            "http://DBpedia.org/sparql", query);
    // Retrieving the SPARQL Query results
    ResultSet results = qexec.execSelect();
    // Iterating over the SPARQL Query results
    while (results.hasNext()) {
        QuerySolution soln = results.nextSolution();
        // Printing DBpedia entries' abstract.
        System.out.println(soln.get("?abstract"));
    }
    qexec.close();
 }

推荐答案

您不会通过类似的代码获得有用的答案

You're not going to get useful answers with code like

"SELECT DISTINCT  ?abstract"+ "WHERE"

"_:b0 dbpedia-owl:abstract ?abstract"+ "FILTER langMatches(lang(?abstract), 'en')"

因为它变成了

SELECT DISTINCT ?abstractWHERE

_:b0 dbpedia-owl:abstract ?abstractFILTER

,并且您不希望使用名为?abstractWHERE?abstractFILTER的变量.

and you don't want variables named ?abstractWHERE or ?abstractFILTER.

这也没有任何意义:

String values = "New York";
"?name { " + values +" @en }"

您最终会得到

?name { New York@en }

我希望您想要的是

values ?name { "New York"@en }

我建议您研究一下 ParameterizedSparqlStrings ,并确保使用终止换行符或至少使用空格,在您的代码中.如果您刚刚打印出查询,则可以将其放入sparql.org的查询验证器中,您会立即发现问题.

I'd suggest you take a look into ParameterizedSparqlStrings, and be sure to put terminating newlines, or at least whitespace, in your code. If you had just printed out the query, you could drop it into sparql.org's query validator and you'd have seen the problem right away.

您可以这样编写查询:

select distinct ?abstract where {
  values ?name { "New York"@en }
  [ rdfs:label ?name ;
    dbpedia-owl:abstract ?abstract ]
  filter langMatches(lang(?abstract),'en')
}

如果?name只有一个值,并且没有选择该变量,则可以在查询中将其写入:

If you only have the one value for the ?name and you're not selecting that variable, you can just write it in the query:

select distinct ?abstract where {
  [ rdfs:label "New York"@en ;
    dbpedia-owl:abstract ?abstract ]
  filter langMatches(lang(?abstract),'en')
}

这篇关于使用VALUES使用Sparql查询DBpedia的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆