Search code examples
rdfsparqlsemantic-webdbpedialinked-data

Sparql query to DBpedia


Can we get list of places in US from DBpedia using a SPARQL query? I have tried the following query, but it (by design) only gives the English abstracts. I want the names of places in the US. How can I achieve this?

SELECT ?abstract
FROM NAMED <http://dbpedia.org>
WHERE { <http://dbpedia.org/resource/Lists_of_populated_places_in_the_United_States> <http://dbpedia.org/ontology/abstract> ?abstract.
FILTER langMatches( lang(?abstract), 'en') }

Solution

  • It's usually easiest to write some these queries iteratively, exploring the data as you go along. For instance, in this case, you could start with:

    select ?place where {
      ?place a dbpedia-owl:PopulatedPlace
    }
    limit 100
    

    SPARQL results

    That's not just places in the United States, but you can browse the results until you find one that is. Then you can examine it and see how you might identify such places. In this case, you might find Furnace, West Virginia, and note that it the value dbpedia:United_States for the property dbpedia-owl:country. Thus we can refine the query:

    select ?place where {
      ?place a dbpedia-owl:PopulatedPlace ;
             dbpedia-owl:country dbpedia:United_States 
    }
    limit 100
    

    SPARQL results

    That's looking much better, but you said you wanted the names of the places, not the actual IRIs that identify them. Based on your filter, it looks like you just want the English names. Then we refine further:

    select ?label where {
      ?place a dbpedia-owl:PopulatedPlace ;
             dbpedia-owl:country dbpedia:United_States ;
             rdfs:label ?label
      filter langMatches( lang(?label), 'en' )
    }
    limit 100
    

    SPARQL results

    Those names are language tagged literals. If you only want the string part, you can do that in the variable projection:

    select (str(?label) as ?strLabel) where {
      ?place a dbpedia-owl:PopulatedPlace ;
             dbpedia-owl:country dbpedia:United_States ;
             rdfs:label ?label
      filter langMatches( lang(?label), 'en' )
    }
    limit 100
    

    SPARQL results