gpt4 book ai didi

java - 我正在使用 Jena 启动 SPARQL 查询。该查询在 DBpedia SPARQL 端点中有效!我在查询 validator 验证了查询并得到了相同的错误

转载 作者:行者123 更新时间:2023-12-01 12:00:31 25 4
gpt4 key购买 nike

我需要根据共同属性找到与电影相似的电影,例如 lagaan。我尝试了下面的代码,但当我使用 jena 时,我收到以下异常,尽管查询在 sparql 端点中工作正常。

sparql 端点中的输出:

similar movies                                  similarity score 
http://dbpedia.org/resource/Lagaan 177
http://dbpedia.org/resource/Jodhaa_Akbar 44
http://dbpedia.org/resource/Jaane_Tu..._Ya_Jaane_Na 42
http://dbpedia.org/resource/Swades 42
http://dbpedia.org/resource/Rangeela_(film) 40
http://dbpedia.org/resource/Dil_Ne_Jise_Apna_Kahaa 38
http://dbpedia.org/resource/Love_You_Hamesha 37
http://dbpedia.org/resource/Sholay 37
http://dbpedia.org/resource/Kannathil_Muthamittal 36
http://dbpedia.org/resource/Andaaz 36
http://dbpedia.org/resource/Jaan-E-Mann 36
http://dbpedia.org/resource/Sarfarosh 36
http://dbpedia.org/resource/Saathiya_(film) 36
http://dbpedia.org/resource/Sillunu_Oru_Kaadhal 36
http://dbpedia.org/resource/Doli_Saja_Ke_Rakhna 36
http://dbpedia.org/resource/Dil_Se.. 36
http://dbpedia.org/resource/Rang_De_Basanti 36
http://dbpedia.org/resource/Lage_Raho_Munna_Bhai 36
http://dbpedia.org/resource/Ishq_Vishk 36

对于我尝试过的任何其他查询,尽管该查询在 sparql 端点和查询 validator 中工作正常,但我都会遇到相同的异常错误。我尝试了以下链接 SPARQL parse error with Jena, but DBpedia accepts the query 中给出的解决方案但这对我不起作用。

import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;

public class Dbpedia {

public static void main(String[] args) {
// TODO Auto-generated method stub

String service="http://dbpedia.org/sparql";
String query= " PREFIX dbpedia: <http://dbpedia.org/resource/> "
+ "PREFIX dbpedia-owl: <http://dbpedia.org/ontology/> "
+ "select ?similar (count(?p) as ?similarity) "
+ "where { values ?movie { dbpedia:Lagaan }"
+ " ?similar ?p ?o ; a dbpedia-owl:Film . "
+ "?movie ?p ?o .} group by "
+ "?similar ?movie having count(?p) > 35 order by desc(?similarity)";
QueryExecution e=QueryExecutionFactory.sparqlService(service, query);
ResultSet rs=e.execSelect();
while (rs.hasNext()) {
QuerySolution qs=rs.nextSolution();
System.out.println(qs);
}
}

}

错误:

Exception in thread "main" com.hp.hpl.jena.query.QueryParseException: Encountered " ">" "> "" at line 1, column 350.
Was expecting one of:
<EOF>
<IRIref> ...
<PNAME_NS> ...
<PNAME_LN> ...
"limit" ...
"offset" ...
"order" ...
"values" ...
"exists" ...
"not" ...
"count" ...
"min" ...
"max" ...
"sum" ...
"avg" ...
"sample" ...
"group_concat" ...
"bound" ...
"coalesce" ...
"if" ...
"bnode" ...
"iri" ...
"uri" ...
"str" ...
"strlang" ...
"strdt" ...
"datatype" ...
"lang" ...
"langmatches" ...
"isURI" ...
"isIRI" ...
"isBlank" ...
"isLiteral" ...
"isNumeric" ...
"regex" ...
"sameTerm" ...
"RAND" ...
"ABS" ...
"CEIL" ...
"FLOOR" ...
"ROUND" ...
"CONCAT" ...
"SUBSTR" ...
"STRLEN" ...
"REPLACE" ...
"UCASE" ...
"LCASE" ...
"ENCODE_FOR_URI" ...
"CONTAINS" ...
"STRSTARTS" ...
"STRENDS" ...
"STRBEFORE" ...
"STRAFTER" ...
"YEAR" ...
"MONTH" ...
"DAY" ...
"HOURS" ...
"MINUTES" ...
"SECONDS" ...
"TIMEZONE" ...
"TZ" ...
"NOW" ...
"UUID" ...
"STRUUID" ...
"MD5" ...
"SHA1" ...
"SHA256" ...
"SHA384" ...
"SHA512" ...
"(" ...

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 com.hp.hpl.jena.query.QueryExecutionFactory.sparqlService(QueryExecutionFactory.java:311)
at com.hp.hpl.jena.query.QueryExecutionFactory.sparqlService(QueryExecutionFactory.java:298)
at com.wiki.Dbpedia.main(Dbpedia.java:22)

