本文整理汇总了Python中SPARQLWrapper.SPARQLWrapper方法的典型用法代码示例。如果您正苦于以下问题:Python SPARQLWrapper.SPARQLWrapper方法的具体用法?Python SPARQLWrapper.SPARQLWrapper怎么用?Python SPARQLWrapper.SPARQLWrapper使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SPARQLWrapper
的用法示例。
在下文中一共展示了SPARQLWrapper.SPARQLWrapper方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: getResponseText
# 需要导入模块: import SPARQLWrapper [as 别名]
# 或者: from SPARQLWrapper import SPARQLWrapper [as 别名]
def getResponseText(endpoint, query, requestedMimeType):
"""Returns the result and mimetype of executing the given query against
the given endpoint.
Keyword arguments:
endpoint - URL of sparql endpoint
query - SPARQL query to be executed
requestedMimeType Type of content requested. can be:
'text/csv; q=1.0, */*; q=0.1'
'application/json'
etc.
"""
retFormat = _mimeTypeToSparqlFormat(requestedMimeType)
client = SPARQLWrapper(endpoint)
client.setQuery(query)
client.setReturnFormat(retFormat)
client.setCredentials(static.DEFAULT_ENDPOINT_USER, static.DEFAULT_ENDPOINT_PASSWORD)
result = client.queryAndConvert()
if retFormat==JSON:
result = jsonify(result)
return result, MIME_FORMAT[retFormat]
示例2: askClassProperty
# 需要导入模块: import SPARQLWrapper [as 别名]
# 或者: from SPARQLWrapper import SPARQLWrapper [as 别名]
def askClassProperty(uri):
sparql = SPARQLWrapper(endpoint)
sparql.setQuery("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX owl: <http://www.w3.org/2002/07/owl#> ASK WHERE {<"+uri+"> rdf:type owl:Class}")
sparql.setReturnFormat(JSON)
results = sparql.query().convert()
#print results
for result in results:
try:
string = str(results[result])
if "False" in string:
return False
if "True" in string:
return True
except:
pass
return False
示例3: check_relation_range_type
# 需要导入模块: import SPARQLWrapper [as 别名]
# 或者: from SPARQLWrapper import SPARQLWrapper [as 别名]
def check_relation_range_type(relation,qType):
return True
sparql = SPARQLWrapper(wikidataSPARQL)
sparql.setQuery("""
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
ASK {<"""+relation+"""> rdfs:range <"""+qType+"""> }
""")
sparql.setReturnFormat(JSON)
results1 = sparql.query().convert()
if results1['boolean']:
return True
else:
sparql.setQuery("""
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
ASK {<"""+relation+"""> rdfs:range ?range. ?range rdfs:subClassOf ?t. ?t rdfs:subClassOf <"""+qType+""">}
""")
sparql.setReturnFormat(JSON)
results2 = sparql.query().convert()
if results2['boolean']:
return True
else:
return False
return results1['boolean']
#To split the surface forms in case of possessive forms
示例4: run_sparql
# 需要导入模块: import SPARQLWrapper [as 别名]
# 或者: from SPARQLWrapper import SPARQLWrapper [as 别名]
def run_sparql(q):
# TODO: select endpoint based on ontology
#sparql = SPARQLWrapper("http://rdf.geneontology.org/sparql")
logger.info("Connecting to sparql endpoint...")
sparql = SPARQLWrapper("http://sparql.hegroup.org/sparql")
logger.info("Made wrapper: {}".format(sparql))
# TODO: iterate over large sets?
full_q = q + ' LIMIT 250000'
sparql.setQuery(q)
sparql.setReturnFormat(JSON)
logger.info("Query: {}".format(q))
results = sparql.query().convert()
bindings = results['results']['bindings']
logger.info("Rows: {}".format(len(bindings)))
for r in bindings:
curiefy(r)
return bindings
示例5: run_sparql_on
# 需要导入模块: import SPARQLWrapper [as 别名]
# 或者: from SPARQLWrapper import SPARQLWrapper [as 别名]
def run_sparql_on(q, ontology):
"""
Run a SPARQL query (q) on a given Ontology (Enum EOntology)
"""
logger.info("Connecting to " + ontology.value + " SPARQL endpoint...")
sparql = SPARQLWrapper(ontology.value)
logger.info("Made wrapper: {}".format(sparql))
sparql.setQuery(q)
sparql.setReturnFormat(JSON)
logger.info("Query: {}".format(q))
results = sparql.query().convert()
bindings = results['results']['bindings']
logger.info("Rows: {}".format(len(bindings)))
for r in bindings:
curiefy(r)
return bindings
示例6: run_sparql
# 需要导入模块: import SPARQLWrapper [as 别名]
# 或者: from SPARQLWrapper import SPARQLWrapper [as 别名]
def run_sparql(q):
# TODO: select endpoint based on ontology
logger.info("Connecting to sparql endpoint...")
sparql = SPARQLWrapper("http://query.wikidata.org/sparql")
logger.info("Made wrapper: {}".format(sparql))
# TODO: iterate over large sets?
full_q = q + ' LIMIT ' + str(LIMIT)
sparql.setQuery(q)
sparql.setReturnFormat(JSON)
logger.info("Query: {}".format(q))
results = sparql.query().convert()
bindings = results['results']['bindings']
logger.info("Rows: {}".format(len(bindings)))
for r in bindings:
curiefy(r)
return bindings
示例7: __init__
# 需要导入模块: import SPARQLWrapper [as 别名]
# 或者: from SPARQLWrapper import SPARQLWrapper [as 别名]
def __init__(self, entities, config_file, dataset, endpoint, default_graph):
self.entities = entities # file containing a list of entities
self.dataset = dataset
self.wrapper = SPARQLWrapper(endpoint)
self.wrapper.setReturnFormat(JSON)
if default_graph:
self.default_graph = default_graph
self.wrapper.addDefaultGraph(self.default_graph)
self.query_prop = "SELECT ?s ?o WHERE {?s %s ?o. }"
self.query_prop_uri = "SELECT ?s ?o WHERE {?s %s ?o. FILTER (?s = %s)}"
self._define_properties(config_file)
示例8: get_uri_from_wiki_id
# 需要导入模块: import SPARQLWrapper [as 别名]
# 或者: from SPARQLWrapper import SPARQLWrapper [as 别名]
def get_uri_from_wiki_id(wiki_id):
sparql = SPARQLWrapper("http://dbpedia.org/sparql")
sparql.setQuery("""select ?s where {?s <http://dbpedia.org/ontology/wikiPageID> %d
}""" % int(wiki_id))
sparql.setReturnFormat(JSON)
try:
uri = sparql.query().convert()['results']['bindings'][0]['s']['value']
except:
uri = None
return uri
示例9: __init__
# 需要导入模块: import SPARQLWrapper [as 别名]
# 或者: from SPARQLWrapper import SPARQLWrapper [as 别名]
def __init__(self, entities, config_file, dataset, endpoint, default_graph, entity_class):
self.entities = entities # file containing a list of entities
self.dataset = dataset
self.wrapper = SPARQLWrapper(endpoint)
self.wrapper.setReturnFormat(JSON)
if default_graph:
self.default_graph = default_graph
self.wrapper.addDefaultGraph(self.default_graph)
self.entity_class = entity_class
self.query_prop = "SELECT ?s ?o WHERE {?s %s ?o. }"
self.query_prop_uri = "SELECT ?s ?o WHERE {?s %s ?o. FILTER (?s = %s)}"
self._define_properties(config_file)
示例10: get_uri_from_wiki_id
# 需要导入模块: import SPARQLWrapper [as 别名]
# 或者: from SPARQLWrapper import SPARQLWrapper [as 别名]
def get_uri_from_wiki_id(wiki_id):
sparql = SPARQLWrapper("http://dbpedia.org/sparql")
sparql.setQuery("""select ?s
where { ?s <http://dbpedia.org/ontology/wikiPageID> %d }""" % int(wiki_id))
sparql.setReturnFormat(JSON)
try:
uri = sparql.query().convert()['results']['bindings'][0]['s']['value']
except:
uri = None
return uri
示例11: get_id_from_label
# 需要导入模块: import SPARQLWrapper [as 别名]
# 或者: from SPARQLWrapper import SPARQLWrapper [as 别名]
def get_id_from_label(label):
#print label
label = label.decode('utf8')
sparql = SPARQLWrapper("http://dbpedia.org/sparql")
url = '<http://dbpedia.org/resource/'+label+'>'
sparql.setQuery("""
SELECT ?uri ?id WHERE {
?uri <http://dbpedia.org/ontology/wikiPageID> ?id.
FILTER (?uri = %s) }""" %url)
sparql.setReturnFormat(JSON)
return int(sparql.query().convert()['results']['bindings'][0]['id']['value'])
示例12: query
# 需要导入模块: import SPARQLWrapper [as 别名]
# 或者: from SPARQLWrapper import SPARQLWrapper [as 别名]
def query(self, q: str) -> Dict:
"""
Query a SPARQL endpoint.
Parameters
----------
q: str
The query string
Returns
-------
dict
A dictionary containing results from the query
"""
sparql = SPARQLWrapper(self.url)
sparql.setQuery(q)
sparql.setReturnFormat(JSON)
logging.info("Query: {}".format(q))
results = sparql.query().convert()
bindings = results['results']['bindings']
logging.info("Rows fetched: {}".format(len(bindings)))
return bindings
示例13: request
# 需要导入模块: import SPARQLWrapper [as 别名]
# 或者: from SPARQLWrapper import SPARQLWrapper [as 别名]
def request(subject, set_objects):
try:
triples = []
wikidata_response = db.get(subject, None)
if not wikidata_response is None:
for t in wikidata_response.split("\n"):
subject = t.split("\t")[0]
property = t.split("\t")[1]
object = t.split("\t")[2]
if object in set_objects:
triples.append((subject, property, object))
else:
sparql = SPARQLWrapper(ENDPOINT)
query = get_query_from_subjects_only(subject)
sparql.setQuery(query)
sparql.setReturnFormat(JSON)
data = sparql.query().convert()
triples_2_store = ""
for p in data["results"]["bindings"]:
object = p["o"]["value"].split("/")[-1]
subject = p["s"]["value"].split("/")[-1]
property = p["relation"]["value"].split("/")[-1]
triples_2_store = triples_2_store + subject + "\t" + property + "\t" + object + "\n"
if object in set_objects:
triples.append((subject, property, object))
if triples_2_store != "":
db[subject] = triples_2_store.rstrip("\n")
return triples
except urllib.error.HTTPError:
time.sleep(1)
return request(subject, set_objects)
示例14: execute_query
# 需要导入模块: import SPARQLWrapper [as 别名]
# 或者: from SPARQLWrapper import SPARQLWrapper [as 别名]
def execute_query(query):
endpoint = SPARQLWrapper('https://query.wikidata.org/sparql')
endpoint.setQuery(query)
endpoint.setReturnFormat(JSON)
return endpoint.query().convert()
示例15: SPARQL_call
# 需要导入模块: import SPARQLWrapper [as 别名]
# 或者: from SPARQLWrapper import SPARQLWrapper [as 别名]
def SPARQL_call(query,endpoint):
sparql = SPARQLWrapper(endpoint)
sparql.setQuery(query)
sparql.setReturnFormat(JSON)
results = sparql.query().convert()
return results