Could anyone please help me with this code. It doesn't come out with any output but with no error.
q = """SELECT DISTINCT ?label ?abstract ?director ?starring
WHERE {
<http://dbpedia.org/resource/Seven_Beauties> rdfs:label ?label.
?label dbo:abstract ?abstract.
?label dbo:director ?director.
?label dbo:starring ?starring.
FILTER (lang(?label) = "en")
FILTER (lang(?abstract) = "en")
}"""
from textwrap import wrap
for result in query(q):
print(result['label'],
"\n----Director----\n",result['director'],
"\n----Starring----\n",result['starring'],
"\n----Abstract----\n",
"\n".join(wrap(result['abstract'])))
<http://dbpedia.org/resource/Seven_Beauties> is the entity which has a Label, Abstract, Director, Star, etc.
The Label of that entity is a literal, and it has no Director, Star, Abstract, etc.
Try changing your query to --
SELECT DISTINCT ?label ?abstract ?director ?starring
WHERE
{
<http://dbpedia.org/resource/Seven_Beauties>
rdfs:label ?label ;
dbo:abstract ?abstract ;
dbo:director ?director ;
dbo:starring ?starring
FILTER ( lang(?label) = "en" )
FILTER ( lang(?abstract) = "en" )
}
-- and see results from DBpedia.
DBpedia Live delivers nothing for the query above -- because the descriptions of films vary. Making some things optional will get you more results on DBpedia Live --
SELECT DISTINCT ?label ?abstract ?director ?starring
WHERE
{
<http://dbpedia.org/resource/Seven_Beauties> rdfs:label ?label
OPTIONAL { <http://dbpedia.org/resource/Seven_Beauties> dbo:abstract ?abstract }
OPTIONAL { <http://dbpedia.org/resource/Seven_Beauties> dbo:director ?director }
OPTIONAL { <http://dbpedia.org/resource/Seven_Beauties> dbo:starring ?starring }
FILTER ( lang(?label) = "en" )
FILTER ( lang(?abstract) = "en" )
}
Related
I'm currently trying to teach myself how to formulate SPARQL queries to extract tourism-related information from DBpedia (via http://dbpedia.org/sparql/).
So far, I've managed to get all museums for a country.
select ?thing ?type ?category ?long ?lat ?country
where
{
VALUES ?country { <http://dbpedia.org/resource/Canada> }
optional
{
?city dbo:country ?country
}
?thing dbo:location ?city.
optional
{
?thing a ?type .
VALUES ?type { dbo:Museum }
BIND( 'Museum' as ?category )
}
optional
{
?thing a ?type.
VALUES ?type { dbo:skiLift }
BIND( 'Skilift' as ?category )
}
optional
{
?thing geo:long ?long.
?thing geo:lat ?lat
}
{
?thing a dbo:Place
}
filter (BOUND (?type))
}
However, I don't understand what I need to do to also get the same information for things like dbo:skiLift, dbo:touristicSite and the like (found here: http://dbpedia.org/ontology/Place).
What am I doing wrong?
This is because both dbo:skiLift and dbo:touristicSite are properties. These resources show up in the page for Place not as subclasses of Place, but as properties which have the class Place as their domain or range. If you want to find subclasses of Place you can perform the exploratory query (which also uses property path to retrieve the transitive closure of the subClassOf property):
select ?thing
where
{
?thing rdfs:subClassOf+ <http://dbpedia.org/ontology/Place> .
}
Apart from that, I cannot understand why you use two optional clauses for different types in the same query. For example, the following query retrieves museums located at cities of Canada, possibly with their lat and lon, without the use of other optional clauses:
select ?thing ?city ?long ?lat
where
{
?city dbo:country <http://dbpedia.org/resource/Canada> .
?thing dbo:location ?city .
?thing a dbo:Museum .
optional
{
?thing geo:long ?long .
?thing geo:lat ?lat
}
}
Trying to execute some queries but when searching for foaf:name resultset is empty. Here's my code:
SELECT DISTINCT ?uri ?string
WHERE {
?uri rdf:type ?x.
?uri foaf:name 'Cavallo domestico'#it .
OPTIONAL { ?uri rdfs:label ?string . FILTER (lang(?string) = 'it') }
}
page exist http://it.dbpedia.org/resource/Equus_caballus/html
Apparently seems it's not related with languages different than english but with foaf:name request. If I execute following, retrieving generic foaf:givenName, it works:
SELECT DISTINCT ?uri ?string
WHERE {
?uri rdf:type ?x.
?uri foaf:givenName 'Jimmy'#en .
OPTIONAL { ?uri rdfs:label ?string . FILTER (lang(?string) = 'en') }
}
I think this wasn't working when I first mentioned that it didn't in a comment, but, as AKSW points out, this seems to be working now. The rdfs:label property has the article titles in various languages, not the foaf:name, so you can do this to get the types of Horse:
select ?x ?type {
?x a ?type ;
rdfs:label "Equus caballus"#it
}
SPARQL results
I have a list of dbpedia URI's and I want to get some informations (categories, label) about each of them in one query:
SELECT ?category ?label where {
{
dbpedia:Financial_Times dcterms:subject ?category .
dbpedia:Financial_Times rdfs:label ?label .
FILTER ( lang(?label) = 'en' )
}
UNION
{
dbpedia:London dcterms:subject ?category .
dbpedia:London rdfs:label ?label .
FILTER ( lang(?label) = 'en' )
}
}
This query works fine, but I'd need to add the URI's themself into the result to be able identify which result row is for which URI.
you can do something like
SELECT distinct ?who ?category ?label where {
{
?who dcterms:subject ?category .
?who rdfs:label ?label .
FILTER ( lang(?label) = 'en' ).
FILTER(?who = dbpedia:Financial_Times or ?who = dbpedia:London )
}}
or use a trick like this
SELECT ?who ?category ?label where {
{
dbpedia:Financial_Times dcterms:subject ?category .
dbpedia:Financial_Times rdfs:label ?label .
FILTER ( lang(?label) = 'en' ).
VALUES ?who { dbpedia:Financial_Times}
}
UNION
{
dbpedia:London dcterms:subject ?category .
dbpedia:London rdfs:label ?label .
FILTER ( lang(?label) = 'en' ) .
VALUES ?who { dbpedia:London }
}}
the second one probably is faster but needs SPARQL 1.1
I'd like to do something like
{
SELECT ?page, "A" AS ?type WHERE
{
?s rdfs:label "Microsoft"#en;
foaf:page ?page
}
}
UNION
{
SELECT ?page, "B" AS ?type WHERE
{
?s rdfs:label "Apple"#en;
foaf:page ?page
}
}
But this gives a syntax error. How can I union two select queries in SPARQL?
You can union them like this:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT * WHERE
{
{
SELECT ?page ("A" AS ?type) WHERE
{
?s rdfs:label "Microsoft"#en;
foaf:page ?page
}
}
UNION
{
SELECT ?page ("B" AS ?type) WHERE
{
?s rdfs:label "Apple"#en;
foaf:page ?page
}
}
}
(check with the SPARQL validator)
However I don't think you need sub queries at all for this case. For example:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?page ?type WHERE
{
?s foaf:page ?page .
{ ?s rdfs:label "Microsoft"#en . BIND ("A" as ?type) }
UNION
{ ?s rdfs:label "Apple"#en . BIND ("B" as ?type) }
}
Based on #user205512's answer, here's one that works on Virtuoso:
SELECT * {
?s foaf:page ?page .
{
SELECT ?page ("A" AS ?type) {
?s rdfs:label "Microsoft"#en;
foaf:page ?page
}
} UNION {
SELECT ?page ("B" AS ?type) {
?s rdfs:label "Apple"#en;
foaf:page ?page
}
}
}
The trick was just do add an additional ?s foaf:page ?page triple outside of the UNION. This is obviously redundant, but it seems to avoid the Virtuoso bug, which is apparently caused when you have a “naked” UNION with subqueries.
I want to get triples about cities, which are from certain country. How can I do that?
I tried:
CONSTRUCT { ?c rdfs:label ?name . ?c rdfs:comment ?desc }
WHERE {
?c dbpprop:wikiPageUsesTemplate <http://dbpedia.org/resource/Template:Infobox_settlement> .
?c rdfs:label ?name .
?c rdfs:comment ?desc .
?c <http://dbpedia.org/ontology/country> ?country . ?country a <http://dbpedia.org/resource/CountryName>
FILTER ( lang(?name) = "en" && lang(?desc) = "en" )
}
but no luck :/ how can i do this?
CONSTRUCT { ?c rdfs:label ?name }
WHERE {
?c dbpprop:wikiPageUsesTemplate <http://dbpedia.org/resource/Template:Infobox_settlement> .
?c rdfs:label ?name .
?c dbpedia-owl:country <http://dbpedia.org/resource/Country> .
OPTIONAL { ?c dbpedia-owl:areaCode ?areacode }
FILTER ( lang(?name) = "pl" && ?population > 5000)
}
Hope it will help :)