Ask if SPARQL resource exists

こ雲淡風輕ζ 提交于 2019-12-10 15:44:14

问题


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.


回答1:


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 }
    } 



回答2:


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


来源:https://stackoverflow.com/questions/47225585/ask-if-sparql-resource-exists

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