Ask if SPARQL resource exists
Asked Answered
R

2

5

What is a good way to check if a SPARQL resource exists? I'm searching for the equivalent of firing a HTTP GET request to e.g. http://dbpedia.org/resource/Game_of_Thrones and check the HTTP status code but I'd like to do it with a SPARQL query.

I thought about something like this:

ASK {<http://dbpedia.org/resource/Game_of_Thrones> a <http://dbpedia.org/resource/>} 

I'm sure there is a good way to do this but I can't find it.

Note: I don't want to check for the existance of a specific triple. I just want to know if a resource exists.

Raki answered 10/11, 2017 at 14:58 Comment(3)
Perhaps you could use DESCRIBE dbr:Game_of_Thrones. More canonical way is ASK { VALUES (?r) {(dbr:Game_of_Thrones)} {?r ?p ?o} UNION {?s ?r ?o} UNION {?s ?p ?r} } . DBpedia understands even this: ASK { VALUES (?r) {(dbr:Game_of_Thrones)} {?r [] []} UNION {[] ?r []} UNION {[] [] ?r} } Quadrangular
The suggestion from @StanislavKralin is perfect. In addition, if you really talk about resources and not about the schema, you could even ignore the triple pattern with the resource being in predicate position. That's all you can do.Atahualpa
Thanks @StanislavKralin! That's what I was looking for. If you wish you can post your commend as answer and I'll accept it. :)Raki
Q
5

SPARQL is an RDF query language. An RDF triple consists of subject, predicate and object.

You just need to check if an URI is present in any of these positions, using UNION as disjunction.

ASK {
    VALUES (?r) { (dbr:Game_of_Thrones) }
        { ?r ?p ?o }
        UNION
        { ?s ?r ?o }
        UNION
        { ?s ?p ?r }
    } 

Although the SPARQL specification doesn't allow [] in predicate position, DBpedia (i.e., the Virtuoso server serving the DBpedia endpoint) understands this:

ASK {
    VALUES (?r) { (dbr:Game_of_Thrones) }
        { ?r [] [] }
        UNION
        { [] ?r [] }
        UNION
        { [] [] ?r }
    } 
Quadrangular answered 11/11, 2017 at 18:4 Comment(1)
P. S. Strictly speaking, an URI can also be a named graph URI…Quadrangular
P
1

I found another way of checking if a dbpedia resource exists for a given string. The first part of the below query will check if a resource exists in dbpedia for the string "Bob Marly". The second part will check if the string "Bob Marly" redirects page to any other page. You need to capitalize the first letter of each string to get correct results.

I think this way is better since you'll know if a resource exists even if you make minor spelling mistakes and you also don't have to replace the spaces with underscores (not that it's that big a deal).

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dbo: <http://dbpedia.org/ontology/>

SELECT ?s WHERE {
  {
    ?s rdfs:label "Bob Marly"@en ;
       a owl:Thing .       
  }
  UNION
  {
    ?altName rdfs:label "Bob Marly"@en ;
             dbo:wikiPageRedirects ?s .
  }
}

Reference: http://www.snee.com/bobdc.blog/2011/05/using-sparql-to-find-the-right.html

There is one more way to achieve the above:

SELECT ?subject ?label

WHERE { 
?subject rdfs:label ?label 

FILTER (langMatches(lang(?label), "EN")) .
FILTER ( ?label = "Two"@en )

} LIMIT 100
Petrel answered 3/2, 2019 at 22:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.