-1

I'm trying to filter out certain properties (e.g dbpprop: ones) from the result of a sparql query. I'm querying dbpedia sparql endoint.

More precisely what I'd like to do is expressed by the following pseudo code :

quantity_of_interest, property, value, property_type = sparqlQuery(query)
if property_type == rdf:Property:
    pass
else:
    return quantity_of_interest, property, value

For now, I filter out the properties afterwards in some python code and I use the following sparql query :

SELECT DISTINCT ?qoi ?property ?value (bif:either(?type=rdf:Property, 0, 1) as ?filter_out) 
WHERE { 
    ?qoi a foaf:Person. ?qoi ?property ?value. 
    OPTIONAL{?property rdf:type ?type }
}

If filter_out == 0 I discard the whole result.

Is there a way I can directly make a sparql query which filters out the whole result whenever ?type == rdf:Property ?

  • possible duplicate of [Exclude results from DBpedia SPARQL query based on URI prefix](http://stackoverflow.com/questions/19044871/exclude-results-from-dbpedia-sparql-query-based-on-uri-prefix) – Joshua Taylor Feb 24 '14 at 15:35
  • The linked duplicate excludes properties based on a prefix, but the same approach works here, only using an even simpler test. You just need to use `filter( ?type != rdf:Property )`. – Joshua Taylor Feb 24 '14 at 15:36
  • Oh, I misread the question a little bit; the answer to the possible duplicate actually goes father than you need, but the sample code in the _question_ shows how to do what you want to do. – Joshua Taylor Feb 24 '14 at 15:40
  • I don't think so, because I'd like to keep properties which have no rdf:type (such as foaf properties) – Raphael Lopez Kaufman Feb 24 '14 at 16:38
  • I don't think the `filter not exists` is a great solution in this case, since you'll lose results unexpectedly in the future if more `p rdf:type rdf:Property` triples are added. On the otherhand, if you simply filter out properties prefixed with `dbpprop:`, you won't lose things unexpectedly. I've written up this point in an answer. – Joshua Taylor Feb 24 '14 at 17:05
  • 1
    Duplicated on answers.semanticweb.com: http://answers.semanticweb.com/questions/26625/filter-out-certain-properties-from-sparql-query-result – Joshua Taylor Feb 24 '14 at 17:53
  • Is that a problem to ask a question in different places ? – Raphael Lopez Kaufman Feb 25 '14 at 08:10

2 Answers2

1

This one is working for me at http://dbpedia.org/snorql/?query=

PREFIX dbo: <http://dbpedia.org/ontology/>

SELECT DISTINCT ?qoi ?property ?value
WHERE { 
    ?qoi a foaf:Person. 
    ?qoi ?property ?value. 
OPTIONAL{ 
    ?property rdf:type ?type 
FILTER( ?type != "rdf:Property" ) }
}
LIMIT 100

I am not sure if this is the answer you are looking for

hibe
  • 96
  • 8
  • Not exactly because this query filters out properties which have no rdf:type. Yet I want to keep the results associated with properties with no rdf:type – Raphael Lopez Kaufman Feb 24 '14 at 10:44
  • 1
    @rlkrlk This should work if the `filter` is moved _inside_ the optional pattern: `optional { ?property rdf:type ?type filter( ?type != rdf:Property ) }`. – Joshua Taylor Feb 24 '14 at 15:38
  • @Joshua Taylor No it does not, it does not bind any value to ?type that's all. Have a look at this [query](http://dbpedia.org/sparql?default-graph-uri=http%3A%2F%2Fdbpedia.org&query=SELECT+DISTINCT+%3Fqoi+%3Fproperty+%3Fvalue%0D%0AWHERE+%7B+%0D%0A++++%3Fqoi+a+foaf%3APerson.+%0D%0A++++%3Fqoi+%3Fproperty+%3Fvalue.+%0D%0A++++OPTIONAL%7B%3Fproperty+rdf%3Atype+%3Ftype.+FILTER+%28+%3Ftype+%21%3D+rdf%3AProperty+%29+%7D%0D%0A++++%0D%0A%7D&format=text%2Fhtml&timeout=30000&debug=on) you get properties such as [dbpprop:after](http://dbpedia.org/property/after) whose type is rdf:Property – Raphael Lopez Kaufman Feb 24 '14 at 16:33
  • @rlkrlk I see your query results, but I can't tell what the query that you ran was (I know it's embedded in the URL, but I'm not going to try to decode it). The query in this question may bind values for `?type`, but you're not going to see it, because only `?qoi ?property ?value` are `select`ed. Perhaps you want `SELECT DISTINCT ?qoi ?property ?value ?type`? – Joshua Taylor Feb 24 '14 at 16:39
  • @rlkrlk But also note that what this optional is doing is getting additional information about `?property`; it's not excluding values of `?property` that have a `rdf:type `. For that you'd need something like a `filter not exists { … }`. – Joshua Taylor Feb 24 '14 at 16:40
  • 1
    That's it, `filter not exists{?property rdf:type rdf:Property}` works fine. Someone answered me on answers.semanticweb.com – Raphael Lopez Kaufman Feb 24 '14 at 16:45
  • @rlkrlk It's not a great solution, since each property can be _inferred_ to be a `rdf:Property`, since it is, in fact, a property. Furthermore, on DBpedia, there are no properties declared as `rdf:Property`s that don't also begin with the `dbpprop:` prefix. (E.g., test with the query `select ?p { ?p a rdf:Property . filter( !strstarts( str(?p), str(dbpprop:) ) ) }`; there are no results—there are no properties declared as `rdf:Property`s except the `dbpprop:` properties. Filtering on the prefix will have a clearer intent, and won't break if RDFS inference is applied. – Joshua Taylor Feb 24 '14 at 16:54
  • @Joshua Taylor There should be some kind of federated query (or maybe it is due to inference) since when I use dbpedia's sparql endpoint with your query within a python's script (`r = urllib2.urlopen('http://dbpedia.org/sparql/?query=select%20%3Fp%20%7B%20%3Fp%20a%20rdf%3AProperty%20.%20filter(%20!strstarts(%20str(%3Fp)%2C%20str(dbpprop%3A)%20)%20)%20%7D&format=json')`) I have several properties which come up when I consume `r`... – Raphael Lopez Kaufman Feb 24 '14 at 17:05
1

You can filter out dbprop: properties,

I'm trying to filter out certain properties (e.g., dbpprop: ones) from the result of a SPARQL query. I'm querying the DBpedia SPARQL endpoint.

If you're binding properties in your result set, and you want to exclude ones whose URI begins with the dbpprop: prefix, you can do that with

filter(!strstarts(str(?p),str(dbpprop:)))

or you can filter out properties that don't have type rdf:Property.

Your pseudocode looks like it's doing something slightly different though:

quantity_of_interest, property, value, property_type = sparqlQuery(query)
if property_type == rdf:Property:
    pass
else:
    return quantity_of_interest, property, value

Every property could have the type rdf:Property, since it's the class of RDF properties. DBpedia might not have the triple

p rdf:type rdf:Property

for each property p, of course, so you may still be able to filter things out like this. If that's what you want to do, you can do it with filter not exists { … }:

filter not exists { ?p rdf:type rdf:Property }

For DBpedia, it's the same right now,

As it turns out, these will have the same effect on DBpedia, since there are no properties that have type rdf:Property and aren't also dbpprop: properties; the following query returns 0:

select (count(*) as ?num) where {
 ?p a rdf:Property
 filter( !strstarts( str(?p), str(dbpprop:) ) )
}
limit 100

but one option is more future compatible.

I'd strongly suggest using the strstarts filter rather than the not exist here, though. While the URI of a property can't change over time (i.e., a URI is constant), the triples about it can change. Thus, if you filter out dbpprop: properties, then you'll never accidentally filter out more than you're expecting. However, if you filter out things that have rdf:Property as a type, then you can easily lose results in the future if more p rdf:type rdf:Property triples are added (and it seems like such an addition would be logically compatible).

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
  • There should be some kind of federated query (or maybe it is due to inference) since when I use dbpedia's sparql endpoint with your query within a python's script (`r = urllib2.urlopen('http://dbpedia.org/sparql/?query=select%20%3Fp%20%7B%20%3Fp%20a‌​%20rdf%3AProperty%20.%20filter(%20!strstarts(%20str(%3Fp)%2C%20str(dbpprop%3A)%20‌​)%20)%20%7D&format=json')`) I have several properties which come up when I consume `r`... – Raphael Lopez Kaufman Feb 24 '14 at 17:45