0

I am using the following sparql query using sparqlwrapper as follows.

from SPARQLWrapper import SPARQLWrapper, JSON
sparql = SPARQLWrapper("http://live.dbpedia.org/sparql")
sparql.setReturnFormat(JSON)
my_category = 'dbc:Meteorological_concepts'
sparql.setQuery(f" ASK {{ {my_category}  skos:broader{{1,3}} dbc:Medicine }} ")
results = sparql.query().convert()
print(results['boolean'])

As mentioned above it works fine with categories that do not have brackets (e.g., dbc:Meteorological_concepts). However, when I enter a category with brackets (i.e my_category = dbc:Elasticity_(physics)) I get the following error.

b"Virtuoso 37000 Error SP030: SPARQL compiler, line 4: syntax error at 'physics' before ')'\n\nSPARQL query:\ndefine sql:big-data-const 0 \n#output-format:application/sparql-results+json\n\n    ASK { dbc:Elasticity_(physics) skos:broader{1,3} dbc:Medicine }\n"
CRITICAL: Exiting due to uncaught exception <class 'SPARQLWrapper.SPARQLExceptions.QueryBadFormed'>

Is there a way to resolve this issue.

I am happy to provide more details if needed.

EmJ
  • 4,398
  • 9
  • 44
  • 105
  • This is an issue with DBpedia URIs. See [this question](https://stackoverflow.com/questions/18232010/sparql-queries-with-round-brackets-throw-exception) – chrisis Mar 17 '19 at 12:39
  • @chrisis thanks a lot. It would be really great if you could post it as an answer :) – EmJ Mar 17 '19 at 13:11
  • 1
    Try to use full URIs: ``. – Stanislav Kralin Mar 17 '19 at 13:24
  • @EmJ Sorry for my delayed comment. I am getting `urllib.error.HTTPError: HTTP Error 502: Bad Gateway`. Can you please give a suggestion to make it run? – Darkmoor Sep 27 '22 at 09:53

1 Answers1

3

I am rewriting what @StanislavKralin mentioned in the above comment. I always try to use full URL in the SPARQL code, particularly when there is special character in SPARQL query.

from SPARQLWrapper import SPARQLWrapper, JSON
sparql = SPARQLWrapper("http://live.dbpedia.org/sparql")
sparql.setReturnFormat(JSON)
my_category = '<http://dbpedia.org/resource/Category:Elasticity_(physics)>'
sparql.setQuery(f" ASK {{ {my_category}  skos:broader{{1,3}} dbc:Medicine }} ")
results = sparql.query().convert()
print(results['boolean'])
Enayat
  • 3,904
  • 1
  • 33
  • 47
  • Sorry for my delayed comment. I am getting `urllib.error.HTTPError: HTTP Error 502: Bad Gateway`. Can you please give a suggestion to make it run? – Darkmoor Sep 27 '22 at 09:49