Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ask if SPARQL resource exists

Tags:

sparql

dbpedia

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.

like image 905
Peter Avatar asked Nov 10 '17 14:11

Peter


People also ask

What types of queries does SPARQL support?

SPARQL allows for a query to consist of triple patterns, conjunctions, disjunctions, and optional patterns. Implementations for multiple programming languages exist. There exist tools that allow one to connect and semi-automatically construct a SPARQL query for a SPARQL endpoint, for example ViziQuer.

What is construct query in SPARQL?

The CONSTRUCT query form returns an RDF graph. The graph is built based on a template which is used to generate RDF triples based on the results of matching the graph pattern of the query.

What is SPARQL prefix?

"PREFIX", however (without the "@"), is the SPARQL instruction for a declaration of a namespace prefix. It allows you to write prefixed names in queries instead of having to use full URIs everywhere. So it's a syntax convenience mechanism for shorter, easier to read (and write) queries.

What is Dbpedia SPARQL?

SPARQL (pronounced "sparkle" /ˈspɑːkəl/, a recursive acronym for SPARQL Protocol and RDF Query Language) is an RDF query language—that is, a semantic query language for databases—able to retrieve and manipulate data stored in Resource Description Framework (RDF) format.


2 Answers

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 }
    } 
like image 131
Stanislav Kralin Avatar answered Oct 02 '22 14:10

Stanislav Kralin


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
like image 23
Pankaj C. Avatar answered Oct 02 '22 14:10

Pankaj C.