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.
Related
So I encountered a Problem on DBpedia. Apparently I can retrieve any kind of property from a resource with the query below, but when the property is from the type DatatypeProperty or just a number (xsd:integer or something similar) the DBpedia SPARQL Endpoint returns an empty result.
I would like to know what I have to change or even better optional include in my current query to solve this problem.
Important note: Germany and population total are required inputs in my program. Please remember that I must use them.
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT DISTINCT ?objectLabel
WHERE {
?subject ?predicate ?object ; rdfs:label "Germany"#en .
?predicate rdfs:label "population total"#en .
?object rdfs:label ?objectLabel
FILTER (LANG(?objectLabel)='en')
}
Thank you for your help.
A DatatypeProperty is used to related an individual to a literal and literals can't have any outgoing edge, thus, no rdfs:label. If you want to have the lexical form of the literal you can use the str function:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT DISTINCT (str(?object) as ?value)
WHERE {
?subject ?predicate ?object ; rdfs:label "Germany"#en .
?predicate rdfs:label "population total"#en
FILTER (LANG(?objectLabel)='en')
}
How can I list properties with their values for any given DBpedia class? I'm new to this and have looked at several other questions on this but I haven't found exactly what I'm looking for.
What I'm trying to do is providing some relevant additional information to topics of conversation I have got from text mining.
Say for example the topic of conversation in a certain community is iPhones. I would like to use this word to query the DBpedia page for this word, IPhone, to get an output such as:
Type: Smartphone
Operating System: IOS
Manufacturer: Foxconn
EDIT:
Using the query from AKSW I can print the p (property?) and o (object?), although I'm still not getting the output I want. Instead of getting something like:
weight: 133.0
I get
http://dbpedia.org/property/weight:133.0
Is there a way to just get the name of the property instead of the DBpedia link?
My Code
Classes do not "have" properties with values. Instances (resp. resources or individuals) do have a relationship via a property to some value which can be an individual itself or a literal (or some anonymous instance aka blank node). And instances belong to a class. e.g. Berlin belongs to the class City
What you want is to get all outgoing values of a given resource in DBpedia:
SELECT * WHERE { <http://dbpedia.org/resource/IPhone> ?p ?o }
Alternatively, you can use SPARQL DESCRIBE, which return the data in forms of an RDF graph resp. a set of RDF triples:
DESCRIBE <http://dbpedia.org/resource/IPhone>
This might also return incoming information because it's not really specified in the W3C recommendation what has to be returned.
As stated by AKSW properties often link to other classes rather than values. If you want all properties and their values, including other classes the the below gives you the label and filters by language (put the language code you need where have put "en").
SELECT DISTINCT ?label ?o
WHERE {
<http://dbpedia.org/resource/IPhone> ?p ?o.
?p <http://www.w3.org/2000/01/rdf-schema#label> ?label .
FILTER(LANG(?label) = "" || LANGMATCHES(LANG(?label), "en"))
}
If you don't want any properties that link to other classes, then you only want datatype properties so this code could help:
SELECT DISTINCT ?label ?o
WHERE {
<http://dbpedia.org/resource/IPhone> ?p ?o.
?p <http://www.w3.org/2000/01/rdf-schema#label> ?label .
?p a owl:DatatypeProperty .
FILTER(LANG(?label) = "" || LANGMATCHES(LANG(?label), "en"))
}
Obviously this gives you far less information and functionality, but it might just be what you're after?
Edit: In reply to your comment, it is also possible to get the labels for the values, using the same technique:
SELECT DISTINCT ?label ?oLabel
WHERE {
<http://dbpedia.org/resource/IPhone> ?p ?o.
?p <http://www.w3.org/2000/01/rdf-schema#label> ?label .
?o <http://www.w3.org/2000/01/rdf-schema#label> ?oLabel
FILTER(LANG(?label) = "" || LANGMATCHES(LANG(?label), "en"))
}
Note that http://www.w3.org/2000/01/rdf-schema#label is often shortened to rdfs:label by defining prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
So you could also do:
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT DISTINCT ?label ?oLabel
WHERE {
<http://dbpedia.org/resource/IPhone> ?p ?o.
?p rdfs:label ?label .
?o rdfs:label ?oLabel
FILTER(LANG(?label) = "" || LANGMATCHES(LANG(?label), "en"))
}
and get exactly the same result but possibly easier to read.
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
I need to get the co-ordinates of some particular places from DbPedia. For the same I am using following snippet:
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT * WHERE {
?x foaf:name 'Mumbai'.
?x dbo:Place 'Mumbai'.
?x geo:lat ?lat .
?x geo:long ?long .
}
On running at http://dbpedia.org/sparql, it returns an empty page. I think I didn't put the name of place in the correct way. Can anyone help me to find the issue
Have a look at the N3/TTL serialization of the data for Mumbai. Note that the foaf:name value has a language tag: foaf:name "Mumbai"#en. You need to use "Mumbai"#en, with the language tag, in the query. Additionally, doing dbo:Place 'Mumbai' doesn't make any sense. You might instead want to ask for things which are dbpedia-owl:Places. You'd end up with a query like:
select * {
?mumbai a dbpedia-owl:Place ;
foaf:name "Mumbai"#en ;
geo:lat ?lat ;
geo:long ?long
}
SPARQL Results
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.)