rene
rene

Reputation: 21

Sparql Query parameterized with string concatenation

I would like to write a Sparql query to obtain information about particular wikipedia articles. I'm a sparql newbie and could use some insights on using zlist as a parameter and passing it a list of articles, as .format would do in python.

SELECT DISTINCT ?lemma ?item 
WHERE {
  ?sitelink schema:about ?item;
    schema:isPartOf <https://de.wikipedia.org/>;
    schema:name ?lemma.
  FILTER (?lemma IN (zlist@de))
}

For example, I tried this unsuccessfully:

from SPARQLWrapper import SPARQLWrapper, JSON
import pandas as pd
sparql = SPARQLWrapper("https://query.wikidata.org/sparql")
sparql.setReturnFormat(JSON)
mylist = "Prato della Valle"

sparql.setQuery("""
SELECT DISTINCT ?item ?lemma ?instance_of 
WHERE {
  ?item wdt:P31 ?instance_of.    
  ?sitelink schema:about ?item;
    schema:isPartOf <https://de.wikipedia.org/>;
    schema:name ?lemma.
    FILTER (?lemma IN ( { %s }@de))
}                  
"""%mylist) 

results = sparql.query().convert()
results_df = pd.io.json.json_normalize(results['results']['bindings'])
print(results_df)

However, I am able to get these to work:

from SPARQLWrapper import SPARQLWrapper, JSON
import pandas as pd

mylist = ["word2vec"]
mystring = '"' + '" "'.join(mylist) + '"'
# mystring = (' '.join('"{0}"'.format(v) for v in mylist) )

sparql = SPARQLWrapper("https://query.wikidata.org/sparql")

sparql.setQuery("""
    SELECT DISTINCT ?item {
    VALUES ?searchTerm { %s }
    SERVICE wikibase:mwapi {
        bd:serviceParam wikibase:api "EntitySearch".
        bd:serviceParam wikibase:endpoint "www.wikidata.org".
        bd:serviceParam mwapi:search ?searchTerm.
        bd:serviceParam mwapi:language "en".
        ?item wikibase:apiOutputItem mwapi:item.
        ?num wikibase:apiOrdinal true.
    }
    ?item (wdt:P279|wdt:P31) ?type
    }
    ORDER BY ?searchTerm ?num                    
""" % mystring ) 
sparql.setReturnFormat(JSON)
results = sparql.query().convert()
results_df = pd.io.json.json_normalize(results['results']['bindings'])
print(results_df)
from SPARQLWrapper import SPARQLWrapper, JSON
sparql = SPARQLWrapper("http://live.dbpedia.org/sparql")
sparql.setReturnFormat(JSON)

my_variable = 'dbc:Meteorological_concepts'

sparql.setQuery(" ASK {{ {}  skos:broader{{1,7}} dbc:Medicine }} ".format(my_variable))
results = sparql.query().convert()
print(results['boolean'])
from SPARQLWrapper import SPARQLWrapper, JSON
from string import Template

sparql = SPARQLWrapper("http://dbpedia.org/sparql")

query = Template("""
    PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
    SELECT ?label
    WHERE { $uri rdfs:label ?label }
""")

sparql.setQuery(query.substitute(uri='<http://dbpedia.org/resource/Asturias>'))
sparql.setReturnFormat(JSON)
results = sparql.query().convert()
for result in results["results"]["bindings"]:
    print (result["label"]["value"])

Note:This stackoverflow answer and this and also this and this and this Github issue along with this one were most helpful.

Upvotes: 1

Views: 1307

Answers (1)

rene
rene

Reputation: 21

In this case, my list items needed to be in single quotes. And the { } around the variable in the FILTER line had to be dropped.

from SPARQLWrapper import SPARQLWrapper, JSON
import pandas as pd
sparql = SPARQLWrapper("https://query.wikidata.org/sparql")
sparql.setReturnFormat(JSON)

preptitles = ['Prato della Valle']
mystring = ','.join('"{0}"@de'.format(w) for w in preptitles) 

sparql.setQuery("""
SELECT DISTINCT ?item ?lemma ?instance_of 
WHERE {
  ?item wdt:P31 ?instance_of.    
  ?sitelink schema:about ?item;
    schema:isPartOf <https://de.wikipedia.org/>;
    schema:name ?lemma.
    FILTER (?lemma IN (%s))
}                  
"""%mylist) 

results = sparql.query().convert()
results_df = pd.io.json.json_normalize(results['results']['bindings'])
print(results_df)

Upvotes: 1

Related Questions