SPARQL正则表达式不匹配

时间:2013-10-07 12:09:39

标签: regex rdf sparql linkedmdb

我希望在标题中没有“指环王”字符串的情况下获得所有电影。我在Linked Movie Database SPARQL endpoint尝试了这个,但它不起作用。有什么问题?

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")) . 
   }
}

1 个答案:

答案 0 :(得分:7)

首先,你应该通过“不起作用”澄清你的意思。首先,我认为你的意思是它没有返回任何结果,但当我在the endpoint运行时,我意识到你实际上得到了一个关于错误的相当明确的错误信息:

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"

到达NOT时,您遇到了解析错误。端点基于原始SPARQL Query Language for RDF而非SPARQL 1.1 Query Language。原始SPARQL没有NOT EXISTS

这很容易修复。首先,要认识到filter采用表达式并仅保留表达式计算结果为true的结果。当正则表达式与标题匹配时,过滤器表达式regex(?titulo_pelicula, "The Lord of the Rings","i")返回true,并且您正在查找返回false的情况,因此您只需要使用!对其进行否定。 (!运算符与XPath函数not相同。映射在SPARQL建议的11.3 Operator Mapping部分中定义。)您需要这样的查询:

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