I'm trying to get details about an artist via DBPedia and the SPARQL query language, however, it seems almost impossible (with my understanding) of how to get certain pieces of information.
I'm trying to get an Artist and pull information such as their Hometown. I'm guessing the query should be something similar to that of:
SELECT ?c WHERE {
?b <http://dbpedia.org/property/Artist> <http://dbpedia.org/resource/Arctic_Monkeys>.
?b <http://www.w3.org/2002/07/owl#ObjectProperty> <http://dbpedia.org/ontology/hometown>.
?b rdfs:label ?c.
}
If anyone could enlighten me to how it should be done, that would be amazing.
I've been trying out the queries at:
http://dbpedia.org/sparql
If you want to find the label of their hometown, try this:
SELECT ?hometownLabel WHERE {
<http://dbpedia.org/resource/Arctic_Monkeys> <http://dbpedia.org/ontology/hometown> ?hometown .
?hometown <http://www.w3.org/2000/01/rdf-schema#label> ?hometownLabel .
}
Maybe you don't have a good understanding of SPARQL syntax. Unlike SQL, SPARQL search results by writing some triples with unknow variables in the WHERE clause.
you can try:
prefix dbpedia-owl:<http://dbpedia.org/ontology/>
SELECT ?c
WHERE {
<http://dbpedia.org/resource/Arctic_Monkeys> dbpedia-owl:hometown ?c.
}
With this search, you will get Arctic_Monkeys' hometown.
SELECT ?hometown
WHERE {
dbr:Arctic_Monkeys dbo:hometown ?label.
?label rdfs:label ?hometown.
FILTER(langMatches(lang(?hometown), "en"))
}
Related
I am using the following query :
select ?value where { <http://dbpedia.org/resource/Paris> dbpedia-owl:wikiPageRedirects* ?value }
in order to retrieve the wikiPageRedirects property of Paris.
Based on dbpedia Paris has more than 20 redirect links. Why am I only retrieving the first one?
Your direction was wrong.
select distinct *
where {
?x dbpedia-owl:wikiPageRedirects <http://dbpedia.org/resource/Paris>
}
Artemis's answer is right; the "direction" in the query is wrong. It's worth explaining that a bit more, though. On the DBpedia "page", you'll see lots of data like:
dbpedia-owl:area 105400000.000000 (xsd:double)
dbpedia-owl:country dbpedia:France
dbpedia-owl:inseeCode 75056 (xsd:integer)
dbpedia-owl:mayor dbpedia:Anne_Hidalgo
These mean that DBpedia contains triples where these are the predicates and objects. That is, DBpedia contains a triple:
dbpedia:Paris dbpedia-owl:country dbpedia:France
On other hand, you'll also see things like "is … of":
is dbpedia-owl:beatifiedPlace of dbpedia:Daniel_Brottier
is dbpedia-owl:billed of dbpedia:René_Duprée
These mean that dbpedia:Paris the object of triples with these subjects and predicates. E.g., DBpedia contains the triple
dbpedia:René_Duprée dbpedia-owl:billed dbpedia:Paris
The redirects properties that you're seeing are like this:
is dbpedia-owl:wikiPageRedirects of dbpedia:City_of_Love_(city)
dbpedia:Département_de_Paris
dbpedia:Departement_de_Paris
dbpedia:FRPAR
That means that there are a bunch of triples of the form:
?something dbpedia-owl:wikiPageRedirects dbpedia:Paris
and that means that your query needs to be
select ?resource where {
?resource dbpedia-owl:wikiPageRedirects dbpedia:Paris
}
SPARQL results
I am trying to select all different language tags that appear on a sparql endpoint (like DBpedia) and display them as a list, but with no luck until now.
A simple Example of Triples on Endpoint.
<person1> rdfs:label "name1"#en
<person1> rdfs:label "name2"#fr
<person2> rdfs:comment "comment"#en
<person2> rdfs:label "name3"#el
The goal is to create a sparql query that returns:
fr
en
el
Is there any way to select languages tags efficiently?
Is there a solution for any sparql version(1.0,1.1) ?
Given your tags include SPARQL, you could try this:
SELECT DISTINCT ?lang
WHERE {
?s ?p ?o .
BIND (lang(?o) AS ?lang)
}
I need to find all DBpedia categories and articles that their abstract include a specific word.
I know how to write a SPARQL query that queries the label like the following:
SELECT ?uri ?txt WHERE {
?uri rdfs:label ?txt .
?txt bif:contains "Machine" .
}
but I have not figured out yet how to search the abstract.
I've tried with the following but it seems not to be correct.
SELECT ?uri ?txt WHERE {
?uri owl:abstract ?txt .
?txt bif:contains "Machine" .
}
How can I retrieve the abstract in order to query its text?
Since you already know how to search a string for text content, this question is really about how to get the abstract. If you retrieve any DBpedia resource in a web browser, e.g., http://dbpedia.org/resource/Mount_Monadnock (which will redirect to http://dbpedia.org/page/Mount_Monadnock), you can see the triples of which it's a subject or predicate. In this case, you'll see that the property is dbpedia-owl:abstract. Thus you can do things like
select * where {
?s dbpedia-owl:abstract ?abstract .
?abstract bif:contains "Monadnock" .
filter langMatches(lang(?abstract),"en")
}
limit 10
SPARQL results
Instead of visiting the page for the resource, which not endpoints will support, you could have simply retrieved all the triples for the subject, and looked at which ones relate it to its abstract. Since you know the abstract is a literal, you could even restrict it to triples where the object is a literal, and perhaps with a language that you want. E.g.,
select ?p ?o where {
dbpedia:Mount_Monadnock ?p ?o .
filter ( isLiteral(?o) && langMatches(lang(?o),'en') )
}
SPARQL results
This also clearly shows that the property you want is http://dbpedia.org/ontology/abstract. When you have a live query interface that you can use to pull down arbitrary data, it's very easy to find out what parts of the data you want. Just pull down more than you want at first, and then refine to get just what you want.
I have the following SPARQL query:
SELECT ?nationalityLabel WHERE {
dbpedia:Henrik_Ibsen dbpedia-owl:nationality ?nationality .
?nationality rdfs:label ?nationalityLabel .
}
I have checked that Henrik Ibsen exists and that he has the nationality ontology/property on him:
http://dbpedia.org/page/Henrik_Ibsen
And this is an ontology:
http://dbpedia.org/ontology/nationality
A very similar query to this listed here works:
https://stackoverflow.com/a/10248653/1680130
The problem I have is that the query doesn't return any result.
If I could get help solving this it would be great.
Summarized solution:
Both answers were great so upvote to both but landed on Joshua's in the end because informing about dbpedia-owl being cleaner. Optimal solution in my opinion:
First check with dbpedia-owl for birth-place:
select ?label {
dbpedia:Henrik_Ibsen
dbpedia-owl:birthPlace
[ a dbpedia-owl:Country ;
rdfs:label ?label ]
filter langMatches(lang(?label),"en")
}
If found then get the demonym:
select ?label {
dbpedia:Norway dbpedia-owl:demonym ?label
filter langMatches(lang(?label),"en")
}
If above fails then do the "dirty" query:
SELECT
?nationality
WHERE {
dbpedia:Henrik_Ibsen dbpprop:nationality ?nationality .
filter langMatches(lang(?nationality),"en")
}
Of course is "dirty" means data being correct but not so often present the order might be better other way around because people can be born in a country but from a different.
Kristian's answer is right that the property is dbpprop:nationality that Henrik Ibsen has. You're right that there is a dbpedia-owl:nationality property, too, but Henrik Ibsen doesn't have a value for it, unfortunately. The value of dbpprop:nationality that Henrik Ibsen has, though, is a string, which is a literal, and literals cannot be the subjects of triples in RDF, so ?nationality rdfs:label ?nationalityLabel in your query will never match.
The DBpedia ontology data (dbpedia-owl) tends to be cleaner than the dbpprop data, so you might prefer a solution using dbpedia-owl properties that Henrik Ibsen does have. In this case, you might look to the dbpedia-owl:birthPlace. Then you could get the name the country of the birth places:
select ?label {
dbpedia:Henrik_Ibsen
dbpedia-owl:birthPlace
[ a dbpedia-owl:Country ;
rdfs:label ?label ]
}
SPARQL results
You might want to narrow the permissible languages:
select ?label {
dbpedia:Henrik_Ibsen
dbpedia-owl:birthPlace
[ a dbpedia-owl:Country ;
rdfs:label ?label ]
filter langMatches(lang(?label),"en")
}
SPARQL results
Those queries will produce the name of the country, but it wanted the corresponding demonym, you can get the dbpedia-owl:demonym value of the country, if it's available. It's probably best to make the demonym optional, since a cursory investigation suggests that lots of countries in DBpedia don't have a value for it, so the name of the country may be the only option. E.g.,
select ?name ?demonym {
dbpedia:Henrik_Ibsen dbpedia-owl:birthPlace ?country .
?country a dbpedia-owl:Country ; rdfs:label ?name .
optional { ?country dbpedia-owl:demonym ?demonym }
filter langMatches(lang(?name),"en")
filter langMatches(lang(?demonym),"en")
}
SPARQL results
Two things are wrong with the query:
It's dbpprop:nationality
The label doesn't appear to exist, and unless you make that variable optional, it will eliminate the row altogether. EDIT: *Joshua Taylor's answer reminded me that the label doesn't exist because the dbprop:nationality value is a literal, which cannot be used as a subject resource, therefore, there will never be a label for dbpprop:nationality. Instead, where the data exists, you would use dbpedia-owl:nationality, which you did originally. it just so happens that Henrik_Ibsen has no dbpedia-owl:nationality value associated with him*
Updated query (updated).
SELECT
#### ?label #### See Edit
?nationality
WHERE {
dbpedia:Henrik_Ibsen dbpprop:nationality ?nationality .
#### OPTIONAL { ?nationality rdfs:label ?label . } #### See Edit.
}
How can I retrieve all the properties of http://dbpedia.org/resource/Milano? I tried with this query but I have a few results and I don't understand the reason:
select ?prop ?c
where {<http://dbpedia.org/resource/Milano> ?prop ?c.}
SPARQL results
The question isn't entirely clear, but expect that the problem you're asking about is why you're getting triples about dbpedia:Milano, but not dbpedia:Milan. This query, as you can see in the results, only returns ten rows:
select ?prop ?c
where {
<http://dbpedia.org/resource/Milano> ?prop ?c.
}
SPARQL results
One of those rows, however, is
prop c
http://dbpedia.org/ontology/wikiPageRedirects http://dbpedia.org/resource/Milan
So, the simple answer is "query for Milan" with a query like this:
select ?prop ?c
where {
<http://dbpedia.org/resource/Milan> ?prop ?c. # you can use dbpedia:Milan, too
}
SPARQL results
A more sophisticated answer would return the triples for dbpedia:Milano and any triples of anything that it redirects to (and, I suppose, anything that any of those redirect to, and so on, though I think that Wikipedia limits redirects to be one level deep). You can do this with a property path query in SPARQL:
select ?prop ?c
where {
dbpedia:Milano dbpedia-owl:wikiPageRedirects* ?subject .
?subject ?prop ?c.
}
SPARQL results
In that query ?subject will be anything related by a path of length zero or more (so, given the data that we've seen, ?subject will be bound to at least dbpedia:Milano and dbpedia:Milan. If you want to preserve information about the subject of the various triples that you're using, you might want to add ?subject to the select line, so as to have select ?subject ?prop ?c.
If you don't care about the particular value of ?subject, then you actually don't need to bind ?subject at all, and could use a blank node in the query:
select ?prop ?c
where {
dbpedia:Milano dbpedia-owl:wikiPageRedirects* [ ?prop ?c ] .
}
SPARQL results
Caveat
Unfortunately, although this last query is legal SPARQL, Virtuoso says it's an error. Fortunately, this last refinement is entirely optional; it's not vital to the solution. If you were querying against a different endpoint, you'd be able to use it. The error that Virtuoso gives is:
Virtuoso 37000 Error SP031: SPARQL compiler: Object of transitive triple pattern should be variable or QName or literal, not blank node
SPARQL query:
define sql:big-data-const 0
#output-format:text/html
define sql:signal-void-variables 1 define input:default-graph-uri <http://dbpedia.org> select ?prop ?c
where {
dbpedia:Milano dbpedia-owl:wikiPageRedirects* [ ?prop ?c ] .
}
I contacted the Virtuoso mailing list and they confirmed that it's a Virtuoso bug, and that they'll fix it. I don't know how long it will take for the fix to get to the DBpedia endpoint, though.