Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract triples containing particular substring using SPARQL

Tags:

regex

sparql

I want to extract a triple which contains word say "alice" in its subject. The query I used was:

SELECT ?s ?p ?o  WHERE { ?s ?p ?o .FILTER regex(?s, \"alice\") .}

This doesn't give me any results inspite of have a triple which satisfies this constraint.

On the other hand when I use the same query to extract a triple which contains a word brillant in its object .It returns only one of the 2 possible matches.

The query used is:

SELECT ?s ?p ?o  WHERE { ?s ?p ?o .FILTER regex(?o, \"brillant\") .}

Please let me know where am I going wrong and what is the reason for this behaviour.

like image 318
user2335580 Avatar asked Feb 11 '14 12:02

user2335580


1 Answers

I'll assume that the escapes around the quotation marks are just a remnant from copying and pasting. The first argument to regex must be a literal, but literals cannot be the subjects of triples in RDF, so it's not true that you have data that should match this pattern. What you might have, though, is subjects whose URI contains the string "alice", and you can get the string representation of the URI using the str function. E.g.,

SELECT ?s ?p ?o  WHERE { ?s ?p ?o .FILTER regex(str(?s), "alice") .}

To illustrate, let's use the two values <http://example.org> and "string containing example" and filter as you did in your original query:

select ?x where {
  values ?x { <http://example.org> "string containing example" }
  filter( regex(?x, "exam" ))
}
-------------------------------
| x                           |
===============================
| "string containing example" |
-------------------------------

We only got "string containing example" because the other value wasn't a string, and so wasn't a suitable argument to regex. However, if we add the call to str, then it's the string representation of the URI that regex will consider:

select ?x where {
  values ?x { <http://example.org> "string containing example" }
  filter( regex(str(?x), "exam" ))
}
-------------------------------
| x                           |
===============================
| <http://example.org>        |
| "string containing example" |
-------------------------------
like image 173
Joshua Taylor Avatar answered Nov 02 '22 21:11

Joshua Taylor