0

I use SPARQLWrapper to create SPARQL queries, but I don't know how to debug the following error message:

Warning (from warnings module):
  File "D:\Python27\lib\site-packages\sparqlwrapper-1.5.2-py2.7.egg\SPARQLWrapper\Wrapper.py", line 550
RuntimeWarning: unknown response content type, returning raw response...

Traceback (most recent call last):
  File "D:\Python27\testwrapper.py", line 31, in <module>
    if (len(results["results"]["bindings"]) == 0):
AttributeError: addinfourl instance has no attribute '__getitem__'

This is my code:

from SPARQLWrapper import SPARQLWrapper,JSON
sparql = SPARQLWrapper('http://thedatahub.org/dataset/semanticquran');

queryString = """
PREFIX  dc:   <http://purl.org/dc/elements/1.1/>
PREFIX  foaf: <http://xmlns.com/foaf/0.1/>
PREFIX  olia-ar: <http://purl.org/olia/arabic_khoja.owl#>
PREFIX  dcterms: <http://purl.org/dc/terms/>
PREFIX  rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX  owl:  <http://www.w3.org/2002/07/owl#>
PREFIX  xsd:  <http://www.w3.org/2001/XMLSchema#>
PREFIX  lexvo: <http://lexvo.org/id/iso639-3/>
PREFIX  rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX  gold: <http://purl.org/linguistics/gold/>
PREFIX  skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX  qvoc: <http://www.nlp2rdf.org/quranvocab#>
SELECT DISTINCT  ?wordText ?pos
WHERE
 { ?wordPart rdf:type qvoc:LexicalItem .
    ?wordPart gold:Root "smw" .
    ?wordPart dcterms:isPartOf ?word .
    ?wordPart gold:PartOfSpeechProperty ?pos .
    ?word rdf:type qvoc:Word .
    ?word skos:prefLabel ?wordText
  }
"""

sparql.setQuery(queryString)
sparql.setReturnFormat(JSON)
results = sparql.query().convert()
if (len(results["results"]["bindings"]) == 0):
  print "No results found."
else:
  for result in results["results"]["bindings"]:
     print result["wordText"]["value"]

Any Help?

Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265

1 Answers1

1

The return format is not JSON, so the setReturnFormat call is not working as expected:

Encode the return value depending on the return format:

  • in the case of XML, a DOM top element is returned;
  • in the case of JSON, a simplejson conversion will return a dictionary;
  • in the case of RDF/XML, the value is converted via RDFLib into a Graph instance.

In all other cases the input simply returned.

Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265