Here I have SPARQL query for DBpedia, to get all the properties for an item.
Usually I do specify ?item by its short label name, just like ?item rdfs:label "Reddit"#en .. But here I'd like to specify the query with long URL like ?item rdfs:label <http://dbpedia.org/resource/Telegram_(software)> ., since ?item rdfs:label "Telegram_(software)"#en . does not work because of its parentheses. Also I tried ?item rdfs:label "Telegram_%28software%29"#en . but not worked as well.
So, how can I use a long URL for rdfs:label on DBpedia? Thanks.
SELECT DISTINCT *
WHERE
{
?item rdfs:label "Reddit"#en . # works
# ?item rdfs:label <http://dbpedia.org/resource/Reddit> . # doesn't work for long url format
# ?item rdfs:label <http://dbpedia.org/resource/Telegram_(software)> . # doesn't work for long url format
?item ?prop ?tmp .
}
Related
For the RDF labels I'm trying something really simple to show all the labels in my ontology , but even that its not working. Do you have any idea how I need to write any rdfs:label
SELECT ?subject ?label
WHERE { ?subject rdfs:label ?label }
Try this:
SELECT ?subject ?label WHERE { ?subject <http://www.w3.org/2000/01/rdf-schema#label> ?label }
You could also maybe use a prefix like so:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?subject ?label WHERE { ?subject rdfs:label ?label }
You can try out the queries by using the DBPedia endpoint:
https://dbpedia.org/sparql
To use existing predicates from other graphs you first have to include the reference to the predicate. Otherwise the query cannot find the correct predicate.
What the asterisk mean in this SPARQL query?
SELECT ?uri ?type
WHERE{
?uri a ?type.
?type rdfs:subClassOf* example:Device.
}
Does it mean "subclass of a subclass"?
Can I use it with other predicates?
An asterisk (*) after a path element means “zero or more of this element”.
If there are no other elements in the path, ?a something* ?b means that ?b might also just be ?a directly, with no path elements between them at all.
?item wdt:P31/wdt:P279* ?class.
# means:
?item wdt:P31 ?class
# or
?item wdt:P31/wdt:P279 ?class
# or
?item wdt:P31/wdt:P279/wdt:P279 ?class
# or
?item wdt:P31/wdt:P279/wdt:P279/wdt:P279 ?class
See here for more detailed answer.
The asterisk after a predicate means that you want to follow a property path with zero or more occurrences of rdfs:subClassOf.
Your phrase "subclass of a subclass" is about right, although I would say "subclasses of subclasses," because the * property path is recursive. As you can see from the technical document in AKSW's comment, there are several other property path operators that go in either direction, with or without a limit on the number of occerances (or depth.)
Here's a pretty good example from Marklogic... I think this should work within any 1.1 endpoint.
https://developer.marklogic.com/features/semantics/path-examples
Yes, property paths are applicable to any predicate/property, not just rdfs:subClassOf.
So my question is simple: from this URI --
http://dbpedia.org/snorql/?describe=http%3A%2F%2Fdbpedia.org%2Fresource%2FRed_Dragon_%28spacecraft%29
-- I want to extract specific things like --
rdfs:comment
rdfs:label
how to do that ?
Currently your query gets all properties, including rdfs:label and rdfs:comment. To get just those properties, substitute them for ?property, e.g.:
{ <http://dbpedia.org/resource/Red_Dragon_(spacecraft)> rdfs:label ?label .
<http://dbpedia.org/resource/Red_Dragon_(spacecraft)> rdfs:comment ?comment .
}
Also, you may want to filter for language tags, e.g., FILTER (lang(?label) = "en).
I'm trying to query data from dbpedia by a country's name. I want it to find it whether there is a resource for that country or via its existence in wikiPageRedirects. Here is a working version:
PREFIX res: <http://dbpedia.org/resource/>
PREFIX ont: <http://dbpedia.org/ontology/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?country ?capital ?label
WHERE {
{ res:Dominion_of_Canada ont:capital ?capital .
?capital rdfs:label ?label }
UNION
{ res:Dominion_of_Canada ont:wikiPageRedirects ?country .
?country ont:capital ?capital .
?capital rdfs:label ?label }
FILTER (lang(?label) = "en")
}
I'd like (if possible), to factor out the ?country. Is it possible to assign a resource to a variable such that the SPARQL query looks like the following?
SELECT ?country ?capital ?label
WHERE {
{ ?country EXISTS res:Dominion_of_Canada } # to get the idea across
UNION
{ res:Dominion_of_Canada ont:wikiPageRedirects ?country }
?country ont:capital ?capital .
?capital rdfs:label ?label .
FILTER (lang(?label) = "en")
}
As ever, speed is important, too. If the resource exists, then it'd be better if it skipped searching on wikiPageRedirects.
Checking whether a resource "exists" or not is a bit vague, since IRIs are just constant data. The question is really whether DBpedia contains any triples about a particular resource. In your case, you're wanting to know whether it it redirects to anything else, or if it has properties of its own. A property path of the form dbpedia:France dbpedia-owl:wikiPageRedirects* ?country is really probably the best way to do that. If there are no redirect links, then ?country is dbpedia:France, and if there are, then ?country is the value of the redirects. The only way to "check" is to look for those triples. I think that means you will end up with something like this (similar to what's shown in my answer to another question involving redirects):
select ?country ?anthem ?author {
#-- The only way to really "check" that the resource
#-- "exists" and is not a redirect, is by checking
#-- whether it has any redirect links. If it doesn't,
#-- then ?country is dbpedia-owl:France, like you want
#-- and if it does, then then you want to follow them.
dbpedia:France dbpedia-owl:wikiPageRedirects* ?country .
#-- I'm using anthem and author here because
#-- it doesn't look like there was reliable information
#-- about the capital.
?country dbpedia-owl:anthem ?anthem .
?anthem dbpprop:author ?author .
}
SPARQL results
What about this?
PREFIX dbr: <http://dbpedia.org/resource/>
select dbr:France ?capital ?label where {
{dbr:France a dbpedia-owl:Country.
dbr:France dbpedia-owl:capital ?capital .
?capital rdfs:label ?label .
}
union {dbr:France dbpedia-owl:wikiPageRedirects ?redirectPage.
?redirectPage dbpedia-owl:capital ?capital.
?capital rdfs:label ?label .
}
}
Result
Basically I have a query (shown below) which works efficiently. However, I want my search to be more precise where the label is the actual string 'yago' rather than containing the string 'yago'. I want to try to do it without filters if possible as I think using FILTER makes querying DBpedia take longer.
SELECT ?uri ?label
WHERE {
?uri rdfs:label ?label.
?label bif:contains "'yago'" .
}
You can try doing the following if you want to do it without filters:
SELECT ?uri ?label
WHERE {
?uri rdfs:label "Yago"#en .
?uri rdfs:label ?label
}
I not sure though it if it is much faster than the corresponding query with filters:
SELECT ?uri ?label
WHERE {
?uri rdfs:label ?label .
filter(?label="Yago"#en)
}
A key correction to the original response. If you have an exact match, with the language label, then the following will work:
SELECT ?uri ?label
WHERE {
?uri rdfs:label "Yago"#en .
}
If, however, the exact match may not be using what you want, SPARQL supports a standard regex:
SELECT ?uri ?label
WHERE {
?uri rdfs:label ?label .
FILTER regex(str(?label), "yago", "i")
}
...which will match the string regardless of character case, and you can play the usual regex games to get the required string match. (Of course, other string functions, such as STRSTARTS and STRENDS will be more efficient if those meet the desired matching criteria.)