Ankit Solanki
Ankit Solanki

Reputation: 680

Sparql : Finding Leaf Uri's

Let me know your thoughts on this..

Goal: To extract URIs (leaf URIs) of a label being passed.

Where a Leaf URI is defined to be a URI which has no disambiguates or redirect to another URI.

Below query works fine for all cases other than for one where the URI has a redirect and the redirected URI has disambiguates.

Query :

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?termURI ?redirects ?term ?disambiguates 
WHERE
{
  ?termURI rdfs:label ?term .
  OPTIONAL{ ?termURI <http://dbpedia.org/ontology/wikiPageRedirects> ?redirects .}
  OPTIONAL{ ?termURI <http://dbpedia.org/ontology/wikiPageDisambiguates> ?disambiguates .} 
  OPTIONAL{ ?redirects <http://dbpedia.org/ontology/wikiPageDisambiguates> ?disambiguates .}
  FILTER((?term = 'Build ups'@en))
}

Any Idea how can I fix this?

Upvotes: 1

Views: 383

Answers (1)

RobV
RobV

Reputation: 28655

You should be able to use a FILTER NOT EXIST to achieve your goal like so:

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbpedia: <http://dbpedia.org/ontology/>

SELECT ?termURI ?term
WHERE
{
  ?termURI rdfs:label ?term .
  FILTER NOT EXISTS 
  { 
    { ?termURI dbpedia:wikiPageRedirects [] }
    UNION
    { ?termURI dbpedia:wikiPageDisambiguates [] }
  }
  FILTER((?term = 'Build ups'@en))
}

What this query expresses is that you want to eliminate any matches for which there is either a redirect or disambiguates links.

See Filtering Using Graph Patterns in the SPARQL specification for more details on these syntax elements.

Upvotes: 1

Related Questions