Skip to content

Instantly share code, notes, and snippets.

@cmarat
Last active August 29, 2015 14:08
Show Gist options
  • Save cmarat/e6a9488309385276dda5 to your computer and use it in GitHub Desktop.
Save cmarat/e6a9488309385276dda5 to your computer and use it in GitHub Desktop.
SPARQL Query Alternative Country Names
# Get alternative names of all countries from geonames. The query should have been
# much simpler, but
#
# 1) It's hard (or impossible) to tell cities from countries in the geonames data
# 2) In dbpedia too many things are dbo:Country whereas they are not.
#
# This was minimally tested using <http://lod.openlinksw.com/sparql>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX factbook: <http://wifo5-04.informatik.uni-mannheim.de/factbook/ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX geonames: <http://www.geonames.org/ontology#>
PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT distinct ?dbp ?geo ?name {
{
# Get a list of countries according to CIA Factbook, fix factbook URI's
SELECT DISTINCT ?fb {
GRAPH <http://wifo5-04.informatik.uni-mannheim.de/factbook/all/countries> {
?c a factbook:Country # ; rdfs:label "Anguilla"
BIND(IRI(REPLACE(str(?c), "http://wifo5-04.informatik.uni-mannheim.de/factbook/resource", "http://www4.wiwiss.fu-berlin.de/factbook/resource")) AS ?fb) .
}}
ORDER BY ?fb
# Limit the number of countries if you get timeouts.
# LIMIT 10
# OFFSET 0
}
SERVICE <http://live.dbpedia.org/sparql> {
# Get sameAs with geonames from dbpedia endpoint as the LOD Cache
# is miraculously incomplete.
?dbp a dbo:Country; (owl:sameAs | ^owl:sameAs) ?fb;
(owl:sameAs | ^owl:sameAs) ?geo .
FILTER(strStarts(str(?geo),"http://sws.geonames.org/")) .
}
?geo geonames:alternateName ?name .
FILTER(lang(?name)="en" || lang(?name)="")
}
ORDER BY ?dbp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment