0

I am using rdflib in Python and running SPARQL SELECT queries to get relevant data.

It is very easy to filter data for some criteria using FILTER command like FILTER regex(?pname,'"""+samplepersnalisedexpertise+"""',"i") described below, but if I have to select data that does not matches, then how do we need to use FILTER? I have tried using FILTER (?personuri != '"""+imURI+"""') below but that does not work.

exprtppl=  GraphR.query(""" 
PREFIX foaf: <http://xmlns.com/foaf/0.1/>         
PREFIX owl: <http://www.w3.org/2002/07/owl#>      
PREFIX dc: <http://purl.org/dc/elements/1.1/>         
PREFIX bibo: <http://purl.org/ontology/bibo/>

SELECT ?nname 
{ 
    ?puburi dc:title ?pname. 
    FILTER regex(?pname,'"""+samplepersnalisedexpertise+"""',"i")
    ?personuri foaf:publications ?puburi.
    ?personuri foaf:nick ?nname
    FILTER (?personuri != '"""+imURI+"""')

 }""")

Can anyone of you please help out for solution. Thanks in Advance.

CAMOBAP
  • 5,523
  • 8
  • 58
  • 93
imran
  • 199
  • 3
  • 11
  • Doesn't look wrong to me. What is `imURI` ? Just a string ? Is there a way to use prepared statements in `rdflib` ? `imURI` might just not be well formed, does it have the `<` and `>` around ? – Martin Maillard Nov 14 '12 at 21:33
  • By the way, http://answers.semanticweb.com is a great place for questions about `SPARQL`. – Martin Maillard Nov 14 '12 at 21:34
  • _imURI_ is a variable and its value has been used here like _samplepersnalisedexpertise_ is a variable. Actually we have to look whether _!=_ is a right way of comparison. Thanks for your suggestion about other forum. – imran Nov 14 '12 at 21:40
  • Yes, `!=` is one way to check if two literals are not equal. There is also `!sameTerm(?term1, ?term2)`. But I think your problem is caused because you compare URIs. Maybe try `FILTER (str(?personuri) != '"+imURI+"')` – Martin Maillard Nov 14 '12 at 21:49

1 Answers1

3

You are trying to compare with a URI value, which should not be surrounded by quotes, but by fish hooks:

FILTER(?personuri != <"""+imURI+""">)

By the way, the suggestion @morphyn gives above (using the str() function) will also work, but is less efficient.

Jeen Broekstra
  • 21,642
  • 4
  • 51
  • 73
  • Jeen, plz can you elaborate on "less efficient" ? – Dominique Guardiola Nov 15 '12 at 10:01
  • 3
    It's less efficient because you are doing a conversion to string followed by a lexical comparison, instead of doing a direct value comparison. Most triplestores are good at URI value lookups but relatively bad at string search, and while some SPARQL query engines may be clever enough to see that you're really just comparing identifiers, not all might. – Jeen Broekstra Nov 15 '12 at 16:58