Reputation: 13
I need to retrieve the list of south american countries where the capital is also the largest city. For some reason I can display all South American countries with their capitals, but I can't use a filter option to compare the capital with the largest city:
SELECT DISTINCT ?country ?capital
WHERE {
?country a dbo:Country .
?country a <http://dbpedia.org/class/yago/SouthAmericanCountries>.
?country dbp:largestCity|dbo:largestCity ?capital.
}
For the 3rd triplet I use 2 arguments because not all countries have complete data. After running this on the dbpedia validator I obtain this: http://dbpedia.org/sparql?default-graph-uri=http%3A%2F%2Fdbpedia.org&query=SELECT+DISTINCT+%3Fcountry+%3Fcapital%0D%0AWHERE+%7B%0D%0A%0D%0A++++%3Fcountry+a+dbo%3ACountry+.%0D%0A++++%3Fcountry+a+%3Chttp%3A%2F%2Fdbpedia.org%2Fclass%2Fyago%2FSouthAmericanCountries%3E.%0D%0A++++%0D%0A++++%3Fcountry+dbp%3AlargestCity%7Cdbo%3AlargestCity+%3Fcapital.%0D%0A%0D%0A++++%0D%0A%0D%0A%7D&format=text%2Fhtml&CXML_redir_for_subjs=121&CXML_redir_for_hrefs=&timeout=30000&debug=on
The next step is to filter the countries using this logic: largestCity=capital. So I used:
FILTER (dbp:largestcity="capital")
but the Viruoso validator sends me an exception for exceeding the execution time. Any suggestions?
Upvotes: 1
Views: 918
Reputation: 85843
I'm not sure what you're expecting your filter to do. In FILTER (dbp:largestcity="capital"), "capital" is a string, and dbp:largestcity (which is not the same as dbp:largestCity, by the way) is an IRI; they're never going to be equal.
Now, I do see that sometimes the value of dbp:largestCity is the string "capital"@en, so there is some string matching that may be helpful. In general, though, the DBpedia ontology properties have much cleaner data than the raw infobox properties, so if you can, you should prefer the dbo: properties. Here, I guess, you'll want both, though.
You need your query to extract the capital, as well the largest city, and then you want to filter for cases where they're equal, or where the largest city is the string "capital".
SELECT DISTINCT ?country ?capital WHERE {
?country a dbo:Country .
?country a <http://dbpedia.org/class/yago/SouthAmericanCountries>.
?country dbo:capital ?capital .
?country dbp:largestCity|dbo:largestCity ?largestCity .
filter (?largestCity = ?capital || str(?largestCity)= "capital")
}
Upvotes: 3