我尝试删除由查询 validator 验证的 group by 子句,并在 sparql 端点处提供一些输出,但当我在 eclipse 中运行它时再次出现相同的异常

" PREFIX  dbpedia-owl: <http://dbpedia.org/ontology/> " +
" PREFIX dbpedia: <http://dbpedia.org/resource/> " +
" SELECT ?similar " +
" WHERE " +
" { VALUES ?movie { dbpedia:Lagaan } " +
" ?similar ?p ?o ."+
" ?similar <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> dbpedia-owl:Film ." +
" ?movie ?p ?o " +
" } ";

变化:` 当我将 () 添加到having时,查询 validator 返回以下查询输出,如@AndyS所述

1 PREFIX  dbpedia-owl: <http://dbpedia.org/ontology/>
2 PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
3
4 SELECT ?similar (count(?p) AS ?similarity)
5 WHERE
6 { VALUES ?movie { <http://dbpedia.org/resource/Lagaan> }
7 ?similar ?p ?o .
8 ?similar rdf:type dbpedia-owl:Film .
9 ?movie ?p ?o
10 }
11 GROUP BY ?similar ?movie
12 HAVING ( count(?p) > 35 )
13 ORDER BY DESC(?similarity)

我在 Eclipse 中将查询更改为以下,但再次出现相同的错误。

String query= "PREFIX  dbpedia-owl: <http://dbpedia.org/ontology/>"+
"PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>"+
"SELECT ?similar (count(?p) AS ?similarity)" +
"WHERE" +
"{ VALUES ?movie { <http://dbpedia.org/resource/Lagaan> }"+
"?similar ?p ?o ."+
"?similar rdf:type dbpedia-owl:Film ."+
"?movie ?p ?o"+
"}"+
"GROUP BY ?similar ?movie"+
"HAVING ( count(?p) > 35 )"+
"ORDER BY DESC(?similarity)";

更正的查询:

String query="PREFIX dbpprop: <http://dbpedia.org/property/> "
+ " PREFIX dbpedia: <http://dbpedia.org/resource/> "
+ "PREFIX dbpedia-owl: <http://dbpedia.org/ontology/> "
+ "select ?similar (count(?p) as ?similarity) "
+ "where { values ?movie { <http://dbpedia.org/resource/Lagaan> }"
+ " ?similar ?p ?o ; a dbpedia-owl:Film . "
+ "?movie ?p ?o .} group by "
+ "?similar ?movie having(count(?p) > 35) order by desc(?similarity)";

查找带有电影名称的电影链接的新查询:

prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix dbpedia-owl: <http://dbpedia.org/ontology/>
SELECT *WHERE
{
{
select distinct ?film where {
?film a dbpedia-owl:Film .
?film rdfs:label ?label .
filter regex( str(?label), "Lagaan", "i")
}
limit 10
}

现在如何将此查询的输出传递给相似性查询?

按照 @Joshau Taylor 的建议,使用 wikiredirects 修改查询来处理拼写错误的电影名称:

PREFIX  rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
SELECT ?s ?other (count(*) AS ?similarity)
WHERE
{
{
SELECT ?s WHERE {
{ ?s rdfs:label "Veer zara"@en .
?s <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> owl:Thing
}
UNION
{ ?altName rdfs:label "Veer zara"@en .
?altName dbo:wikiPageRedirects ?s
}
}
}
?s ?p ?o .
?other <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> dbo:Film .
?other ?p ?o
}
GROUP BY ?s ?other
HAVING ( count(?p) > 25 )
ORDER BY DESC(?similarity)

最佳答案

看来这个问题已经在评论中解决了。这是为访问者提供的社区 wiki 答案(以防评论被删除):

  • Put some newlines in the query and see exactly where parser referes to. It will be the place the parse error starts. HAVING clause needs () for legal SPARQL 1.1 having (count(?p) > 35). – AndyS
  • { dbpedia:Lagaan } isn't right either – AndyS yesterday
  • i got it.It needed a space after the angular bracket in the prefix. – Kulsum Fatima

关于java - 我正在使用 Jena 启动 SPARQL 查询。该查询在 DBpedia SPARQL 端点中有效!我在查询 validator 验证了查询并得到了相同的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28009584/

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