1

I am trying get the dbpedia movie link using the movie name in the first query and pass that link in the second query to get the movies similar to this movie.For e.g Lagaan.Now instead of passing the link manually in the second query is there a way to combine the two queries and pass the output of first query as an input to the second query.i.e:the link of the movie lagaan.Also,if the first query gives multiple links eg:if i am searching for Harry potter it will return multiple harry potter series links so,it should handle that case as well.

Query1

prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
        prefix dbpedia-owl: <http://dbpedia.org/ontology/>

          select distinct ?film where  {
          ?film a dbpedia-owl:Film .
          ?film rdfs:label ?label .
          filter regex( str(?label), "Lagaan", "i")
          }
          limit 10

Query 2

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)

Edited query:

  select ?film ?similar (count(?p) as ?similarity) where {

  { 
          select distinct ?film where  {
          ?film a dbpedia-owl:Film .
          ?film rdfs:label ?label .
          filter regex( str(?label), "Lagaan", "i")
          } 
  }

         ?similar ?p ?o ; a dbpedia-owl:Film .
         ?film  ?p ?o .
        }
        group by ?similar ?film
        having count(?p) > 35
        order by desc(?similarity)

corrected query as told by Joshua Taylor

select ?film ?other (count(*) as ?similarity) {
  {
    select ?film where {
      ?film a dbpedia-owl:Film ; rdfs:label ?label .
      filter contains(lcase(?label),"lagaan")
    }
    limit 1
  }
  ?film ?p ?o .
  ?other a dbpedia-owl:Film ; ?p ?o .
}
group by ?film ?other
having count(?p) > 25
order by desc(?similarity)
Kulsum Fatima
  • 15
  • 1
  • 7

1 Answers1

3

is there a way to combine the two queries and pass the output of first query as an input to the second query.

SPARQL 1.1 defines subqueries. The results of inner queries are available to outer queries, so they are "passed" to them. In your case, you would have something along the lines of:

select ?similarMovie (... as ?similarity) where {

  { #-- QUERY 1, find one or more films
    select distinct ?film where {
      #-- ...
    }
  }

  #-- QUERY 2, find films similar to ?film
  #-- ...
}
Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
  • ,I edited the query above,I am getting null when i tried in dbpedia endpoint. – Kulsum Fatima Feb 03 '15 at 03:56
  • 1
    It looks like you might find [my earlier answer](http://stackoverflow.com/a/21290432/1281433) to [How to find similar content using SPARQL](http://stackoverflow.com/q/21290186/1281433) useful. – Joshua Taylor Feb 03 '15 at 04:08
  • That said, I don't know that I see any real reason to use a subquery here; it looks like you could just combine this into one query. – Joshua Taylor Feb 03 '15 at 04:09
  • In fact; now that I look at it, it appears that you're already using the code from that answer. You should generally provide attribution if you're using content from other places; the license for Stack Overflow requires attribution. – Joshua Taylor Feb 03 '15 at 04:18
  • Sorry sir,I am using stackoverflow for first tym so didnt knew about mentionind that.I wil keep this is mind from next time.Thanks u soo mch sir for all ur help. – Kulsum Fatima Feb 03 '15 at 05:33