gpt4 book ai didi

java - 使用 sparql、java、Jena API 查询 DBPedia 本体

转载 作者:行者123 更新时间:2023-11-29 03:10:36 36 4
gpt4 key购买 nike

我正在尝试使用来自 javacode 的 sparql 查询 DBPedia对于某些查询,它可以完美地工作,而对于另一个它不起作用。我认为我的查询没有错误,因为我已经在 DBPedia aparql 端点中对其进行了测试。这是我的 Java 代码:

package ja1;

import com.hp.hpl.jena.query.*;
import com.hp.hpl.jena.rdf.model.*;
import com.hp.hpl.jena.util.*;

public class Q_DBP_Online {

public static void main(String[]args)
{
sparqlTest();
}

public static void sparqlTest()
{
/*String queryString = "SELECT ?o WHERE {"+
"?s ?p ?o ."+
"} LIMIT 10";*/
String str="Obama";
String queryString = "PREFIX pr:<http://xmlns.com/foaf/0.1/>" +
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"+
"SELECT DISTINCT ?s ?label WHERE {" +
"?s rdfs:label ?label . "+
"?s a pr:Person."+
"FILTER (lang(?label) = 'en'). "+
"?label bif:contains"+str+" ."+
"}";

Query query = QueryFactory.create(queryString);
QueryExecution qexec = QueryExecutionFactory.sparqlService("http://dbpedia.org/sparql", query);
try
{
ResultSet results = qexec.execSelect();
while(results.hasNext()){
QuerySolution soln = results.nextSolution();
//Literal name = soln.getLiteral("x");
System.out.println(soln);
}
}
finally{
qexec.close();
}

}
}

因此第一个被注释的查询运行完美,第二个查询没有运行,我在 netbeans 中收到此消息:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/home/rodwan/Desktop/Th_Pr/apache-jena-2.12.1/lib/slf4j-log4j12-1.7.6.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/home/rodwan/Desktop/Th_Pr/pellet-2.3.1/lib/jena/slf4j-log4j12-1.6.4.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]

Exception in thread "main" com.hp.hpl.jena.query.QueryParseException: Encountered " "(" "( "" at line 1, column 169.
Was expecting one of:
"values" ...
"graph" ...
"optional" ...
"minus" ...
"bind" ...
"service" ...
"filter" ...
"{" ...
"}" ...
";" ...
"," ...
"." ...

at com.hp.hpl.jena.sparql.lang.ParserSPARQL11.perform(ParserSPARQL11.java:102)
at com.hp.hpl.jena.sparql.lang.ParserSPARQL11.parse$(ParserSPARQL11.java:53)
at com.hp.hpl.jena.sparql.lang.SPARQLParser.parse(SPARQLParser.java:37)
at com.hp.hpl.jena.query.QueryFactory.parse(QueryFactory.java:148)
at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:80)
at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:53)
at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:41)
at ja1.Q_DBP_Online.sparqlTest(Q_DBP_Online.java:38)
at ja1.Q_DBP_Online.main(Q_DBP_Online.java:18)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)

----------------------------------

最佳答案

我试过你的例子,但似乎缺少一些空格。以下是我在 DBPedia 上的工作:

PREFIX pr:<http://xmlns.com/foaf/0.1/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT DISTINCT ?s ?label WHERE {?s rdfs:label ?label . ?s a pr:Person . FILTER (lang(?label) = 'en') . ?label bif:contains "Obama" .}

Java 的翻译看起来像这样:

String queryString = "PREFIX pr:<http://xmlns.com/foaf/0.1/>\n" +
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n"+
"SELECT DISTINCT ?s ?label WHERE {" + "?s rdfs:label ?label . "+
"?s a pr:Person . "+
"FILTER (lang(?label) = 'en') . "+
"?label bif:contains \""+str+"\" ."+
"}";

希望这对您有所帮助。

关于java - 使用 sparql、java、Jena API 查询 DBPedia 本体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29592241/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com