1

我想编写一个 Sparql 查询来获取有关特定维基百科文章的信息。我是一个 sparql 新手,可以使用一些关于使用 zlist 作为参数并将其传递给它的文章列表的见解,就像 .format 在 python 中所做的那样。

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

例如,我尝试了这个不成功:

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)

但是,我能够让这些工作:

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"])

注意:这个 stackoverflow 答案这个以及这个 和这个以及这个Github 问题以及这个问题是最有帮助的。

4

1 回答 1

1

在这种情况下,我的列表项需要用单引号括起来。并且必须删除 FILTER 行中变量周围的 { }。

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)
于 2020-01-21T18:22:59.760 回答