0

I need to find movies which are similar to movie for e.g.lagaan based on the common properties.I tried the code below but i am getting the below Exception when i use jena though the query works fine in sparql endpoint.

Output in sparql endpoint:

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

For any other query which i tried i m getting the same exception error though that query is working fine in sparql endpoint and Query validator. I tried the solution given in the below link SPARQL parse error with Jena, but DBpedia accepts the query but is doesnt work for me.

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);
        }
    }

}

ERROR:

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)

I tried removing the group by clause which is validated by query validator and gives some output at sparql endpoint but again same exception when i run it in 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 " +
              " } ";

CHANGES:` query validator returns the following query output when i added () to having as said by @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)

I changed query to following in eclipse but again the same error.

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)";

Corrected Query:

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)";

new query to find movie link with movie name:

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
   }

now how to pass the output of this query to the similarity query?

Modified query using wikiredirects to handle mispelled movie names as suggested by @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)
Community
  • 1
  • 1
Kulsum Fatima
  • 15
  • 1
  • 7
  • 1
    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 Jan 18 '15 at 16:16
  • 1
    { dbpedia:Lagaan } isn't right either – AndyS Jan 18 '15 at 16:19
  • @AndyS Thanks.Yes,adding () to having worked in validator and it doesnt give me error.But same exception in eclipse.And instead of {dbpedia:Lagaan},I tried directly writing the url but it didnt work either. – Kulsum Fatima Jan 19 '15 at 05:29
  • @Andy i got it.It needed a space after the angular bracket in the prefix.Thanks – Kulsum Fatima Jan 19 '15 at 08:37

1 Answers1

1

It looks like this got resolved in the comments. Here's a community wiki answer for the sake of visitors (in case the comments get deleted):

  • 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
Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
  • I had one more query @ Joshua Taylor.I am actually trying to build a social network based movie recommendation system,for which the user profile movies are fetched from Facebook and this initial set of movies are further used to generate movies based on content matching from dbpedia. But some of the movie names in facebook are mispelled e.g:Tare Zamee Par whose actually spelling is "Taare Zameen Par".So, is there a way to get the dbpedia movie link with that mispelled movie input. – Kulsum Fatima Jan 20 '15 at 04:01
  • And how can we get dbpedia movie link from imdb movie id. – Kulsum Fatima Jan 20 '15 at 04:08
  • There's no general way to get 'close matches' for spelling, although you can do a rough type of similar text matching with SPARQL (see: ). However, for common mispellings, you may note that DBpedia contains the triple **[dbpedia:Tare_Zamee_Par dbpedia-owl:wikiPageRedirects dbpedia:Taare_Zameen_Par]**, and those redirects may give you enough of the common mispellings. – Joshua Taylor Jan 20 '15 at 13:37
  • Thanks Joshua Taylor.I vl try doing soo. – Kulsum Fatima Jan 22 '15 at 13:36
  • @JoshuaTaylor..I tried to figure out the query using wikipageRedirects,But unable to write it.Can u pls help me.I also tried using freebase to get the correct spelling,it worked for few spellings. – Kulsum Fatima Feb 02 '15 at 04:17
  • Also roughly i matched the spellings using regex ,and i want to combine the two queries..First one which get the movie dbpedia link from movie name and then pass this link to the next query to get the similar movies.Is it possible to such a query where in output of one is passed as input to other recursively.Thanks – Kulsum Fatima Feb 02 '15 at 04:20
  • If you have a different question, it's probably more appropriate to ask it as a new question. Stack Overflow is designed to help organize information. If people have a similar issue, they won't find it if it's only described on the comments of an unrelated question. – Joshua Taylor Feb 02 '15 at 14:19
  • Oh extremely sorry sir.I Will ask it as a new query.Thanks – Kulsum Fatima Feb 02 '15 at 15:46
  • Yes Sir the idea u gave for mispellings using wikiredirect is accepted by me,But I am unable to write the query:(. – Kulsum Fatima Feb 02 '15 at 16:45
  • I added query with wikiredirects.Can u please help me removing the first movie link in the query and displaying the other movies only.And also how to display the name rather than movie link of those movies.I referred your comment at [stackoverflow.com/questions/21290186/…. Its returning all language rdf labels,but when i use filter its doesnt return anything.Thanks your posts helps me alot.:) @Joshua Taylor – Kulsum Fatima Feb 09 '15 at 04:31