filter out certain properties from sparql query result

蹲街弑〆低调 提交于 2019-12-25 17:28:05

问题


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 ?


回答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




回答2:


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



来源:https://stackoverflow.com/questions/21984461/filter-out-certain-properties-from-sparql-query-result

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!