Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SPARQL regular expression not match

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")) . 
   }
}
like image 567
Ortzi Avatar asked Feb 16 '23 01:02

Ortzi


1 Answers

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

like image 193
Joshua Taylor Avatar answered Feb 27 '23 22:02

Joshua Taylor