Python SPARQLWrapper.SPARQLWrapper() Examples

The following are 29 code examples of SPARQLWrapper.SPARQLWrapper(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module SPARQLWrapper , or try the search function .
Example #1
Source File: sparql.py    From grlc with MIT License 6 votes vote down vote up
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] 
Example #2
Source File: sparql.py    From entity2rec with Apache License 2.0 6 votes vote down vote up
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 
Example #3
Source File: sparql.py    From entity2rec with Apache License 2.0 6 votes vote down vote up
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) 
Example #4
Source File: sparql.py    From entity2vec with Apache License 2.0 6 votes vote down vote up
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) 
Example #5
Source File: sparql.py    From entity2vec with Apache License 2.0 6 votes vote down vote up
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 
Example #6
Source File: wikidata.py    From ontobio with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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 
Example #7
Source File: sparql_ontol_utils.py    From ontobio with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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 
Example #8
Source File: sparql_ontol_utils.py    From ontobio with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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 
Example #9
Source File: scorer.py    From entity2vec with Apache License 2.0 6 votes vote down vote up
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']) 
Example #10
Source File: main.py    From falcon2.0 with MIT License 6 votes vote down vote up
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 
Example #11
Source File: lexiconEvaluation.py    From QALD with MIT License 6 votes vote down vote up
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 
Example #12
Source File: sparql_transformer.py    From kgx with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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 
Example #13
Source File: local_to_wiki.py    From entity2vec with Apache License 2.0 5 votes vote down vote up
def get_wiki_id_from_url(url):

    sparql = SPARQLWrapper("http://3cixty.eurecom.fr/dbpedia")

    sparql.setQuery("""select ?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'])

#def get_wiki_id_from_url_local(url, wiki_dict):

 #   return wiki_dict[url] 
Example #14
Source File: scorer.py    From entity2vec with Apache License 2.0 5 votes vote down vote up
def get_url_from_id(wiki_id):

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

    sparql.setQuery("""
    PREFIX db: <http://dbpedia.org/resource/>
    SELECT *
    WHERE { ?s dbo:wikiPageID %d }
    """ %wiki_id)

    sparql.setReturnFormat(JSON)

    return str(sparql.query().convert()['results']['bindings'][0]['s']['value']) 
Example #15
Source File: sparql.py    From webvectors with GNU General Public License v3.0 5 votes vote down vote up
def getdbpediaimage(query, cache):
    query = query
    if '::' in query:
        query = ' '.join([w.capitalize() for w in query.split('::')])
    else:
        query = query.capitalize()

    if query in cache:
        return cache[query]
    else:
        # return None
        sparql = SPARQLWrapper("http://dbpedia.org/sparql")
        sparql.setQuery("""
        SELECT DISTINCT ?e ?pic
        WHERE {
            ?e rdfs:label "%s"@en .
            ?e <http://dbpedia.org/ontology/thumbnail> ?pic .
                }
        """ % query)
        sparql.setReturnFormat(JSON)
        try:
            results = sparql.query().convert()
        except SPARQLExceptions.QueryBadFormed:
            return None
        if len(results["results"]["bindings"]) > 0:
            image = results["results"]["bindings"][0]["pic"]["value"]
            image = image.replace('http://', 'https://')
            data = open(root + cachefile, 'a')
            data.write(query + '\t' + image + '\n')
            data.close()
            return image
        else:
            data = open(root + cachefile, 'a')
            data.write(query + '\tNone\n')
            data.close()
            return None 
Example #16
Source File: distant_s.py    From NeuralTripleTranslation with Apache License 2.0 5 votes vote down vote up
def get_country_description(query):
    sparql = SPARQLWrapper("http://dbpedia.org/sparql")
    sparql.setReturnFormat(JSON)

    sparql.setQuery(query)  # the previous query as a literal string

    return sparql.queryAndConvert() 
Example #17
Source File: wd_sparql.py    From biolink-api with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def run_sparql_query(q,limit=10):
    """
    Run a given SPARQL query over the Wikidata SPARQL endpoint
    """
    full_sparql = "{}\n{}\nLIMIT {}".format(prefix_map.gen_header(),q,limit)
    logging.info("FULL:"+full_sparql)
    sparql.setQuery(full_sparql)
    sparql.setReturnFormat(SPARQLWrapper.JSON)
    results = sparql.query().convert()
    return results 
Example #18
Source File: uniprot_sparql.py    From biolink-api with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def run_sparql_query(q,limit=10):
    full_sparql = "{}\n{}\nLIMIT {}".format(prefix_map.gen_header(),q,limit)
    logging.info("FULL:"+full_sparql)
    sparql.setQuery(full_sparql)
    sparql.setReturnFormat(SPARQLWrapper.JSON)
    results = sparql.query().convert()
    return results 
Example #19
Source File: lego_sparql_util.py    From biolink-api with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def lego_query(q,limit=10):
    full_sparql = "{}\n{}\nLIMIT {}".format(prefix_map.gen_header(),q,limit)
    print("FULL:"+full_sparql)
    sparql.setQuery(full_sparql)
    sparql.setReturnFormat(SPARQLWrapper.JSON)
    results = sparql.query().convert()
    return results 
Example #20
Source File: wdaccess.py    From starsem2018-entity-linking with Apache License 2.0 5 votes vote down vote up
def set_backend(backend_url):
    global sparql
    sparql = SPARQLWrapper(backend_url)
    sparql.setReturnFormat(JSON)
    sparql.setMethod("GET")
    sparql.setTimeout(wdaccess_p.get('timeout', 40)) 
Example #21
Source File: wikidata_ontology.py    From ontobio with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def create_from_hub(self, hub_id):
        if hub_id.find(":") == -1:
            hub_id = 'wd:' + hub_id
        if hub_id.startswith('http'):
            hub_id = '<' + hub_id + '>'
        q = """
        PREFIX wd: <http://www.wikidata.org/entity/> 
        CONSTRUCT {{
        ?cls rdfs:label ?clsLabel .
        ?cls a owl:Class .
        ?superClass a owl:Class .
        ?superClass rdfs:label ?superClassLabel .
        ?cls rdfs:subClassOf ?superClass
        }}
        WHERE {{
         {{  {{ ?cls wdt:P279* {hub_id} }} UNION
             {{ {hub_id} wdt:P279* ?cls }} }}
        ?cls wdt:P279 ?superClass
        SERVICE wikibase:label {{
          bd:serviceParam wikibase:language "en" .
        }}
        }}
        """.format(hub_id=hub_id)
        logger.info("QUER={}".format(q))
        sparql = SPARQLWrapper("http://query.wikidata.org/sparql")
        sparql.setQuery(q)
        sparql.setReturnFormat(RDF)
        rg = sparql.query().convert()
        logger.info("RG={}".format(rg))
        ont = rdfgraph_to_ontol(rg)
        self.graph = ont.graph 
Example #22
Source File: virtuoso_wrapper.py    From plastering with MIT License 5 votes vote down vote up
def __init__(self, sparql_url, brick_version, base_ns='', load_schema=True):
        BRICK_VERSION = brick_version
        self.sparql_url = sparql_url
        self.sparql = SPARQLWrapper(endpoint=self.sparql_url,
                                    updateEndpoint=self.sparql_url + '-auth')
        self.sparql.queryType= SELECT
        self.sparql.setCredentials('dba', 'dba')
        self.sparql.setHTTPAuth(DIGEST)
        if not base_ns:
            base_ns = 'http://example.com/'
        self.base_graph = 'urn:' + str(gen_uuid())
        self.sparql.addDefaultGraph(self.base_graph)
        self.namespaces = {
            '': BASE,
            'brick':BRICK,
            'bf': BF,
            'rdfs': RDFS,
            'rdf': RDF,
            'owl': OWL,
            'foaf': FOAF
        }
        sparql_prefix = ''
        #for prefix, ns in self.namespaces.items():
        #    ns_n3 = ns.uri.n3()
        #    sparql_prefix += 'prefix {0}: {1}\n'.format(prefix, ns_n3)
        #sparql_prefix += '\n'

        self._init_brick_constants()
        if load_schema:
            self.load_schema() 
Example #23
Source File: evaluateFalconAPI_entities.py    From falcon2.0 with MIT License 5 votes vote down vote up
def SPARQL_call(query,endpoint):
    sparql = SPARQLWrapper(endpoint)
    sparql.setQuery(query)
    sparql.setReturnFormat(JSON)
    results = sparql.query().convert()
    return results 
Example #24
Source File: evaluateFalconAPI.py    From falcon2.0 with MIT License 5 votes vote down vote up
def SPARQL_call(query,endpoint):
    sparql = SPARQLWrapper(endpoint)
    sparql.setQuery(query)
    sparql.setReturnFormat(JSON)
    results = sparql.query().convert()
    return results 
Example #25
Source File: MyChem.py    From dipper with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def execute_query(query):
        endpoint = SPARQLWrapper('https://query.wikidata.org/sparql')
        endpoint.setQuery(query)
        endpoint.setReturnFormat(JSON)
        return endpoint.query().convert() 
Example #26
Source File: wikidata.py    From knowledge-net with MIT License 5 votes vote down vote up
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) 
Example #27
Source File: sparql_transformer.py    From kgx with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def load_networkx_graph(self, rdfgraph: rdflib.Graph = None, predicates: Set[URIRef] = None, **kwargs: Dict) -> None:
        """
        Fetch all triples using the specified predicates and add them to networkx.MultiDiGraph.

        Parameters
        ----------
        rdfgraph: rdflib.Graph
            A rdflib Graph (unused)
        predicates: set
            A set containing predicates in rdflib.URIRef form
        kwargs: dict
            Any additional arguments.
            Ex: specifying 'limit' argument will limit the number of triples fetched.

        """
        for predicate in predicates:
            sparql = SPARQLWrapper(self.url)
            association = '<{}>'.format(predicate)
            query = render(self.count_query, {'association': association})
            logging.debug(query)
            sparql.setQuery(query)
            sparql.setReturnFormat(JSON)
            results = sparql.query().convert()
            count = int(results['results']['bindings'][0]['triples']['value'])
            logging.info("Expected triples for query: {}".format(count))
            step = 1000
            start = 0
            for i in range(step, count + step, step):
                end = i
                query = render(self.edge_query, {'association': association, 'offset': start, 'limit':step})
                sparql.setQuery(query)
                logging.debug("Fetching triples with predicate {}".format(predicate))
                results = sparql.query().convert()
                node_list = set()
                for r in results['results']['bindings']:
                    node_list.add("<{}>".format(r['subject']['value']))
                    node_list.add("<{}>".format(r['object']['value']))
                start = end
                self.load_nodes(node_list)
                for r in results['results']['bindings']:
                    s = r['subject']['value']
                    p = r['predicate']['value']
                    o = r['object']['value']
                    self.add_edge(s, o, p)
                    # TODO: preserve edge properties

                if 'limit' in kwargs and i > kwargs['limit']:
                    break

        self.categorize() 
Example #28
Source File: sparql_transformer.py    From kgx with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def load_nodes(self, node_set: Set) -> None:
        """
        Load nodes into networkx.MultiDiGraph.

        This method queries the SPARQL endpoint for all triples where nodes in the
        node_set is a subject.

        Parameters
        ----------
        node_set: list
            A list of node CURIEs

        """
        node_generator = self._grouper(node_set, 10000)
        nodes = next(node_generator, None)
        while nodes is not None:
            logging.info("Fetching properties for {} nodes".format(len(nodes)))
            nodes = filter(None, nodes)
            # TODO: is there a better way to fetch node properties?
            query = self.get_node_properties_query.format(curie_list=' '.join(nodes))
            logging.info(query)
            sparql = SPARQLWrapper(self.url)
            sparql.setRequestMethod(POSTDIRECTLY)
            sparql.setMethod("POST")
            sparql.setQuery(query)
            sparql.setReturnFormat(JSON)
            node_results = sparql.query().convert()
            d = {}
            for r in node_results['results']['bindings']:
                if r['object']['type'] != 'bnode':
                    subject = r['subject']['value']
                    object = r['object']['value']
                    predicate = r['predicate']['value']
                    if predicate.startswith('bl:'):
                        predicate = predicate.split(':')[1]
                    if subject not in d:
                        d[subject] = {}
                    d[subject][predicate] = object

            for node, attr_dict in d.items():
                for key, value in attr_dict.items():
                    self.add_node_attribute(node, key=key, value=value)
            d.clear()
            nodes = next(node_generator, None) 
Example #29
Source File: sparqldump.py    From Skosify with MIT License 4 votes vote down vote up
def query_to_file(endpoint, graph, output, format, limit=0, offset=0, ordered=False):
    logging.debug("Querying to file %s", output)
    sparql = SPARQLWrapper(endpoint)

    if graph:
        graph = "GRAPH <%s>" % graph
    else:
        graph = ""

    if limit:
        extra = "LIMIT %s OFFSET %s" % (limit, offset)
        if ordered:
            extra = "ORDER BY ?s ?p ?o " + extra
    else:
        extra = ""

    query = """
    CONSTRUCT { ?s ?p ?o }
    WHERE {
      %s { ?s ?p ?o . }
    } %s
  """ % (graph, extra)
    logging.debug("query: %s", query)
    sparql.setQuery(query)
    sparql.setReturnFormat(FORMATMAP[format])
    response = sparql.query().response

    if output == '-':
        out = sys.stdout
    else:
        out = open(output, "w")

    size = 0
    while True:
        data = response.read(1024)
        size += len(data)
        if len(data) == 0:
            break
        out.write(data)
    out.close()
    logging.info("Wrote %d bytes to output file %s", size, output)
    return size