4

I want to get all the movies without "The Lord of the rings" string in the title. I tried this at the Linked Movie Database SPARQL endpoint, but it doesn't work. What is wrong?

PREFIX m: <http://data.linkedmdb.org/resource/movie/>
SELECT DISTINCT *  WHERE {
   ?film dc:title ?titulo_pelicula.
   FILTER NOT EXISTS { 
      FILTER (regex(?titulo_pelicula, "The Lord of the Rings","i")) . 
   }
}
Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
Ortzi
  • 363
  • 1
  • 6
  • 17
  • In the future, note that "Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself." and "Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results." In this case, "doesn't work" isn't descriptive enough. At first I'd assumed that you weren't getting the results that you expected, but in trying the query, I realized that it generates an error. Please be more descriptive about what – Joshua Taylor Oct 07 '13 at 14:10
  • happened (whether you got an error, or things worked, but with different results than what you expected, etc.). – Joshua Taylor Oct 07 '13 at 14:11

1 Answers1

9

First, you should clarify what you mean by “doesn't work.” At first, I'd assumed that you meant that it doesn't return any results, but when I ran it at the endpoint, I realized that you're actually getting a fairly clear error message about what's wrong:

Parse error: 
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX oddlinker: <http://data.linkedmdb.org/resource/oddlinker/>
PREFIX map: <file:/C:/d2r-server-0.4/mapping.n3#>
PREFIX db: <http://data.linkedmdb.org/resource/>
PREFIX dbpedia: <http://dbpedia.org/property/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX dc: <http://purl.org/dc/terms/>
PREFIX movie: <http://data.linkedmdb.org/resource/movie/>
PREFIX m: <http://data.linkedmdb.org/resource/movie/>
SELECT DISTINCT *  WHERE {
   ?film dc:title ?titulo_pelicula.
   FILTER NOT EXISTS { 
      FILTER (regex(?titulo_pelicula, "The Lord of the Rings","i")) . 
   }
}

Lexical error at line 16, column 14.  Encountered: " " (32), after : "NOT"

You've got a parse error when you get to NOT. The endpoint is based on the original SPARQL Query Language for RDF rather than SPARQL 1.1 Query Language. The original SPARQL doesn't have NOT EXISTS.

This is easy to fix though. First, recognize that filter takes an expression and keeps only those results for which the expression evaluates to true. The filter expression regex(?titulo_pelicula, "The Lord of the Rings","i") returns true when the regular expression matches the title, and you're looking for the cases where it returns false, so you just need to negate it with !. (The ! operator is the same as the XPath function not. The mappings are defined in section 11.3 Operator Mapping of the SPARQL recommendation.) You need a query like this:

PREFIX m: <http://data.linkedmdb.org/resource/movie/>
SELECT DISTINCT *  WHERE {
   ?film dc:title ?titulo_pelicula.
   FILTER (!regex(?titulo_pelicula, "The Lord of the Rings","i")) . 
}

SPARQL results

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
  • 1
    Thank you, but I need to get the films without "The Lorf of the Rings" in the title, exactly the opposite of that query. – Ortzi Oct 07 '13 at 14:14
  • 1
    @Ortzi Oh, sorry! I misread the question. In that case, you just need to use `filter(!regex(...))` (notice the `!`). I've updated my answer. – Joshua Taylor Oct 07 '13 at 14:20