Search code examples
databasesparqldbpedia

How to remove parenthesis in dbpedia query result in SPARQL?


I am trying to query DBpedia with SPARQL by using CONSTRUCT to get a list of countries and their capitals and parse the result back in Turtle format:

CONSTRUCT {
  ?country dbo:capital ?city.
  ?country a dbo:Country.
} WHERE {
  ?country dbo:capital ?city.
  ?country a dbo:Country
}

However, when there are parenthesis () or a _ in the country name, the result does not use the prefixes properly.

Example:

dbr:Virginia  dbo:capital  <http://dbpedia.org/resource/Richmond,_Virginia>

Is there any automated way to fix this? I don't really look forward to fixing this manually...


Solution

  • To be honest, I don't understand why you want to do this and it remains open how you want to handle cases which aren't (can't be) prefixed in Turtle syntax. But, to give you a rough idea:

    CONSTRUCT {
      ?country dbo:capital ?city. 
      ?country a dbo:Country.
    } WHERE {
      ?country dbo:capital ?city_tmp. 
      ?country a dbo:Country
      ## here you would handle the "fix", e.g. use a regex and find some replacement
      BIND(uri(REPLACE(str(?city_tmp), ",_", "_", "i")) AS ?city)
    }
    

    You can see in the query that I used just a dummy regex matching ,_ and a replacement string _. For example, http://dbpedia.org/resource/Nancy,_France will be replaced to dbr:Nancy_France