gpt4 book ai didi

带圆括号的 sparql 查询抛出异常

转载 作者:行者123 更新时间:2023-12-04 14:59:53 26 4
gpt4 key购买 nike

我正在尝试为某些人从 DBpedia 中提取标签。我现在部分成功,但我陷入了以下问题。以下代码有效。

public class DbPediaQueryExtractor {
public static void main(String [] args) {
String entity = "Aharon_Barak";
String queryString ="PREFIX dbres: <http://dbpedia.org/resource/> SELECT * WHERE {dbres:"+ entity+ "<http://www.w3.org/2000/01/rdf-schema#label> ?o FILTER (langMatches(lang(?o),\"en\"))}";
//String queryString="select * where { ?instance <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://xmlns.com/foaf/0.1/Person>; <http://www.w3.org/2000/01/rdf-schema#label> ?o FILTER (langMatches(lang(?o),\"en\")) } LIMIT 5000000";
QueryExecution qexec = getResult(queryString);
try {
ResultSet results = qexec.execSelect();
for ( ; results.hasNext(); )
{
QuerySolution soln = results.nextSolution();
System.out.print(soln.get("?o") + "\n");
}
}
finally {
qexec.close();
}
}

public static QueryExecution getResult(String queryString){
Query query = QueryFactory.create(queryString);
//VirtuosoQueryExecution vqe = VirtuosoQueryExecutionFactory.create (sparql, graph);
QueryExecution qexec = QueryExecutionFactory.sparqlService("http://dbpedia.org/sparql", query);
return qexec;
}
}

但是,当实体包含括号时,它不起作用。例如,
String entity = "William_H._Miller_(writer)";

导致此异常:

Exception in thread "main" com.hp.hpl.jena.query.QueryParseException: Encountered " "(" "( "" at line 1, column 86.`



问题是什么?

最佳答案

它需要一些复制和粘贴才能看到到底发生了什么。我建议您在查询中添加换行符以便于阅读。您正在使用的查询是:

PREFIX dbres: <http://dbpedia.org/resource/>
SELECT * WHERE
{
dbres:??? <http://www.w3.org/2000/01/rdf-schema#label> ?o
FILTER (langMatches(lang(?o),"en"))
}

其中 ??? 被字符串 entity 的内容替换。您在这里绝对没有进行输入验证,以确保 entity 的值可以合法粘贴。根据您的问题,听起来 entity 包含 William_H._Miller_(writer) ,所以您得到了查询:
PREFIX dbres: <http://dbpedia.org/resource/>
SELECT * WHERE
{
dbres:William_H._Miller_(writer) <http://www.w3.org/2000/01/rdf-schema#label> ?o
FILTER (langMatches(lang(?o),"en"))
}

您可以将其粘贴到 public DBpedia endpoint 中,您将收到类似的解析错误消息:
Virtuoso 37000 Error SP030: SPARQL compiler, line 6: syntax error at 'writer' before ')'

SPARQL query:
define sql:big-data-const 0
#output-format:text/html
define sql:signal-void-variables 1 define input:default-graph-uri <http://dbpedia.org> PREFIX dbres: <http://dbpedia.org/resource/>
SELECT * WHERE
{
dbres:William_H._Miller_(writer) <http://www.w3.org/2000/01/rdf-schema#label> ?o
FILTER (langMatches(lang(?o),"en"))
}

比使用错误查询命中 DBpedia 的端点更好,您还可以使用 the SPARQL query validator 报告该查询:

Syntax error: Lexical error at line 4, column 34. Encountered: ")" (41), after : "writer"



在 Jena 中,您可以使用 ParameterizedSparqlString 来避免此类问题。这是您的示例,经过重新设计以使用参数化字符串:

import com.hp.hpl.jena.query.ParameterizedSparqlString;

public class PSSExample {
public static void main( String[] args ) {
// Create a parameterized SPARQL string for the particular query, and add the
// dbres prefix to it, for later use.
final ParameterizedSparqlString queryString = new ParameterizedSparqlString(
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n" +
"SELECT * WHERE\n" +
"{\n" +
" ?entity rdfs:label ?o\n" +
" FILTER (langMatches(lang(?o),\"en\"))\n" +
"}\n"
) {{
setNsPrefix( "dbres", "http://dbpedia.org/resource/" );
}};

// Entity is the same.
final String entity = "William_H._Miller_(writer)";

// Now retrieve the URI for dbres, concatentate it with entity, and use
// it as the value of ?entity in the query.
queryString.setIri( "?entity", queryString.getNsPrefixURI( "dbres" )+entity );

// Show the query.
System.out.println( queryString.toString() );
}
}

输出是:
PREFIX dbres: <http://dbpedia.org/resource/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT * WHERE
{
<http://dbpedia.org/resource/William_H._Miller_(writer)> rdfs:label ?o
FILTER (langMatches(lang(?o),"en"))
}

您可以在公共(public)端点运行此查询并获取 the expected results 。请注意,如果您使用不需要特殊转义的 entity,例如,

final String entity = "George_Washington";

那么查询输出将使用前缀形式:
PREFIX dbres: <http://dbpedia.org/resource/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT * WHERE
{
dbres:George_Washington rdfs:label ?o
FILTER (langMatches(lang(?o),"en"))
}

这非常方便,因为您不必检查您的后缀,即 entity 是否有任何需要转义的字符; Jena 会为您解决这些问题。

关于带圆括号的 sparql 查询抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18232010/

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