簡體   English   中英

我正在使用Jena啟動SPARQL查詢。 該查詢在DBpedia SPARQL端點中有效! 我在查詢驗證器上驗證了查詢,並得到了相同的錯誤

[英]I'm using Jena to launch a SPARQL query. The query works in DBpedia SPARQL endpoint! I verified the query at query validator and got the same error

我需要基於通用屬性找到與eglagaan電影相似的電影。我嘗試了下面的代碼,但是盡管在sparql端點中查詢正常,但我使用jena時卻遇到了以下異常。

在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端點和查詢驗證器中運行良好。 我嘗試了使用Jena在下面的鏈接SPARQL解析錯誤中給出的解決方案,但DBpedia接受了查詢但對我不起作用。

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)

我嘗試刪除由查詢驗證器驗證的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 " +
              " } ";

更改:當我在@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
   }

現在如何將此查詢的輸出傳遞給相似性查詢?

修改后的查詢使用wikiredirects處理@Joshau Taylor建議的拼寫錯誤的電影名稱:

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答案(以防刪除評論):

  • 在查詢中放入一些換行符,然后查看解析器確切指向的位置。 它將是解析錯誤開始的地方。 having (count(?p) > 35)合法SPARQL 1.1的HAVING子句需要( having (count(?p) > 35) –安迪斯
  • { dbpedia:Lagaan }也不正確–昨天{ dbpedia:Lagaan }
  • 我明白了。它在前綴的尖括號后面需要一個空格。 –庫爾蘇姆·法蒂瑪(Kulsum Fatima)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM