def distance_one_query(id1, distance_one): query = ( ' SELECT distinct ?p ?id2 WHERE { <http://dbpedia.org/resource/' + id1 + '> ?p ?id2\ . ' + suffixes_dbpedia_0 + ' FILTER (!regex(str(?pl), "Wikipage","i")) . FILTER (!regex(str(?pl), \ "abstract","i")) . }') query_date = ( ' SELECT distinct ?p ?id2 WHERE { <http://dbpedia.org/resource/' + id1 + '> ?p ?id2\ . ' + suffixes_dbpedia_0 + ' FILTER (!regex(str(?pl), "Wikipage","i")) . FILTER (!regex(str(?pl), \ "abstract","i")) . FILTER (!regex(str(?pl), "Date","i")). FILTER (!regex(str(?pl), "Year","i"))}' ) # print query try: result = sparql.query(sparql_endpoint, query) q1_values = [sparql.unpack_row(row_result) for row_result in result] # print query except: result = sparql.query(sparql_endpoint, query_date) q1_values = [sparql.unpack_row(row_result) for row_result in result] # print query_date if q1_values: # print q1_values for vals in q1_values: vals_0 = vals[0].split('/')[-1] if vals_0 not in unwanted_predicates: if isinstance(vals[1], basestring): if '/' in vals[1]: distance_one.append( [id1, vals_0, vals[1].split('/')[-1]]) else: distance_one.append([id1, vals_0, vals[1]]) return distance_one
def get_predicate_wikidata_sameas(predicate: str): q = "ASK WHERE { <http://dbpedia.org/ontology/" + predicate + "> ?p ?o. }" result = sparql.query(DBPEDIA_ENDPOINT, q) is_ontology = result.hasresult() predicate_uri = f'<http://dbpedia.org/{"ontology" if is_ontology else "property"}/{predicate}>' q = "SELECT * WHERE { " + predicate_uri + " <http://www.w3.org/2002/07/owl#equivalentProperty> ?o. FILTER regex(str(?o), \"wikidata\")}" result = sparql.query(DBPEDIA_ENDPOINT, q) if result.hasresult(): return sparql.unpack_row(result[0])[0] else: return None
def get_description(entity_type): query = 'SELECT distinct ?label WHERE { <http://dbpedia.org/' + entity_type + '> rdfs:comment ?label . \ FILTER langMatches( lang(?label), "EN" ) . }' result = sparql.query(sparql_dbpedia, query) type_comment = [sparql.unpack_row(row_result) for row_result in result] query = 'SELECT distinct ?label WHERE { <http://dbpedia.org/' + entity_type + '> rdfs:label ?label . \ FILTER langMatches( lang(?label), "EN" ) . }' result = sparql.query(sparql_dbpedia, query) type_label = [sparql.unpack_row(row_result) for row_result in result] return type_comment, type_label
def get_rank(id, entity_1, entity_2, label): query_1 = prefix + entity_1 + '>' + suffix query_2 = prefix + entity_2 + '>' + suffix # print query_1, query_2 # try: result_1 = sparql.query(sparql_endpoint, query_1) result_2 = sparql.query(sparql_endpoint, query_2) value_1 = [sparql.unpack_row(row_result) for row_result in result_1][0][0] value_2 = [sparql.unpack_row(row_result) for row_result in result_2][0][0] score = (value_1 + value_2) return [ id, entity_1.encode('utf-8'), entity_2.encode('utf-8'), label, score ]
def query_for_value(query): row = sparql.query(ENDPOINT, PREFIXES + query).fetchone() try: (a, ) = row return a[0].value except ValueError, e: return None
def get_confidence_rudik(numerator_query, denominator_query, pos_neg, examples): rule_true = 0 body_true = 0 i = 0 for example in examples: # print i, example # i += 1 # numerator_query_new = numerator_query.replace('?subject','<http://dbpedia.org/resource/'+example[0]+'>') denominator_query_new = denominator_query.replace('?subject','<http://dbpedia.org/resource/'+example[0]+'>') denominator_query_new = denominator_query_new.replace('?object','<http://dbpedia.org/resource/'+example[1]+'>') # numerator_query_new = numerator_query_new.replace('?object','<http://dbpedia.org/resource/'+example[1]+'>') # try: # result = sparql.query(sparql_endpoint, numerator_query_new) # numerator_value = [sparql.unpack_row(row_result) for row_result in result][0][0] # except: # numerator_value = 0 try: result = sparql.query(sparql_endpoint, denominator_query_new) denominator_value = [sparql.unpack_row(row_result) for row_result in result][0][0] except: denominator_value = 0 # print numerator_value, denominator_value # if numerator_value > 0: # rule_true += 1 if denominator_value > 0: body_true += 1 # print numerator_query_new # print denominator_query_new # print "-----------------" # print float(body_true),float(len(examples)) confidence = float(body_true)/float(len(examples)) return confidence
def get_evidence(resource, rules): evidence = [] for rule in rules.split('\n'): if rule: body = rule.split(':-')[1] relations = re.findall(r"(\w*?)\(", body) vars = re.findall(r"\((.*?)\)", body) query = get_evidence_query(relations, vars, resource) # print query result = sparql.query(sparql_endpoint, query) q1_values = [ sparql.unpack_row(row_result) for row_result in result ] # print q1_values if q1_values: for vals in q1_values: try: vals = [ val.split('/')[-1] if '/' in val else val for val in vals ] evid = [vals[i:i + 3] for i in xrange(0, len(vals), 3)] evidence.extend(evid) except: pass return evidence
def predicate_finder(triple_dict): pval_list = [] for k in triple_dict.keys(): q_comment = 'SELECT distinct ?uri ?comment WHERE { ?uri rdfs:comment ?comment . \ FILTER langMatches( lang(?comment), "EN" ). ?comment bif:contains "' + k.split( )[1] + '" .}' q_label = 'SELECT distinct ?uri ?label WHERE { ?uri rdfs:label ?label . ?uri rdf:type rdf:Property . \ FILTER langMatches( lang(?label), "EN" ). ?label bif:contains "' + k.split( )[1] + '" .}' predicate_result = sparql.query(sparql_dbpedia, q_comment) p_values = [sparql.unpack_row(row) for row in predicate_result] if not p_values: predicate_result = sparql.query(sparql_dbpedia, q_label) p_values = [sparql.unpack_row(row) for row in predicate_result] pval_list.append(p_values) return pval_list
def fetch_qualifiers(self): country_list = ['wd:' + r['countryCode'] for r in self.regions] countries = ' '.join(country_list) query = f''' # Retrieve K random statement/qualifier pairs SELECT DISTINCT ?statement ?pq ?wdpqLabel ?qualifier_no_prefix WHERE {{ VALUES (?p) {{ (p:{self.pnode}) }} VALUES ?country {{ { countries } }} ?country ?p ?statement . ?statement ?pq ?pqv . ?wdpq wikibase:qualifier ?pq . BIND(MD5(str(?statement)) as ?random) BIND (STR(REPLACE(STR(?pq), STR(pq:), "")) AS ?qualifier_no_prefix) SERVICE wikibase:label {{ bd:serviceParam wikibase:language "en". }} }} ORDER BY ?random LIMIT 600 ''' self.queries.append(query) results = sparql.query(query) qualifiers = {} for result in results["results"]["bindings"]: # The result contains the same qualifier numerous times, we just overwrite it in the dictionary, so we get just one dictionary # entry per each qualifier pq = result['qualifier_no_prefix']['value'] label = result['wdpqLabel']['value'] qualifiers[pq] = label self.qualifiers = qualifiers
def resolve_title_as_artist_dbpedia(artist_name, sim_threshold=0.5): try: logging.info("Resolving %s as artist using DBpedia" % artist_name) query = sparql.query('http://dbpedia.org/sparql', ''' select ?artist where { ?artist rdf:type ?type . ?type rdf:subClassOf* <http://schema.org/MusicGroup> . ?artist foaf:name "''' + artist_name.decode('utf-8') + '''"@en . } LIMIT 10''') results = query.fetchall() if len(results) < 1: logging.warning("Request to DBpedia returned no results for %s, skipping resolution" % artist_name) return (artist_name, None) title = None maxSimilarity = 0.0 for result in results: resolved_name = (sparql.unpack_row(result)[0].encode('utf-8') .replace('http://dbpedia.org/resource/', '') .replace('_', ' ')) similarity = sentence_similarity(artist_name, urllib.unquote(resolved_name)) if similarity > maxSimilarity: title = resolved_name maxSimilarity = similarity except urllib2.HTTPError, sparql.SparqlException: logging.warning("Request to DBpedia failed for %s, skipping resolution" % artist_name) return (artist_name, None)
def print_result(offset,f): endpoint='http://130.235.17.116:8000/openrdf-sesame/repositories/AAOT' statement=("""PREFIX aaot:<http://cs.lth.se/ontologies/aaot.owl#> select * WHERE { ?name aaot:age ?age . ?name aaot:age_donor ?age_donor . ?name aaot:operation_year ?operation_year ?name aaot:gender ?gen . ?name aaot:survival_time ?survival_time . } limit 1000 offset """ +str(offset) ) result = sparql.query(endpoint,statement) variables = result.variables for row in result.fetchall(): values=sparql.unpack_row(row) i=1 while i<len(values): if variables[i]=='gen': if values[i]=='F': values[i]=1 else: values[i]=0 print >> f,values[i],'\t', i+=1 print >> f
def query_for_value(endpoint, query, prefixes = ''): row = sparql.query(endpoint, prefixes + query).fetchone() try: (a, ) = row return a[0].value except ValueError: # means we got no results return None
def objinfo(): subject = sparql.IRI(flask.request.args['s']) query = flask.render_template('objinfo.sparql', **{'subject': subject}) return flask.render_template('objinfo.html', **{ 'subject': subject, 'rows': sparql.query(ENDPOINT, query), })
def resource_extractor(entity): db_resource = dict() wiki_resource = dict() resource_ids = dict() result = [] query = 'PREFIX dbo: <http://dbpedia.org/ontology/> SELECT distinct ?uri ?label WHERE { ?uri rdfs:label ?label . \ FILTER langMatches( lang(?label), "EN" ) . ?label bif:contains "' + entity + '" . }' print query try: result = sparql.query(sparql_dbpedia, query) except: pass if result: resources = [sparql.unpack_row(row_result) for row_result in result] for resource in resources: if 'wikidata' in resource[0]: if resource[1] not in wiki_resource.keys(): wiki_resource[resource[1]] = [resource[0]] else: if resource[0] not in sum(wiki_resource.values(), []): wiki_resource[resource[1]].append(resource[0]) else: if resource[1] not in db_resource.keys( ) and 'Category' not in resource[0]: db_resource[resource[1]] = [resource[0]] else: if resource[0] not in sum(db_resource.values(), []): db_resource.get(resource[1], []).append(resource[0]) resource_ids['dbpedia_id'] = db_resource.get(entity)[0].split('/')[-1] resource_ids['wikidata_id'] = wiki_resource.get(entity)[0].split( '/')[-1] return resource_ids
def distance_one_query(id1, distance_one): print "Distance One Query" sparql_endpoint = sparql_dbpedia query = ( prefixes_dbpedia + ' SELECT distinct ?p ?id2 WHERE { <http://dbpedia.org/resource/' + id1 + '> ?p ?id2\ . ' + suffixes_dbpedia_0 + ' FILTER (!regex(str(?pl), "Wikipage","i")) . FILTER (!regex(str(?pl), \ "abstract","i")) . }') print query result = sparql.query(sparql_endpoint, query) q1_values = [sparql.unpack_row(row_result) for row_result in result] print len(q1_values) if q1_values: print len(q1_values) for vals in q1_values: vals_0 = vals[0].split('/')[-1] if vals_0 not in unwanted_predicates: if isinstance(vals[1], basestring): if '/' in vals[1]: distance_one.append( [id1, vals_0, vals[1].split('/')[-1]]) else: distance_one.append([id1, vals_0, vals[1]]) return distance_one
def delete(self, in_triples, in_RDFGraph): print '\n' + '*'*40 print 'VIRTUOSO CONNECTOR delete' print '*'*40 for triple in in_triples: ''' query = 'PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> ' query += 'PREFIX foaf: <http://xmlns.com/foaf/0.1/> ' ''' query = 'DELETE FROM GRAPH <' + in_RDFGraph + '> ' query += '{ ' if isinstance(triple[2], basestring) and "http" in triple[2] and not (".jpg" in triple[2]): query += '<' + triple[0] + '> <' + triple[1] + '> <' + triple[2] + '> . ' elif isinstance(triple[2], basestring): query += '<' + triple[0] + '> <' + triple[1] + '> "' + triple[2].replace('"','').replace("'","") + '" . ' else: query += '<' + triple[0] + '> <' + triple[1] + '> ' + str(triple[2]) + '' query += '}' # launch try: result = sparql.query(self.VIRTUOSO_SPARQL_ENDPOINT, query) print query except sparql.SparqlException as e: print 'Exception: ' print e.message print 'Query: ' print query return -1 return 0
def kgminer_training_data(poi, q_part): q_ts = 'PREFIX dbo: <http://dbpedia.org/ontology/> select distinct ?url1 ?url2 where { \ { ?url2 <http://dbpedia.org/' + poi + '> ?url1 } . ' + q_part + \ ' FILTER(?url1 != ?url2).} ' print q_ts result = sparql.query(sparql_dbpedia, q_ts) training_set = [sparql.unpack_row(row_result) for row_result in result] if not training_set: q_ts = 'PREFIX dbo: <http://dbpedia.org/ontology/> select distinct ?url1 ?url2 where { \ {?url1 <http://dbpedia.org/' + poi + '> ?url2} . ' + q_part + \ ' FILTER(?url1 != ?url2).} ' result = sparql.query(sparql_dbpedia, q_ts) training_set = [sparql.unpack_row(row_result) for row_result in result] print q_ts return training_set
def listObservations(endpoint, graph, countries, indicators, datasets, years): allObservations = [] q = Template('SELECT ?value ?indicatorLabel ?idcountry ?year \n FROM <$graph> WHERE{ ?obs rdf:type qb:Observation.\n\ ?obs wi-onto:ref-indicator ?indicator. \n\ FILTER(?indicator = wi-indicator:$indicator). \n\ ?indicator rdfs:label ?indicatorLabel. \n\ ?obs qb:dataSet ?dataset. \n\ FILTER(?dataset = wi-dataset:$indicator-$dataset). \n\ ?obs wi-onto:ref-area ?country. \n\ FILTER(?country = wi-country:$country). \n\ ?country wi-onto:has-iso-alpha3-code ?idcountry. \n\ ?obs wi-onto:ref-year ?year. \n\ FILTER(?year = $year). \n\ ?obs sdmx-concept:obsStatus ?status. \n\ ?obs wi-onto:value ?value. \n}') for country in countries: for indicator in indicators: for dataset in datasets: for year in years: query = q.substitute(graph=graph, indicator=indicator[0], dataset=dataset, year=year, country=country[1]) print "Selecting ", indicator[0], year, country[1] result = sparql.query(endpoint, query) allObservations.append(rowToList(result)) return allObservations
def get_resource_wikidata_sameas(resource: str): q = "SELECT ?x WHERE { <http://dbpedia.org/resource/" + resource + "> <http://www.w3.org/2002/07/owl#sameAs> ?x. FILTER regex(str(?x), \"wikidata\")}" # print(resource) result = sparql.query(DBPEDIA_ENDPOINT, q) if result.hasresult(): return sparql.unpack_row(result[0])[0] else: return None
def raw_query_and_get_result(*args, **kwargs): """ Returns unparsed query results for xml, xmlschema, json formats """ timeout = kwargs.get("timeout", 0) accept = kwargs.get("accept", "application/sparql-results+xml") result = sparql.query(*args, timeout=timeout, accept=accept, raw=True) return result
def dbpedia_wikidata_equivalent(dbpedia_url): query = 'PREFIX owl:<http://www.w3.org/2002/07/owl#> PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#> SELECT \ ?WikidataProp WHERE { <' + dbpedia_url + '> owl:sameAs ?WikidataProp . FILTER (CONTAINS \ (str(?WikidataProp) , "wikidata.org")) .} ' result = sparql.query(sparql_dbpedia, query) resources = [sparql.unpack_row(row_result) for row_result in result] return resources
def dbpedia_things(query): things = [] result = sparql.query('http://dbpedia.org/sparql', query) for row in result.fetchall(): values = sparql.unpack_row(row) name = values[1] things.append(name) return things
def queryCurrentVoid(self): result = sparql.query(self.endpoint, self._listCurrentVoid) for row in result.fetchall(): print "\t".join(map(str,row)) ckan_name = "void_%s" %str(row[1]) ckan_name = reduce_to_length(ckan_name, 100) self.createRdfRecord(str(row[0]),str(row[1]), ckan_name) self.createAddReplaceLine(str(row[0]),str(row[1]), ckan_name)
def objinfo(): subject = sparql.IRI(flask.request.args['s']) query = flask.render_template('objinfo.sparql', **{'subject': subject}) return flask.render_template( 'objinfo.html', **{ 'subject': subject, 'rows': sparql.query(ENDPOINT, query), })
def do_query(query): rows = error = None try: rows = sparql.query(ENDPOINT, query) except urllib2.HTTPError, e: if 400 <= e.code < 600: error = e.fp.read() else: raise
def queryOldDS(self): """ Find datasets and create metadata records for them """ result = sparql.query(self.endpoint, self._listOldDS) for row in result.fetchall(): print "\t".join(map(str,row)) ckan_name = "%s_%s" %(str(row[0]).split("/")[-2], str(row[1])) ckan_name = reduce_to_length(ckan_name, 100) self.createDSRecord(str(row[0]),str(row[1]), ckan_name, old=True) self.createAddReplaceLine(str(row[0]),str(row[1]), ckan_name)
def sparql_example(): q = ('SELECT DISTINCT ?station, ?orbits WHERE { ' '?station a <http://dbpedia.org/ontology/SpaceStation> . ' '?station <http://dbpedia.org/property/orbits> ?orbits . ' 'FILTER(?orbits > 50000) } ORDER BY DESC(?orbits)') result = sparql.query('http://dbpedia.org/sparql', q) for row in result: print 'row:', row values = sparql.unpack_row(row) print values[0], "-", values[1], "orbits"
def get_all_entity(predicate): # q_ts = 'PREFIX dbo: <http://dbpedia.org/ontology/> select distinct ?url1 ?url2 where { ?url1 rdf:type dbo:Person .\ # ?url1 dbo:' + predicate + ' ?url2 .} ' q_ts = 'PREFIX dbo: <http://dbpedia.org/ontology/> select distinct ?url1 where { ?url1 rdf:type dbo:Person .} ' print q_ts result = sparql.query(sparql_dbpedia, q_ts, timeout=30000) training_set = [sparql.unpack_row(row_result) for row_result in result] print len(training_set) print training_set[:2] return training_set sys.exit(0)
def query_and_get_result(*args, **kwargs): """ Helper function that calls `sparql.query` with the given arguments and returns its results as an easy-to-cache dictionary. """ result = sparql.query(*args, timeout = kwargs.get("timeout", 0)) return { 'var_names': [unicode(name) for name in result.variables], 'rows': result.fetchall(), 'has_result': result._hasResult, }
def query_and_get_result(*args, **kwargs): """ Helper function that calls `sparql.query` with the given arguments and returns its results as an easy-to-cache dictionary. """ result = sparql.query(*args, timeout=kwargs.get("timeout", 0)) return { 'var_names': [unicode(name) for name in result.variables], 'rows': result.fetchall(), 'has_result': result._hasResult, }
def positive_relations(predicate): query = 'select distinct ?a where {?a <http://dbpedia.org/property/' + predicate + '> ?b. ?b rdf:type \ <http://dbpedia.org/ontology/Person> . ?a rdf:type <http://dbpedia.org/ontology/Company> .}' sparql_endpoint = sparql_dbpedia print query try: result = sparql.query(sparql_endpoint, query) q1_values = [sparql.unpack_row(row_result) for row_result in result] except: q1_values = [] return q1_values
def main(): try: result = query(ENDPOINT, Q) message = create_message(result) if message: trigger_content_rule(message) except Exception as e: error_text = "; ".join((repr(e), e.code)) trigger_content_rule(error_text)
def read_enipedia(): import pandas as pd import sparql import datetime res = sparql.query( 'http://enipedia.tudelft.nl/wiki/Special:SparqlExtension', """ BASE <http://enipedia.tudelft.nl/wiki/> PREFIX a: <http://enipedia.tudelft.nl/wiki/> PREFIX prop: <http://enipedia.tudelft.nl/wiki/Property:> PREFIX cat: <http://enipedia.tudelft.nl/wiki/Category:> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> select ?plant_name ?fuel_used ?country ?elec_capacity_MW ?longitude ?latitude ?year_built ?status where { ?plant rdf:type cat:Powerplant . ?plant rdfs:label ?plant_name . ?plant prop:Latitude ?latitude . ?plant prop:Longitude ?longitude . ?plant prop:Primary_fuel_type ?fuel_type . ?fuel_type rdfs:label ?fuel_used . ?plant prop:Annual_Energyoutput_MWh ?OutputMWh . OPTIONAL{?plant prop:Generation_capacity_electrical_MW ?elec_capacity_MW }. OPTIONAL{?plant prop:Country ?country_link . ?country_link rdfs:label ?country }. OPTIONAL{?plant prop:Year_built ?year_built }. OPTIONAL{?plant prop:Status ?status }. } """) def literal_to_python(l): if isinstance(l, tuple): return list(map(literal_to_python, l)) elif l is None: return None elif l.datatype is None: return l.value else: parse_datetime = lambda x: datetime.datetime.strptime( x, "%Y-%m-%dT%H:%M:%SZ") return \ {'decimal': float, 'double': float, 'integer': int, 'dateTime': parse_datetime, 'gYearMonth': parse_datetime} \ [l.datatype[len('http://www.w3.org/2001/XMLSchema#'):]](l.value) df = pd.DataFrame(list(map(literal_to_python, res.fetchone())), columns=[ "Name", "Type", "Country", "Capacity", "lon", "lat", "Built", "Status" ]) return df
def dbpedia_wikidata_mapping(): resource_dict = dict() query = "PREFIX owl:<http://www.w3.org/2002/07/owl#> PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#> SELECT \ ?itemLabel ?WikidataProp WHERE { ?DBpediaProp owl:equivalentProperty ?WikidataProp . FILTER ( \ CONTAINS ( str(?WikidataProp) , 'wikidata')) . ?DBpediaProp rdfs:label ?itemLabel . FILTER (lang(?itemLabel) \ = 'en') .} ORDER BY ?DBpediaProp" result = sparql.query(sparql_dbpedia, query) resources = [sparql.unpack_row(row_result) for row_result in result] for resource in resources: resource_dict[resource[1].split('/')[-1]] = resource[0] with open('LPmln/predicate_dict.json', 'w') as fp: json.dump(resource_dict, fp, default=json_serial)
def get_all_person(): q_ts = 'PREFIX foaf: <http://xmlns.com/foaf/0.1/> PREFIX dbo: <http://dbpedia.org/ontology/> \ PREFIX dbp: <http://dbpedia.org/property/> select ?url1 ?url2 ?url3 ?url4 ?url5 ?url6 ?url7 ?url8 ?url9 ?url10 ?url11\ ?url12 ?url13 ?url14 where { ?url1 dbo:birthPlace ?url6 . ?url1 foaf:gender ?url2 . optional { ?url1 \ dbp:nationality ?url3} . optional { ?url1 dbo:deathPlace ?url5 }. \ optional { ?url1 dbo:profession ?url4 }. optional { ?url1 dbo:residence ?url7} . optional { ?url1 dbo:almaMater \ ?url8 }. optional { ?url1 dbo:deathCause ?url9 }. optional { ?url1 dbo:religion ?url10 } . optional { ?url1 dbo:parent ?url11} \ . optional { ?url1 dbo:child ?url12} . optional { ?url1 dbo:ethnicity ?url13} . optional { ?url1 dbo:spouse ?url14} . }' print q_ts result = sparql.query(sparql_dbpedia, q_ts) training_set = [sparql.unpack_row(row_result) for row_result in result] return training_set
def test_simple_query(self): from sparql import IRI URI_LANG = 'http://rdfdata.eionet.europa.eu/eea/languages' URI_TYPE = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type' URI_LANG_TYPE = 'http://rdfdata.eionet.europa.eu/eea/ontology/Language' endpoint = "http://cr3.eionet.europa.eu/sparql" result = sparql.query(endpoint, "SELECT * WHERE {?s ?p ?o} LIMIT 2") self.assertEqual(result.variables, ['s', 'p', 'o']) self.assertEqual(list(result), [ (IRI(URI_LANG+'/en'), IRI(URI_TYPE), IRI(URI_LANG_TYPE)), (IRI(URI_LANG+'/da'), IRI(URI_TYPE), IRI(URI_LANG_TYPE)), ])
def test_simple_query(self): from sparql import IRI URI_LANG = 'http://rdfdata.eionet.europa.eu/eea/languages' URI_TYPE = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type' URI_LANG_TYPE = 'http://rdfdata.eionet.europa.eu/eea/ontology/Language' endpoint = "http://cr3.eionet.europa.eu/sparql" result = sparql.query(endpoint, "SELECT * WHERE {?s ?p ?o} LIMIT 2") self.assertEqual(result.variables, ['s', 'p', 'o']) self.assertEqual(list(result), [ (IRI(URI_LANG + '/en'), IRI(URI_TYPE), IRI(URI_LANG_TYPE)), (IRI(URI_LANG + '/da'), IRI(URI_TYPE), IRI(URI_LANG_TYPE)), ])
def insert(self, in_triples, in_RDFGraph): print '\n' + '*' * 40 print 'VIRTUOSO CONNECTOR insert' print '*' * 40 for triple in in_triples: ''' query = 'PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> ' query += 'PREFIX foaf: <http://xmlns.com/foaf/0.1/> ' ''' query = 'INSERT IN GRAPH <' + in_RDFGraph + '> ' query += '{ ' ''' if isinstance(triple[2], (int, long, float, complex)): query += '<' + triple[0] + '> <' + triple[1] + '> "' + triple[2] + '"' elif "http" in triple[2]: query += '<' + triple[0] + '> <' + triple[1] + '> <' + triple[2] + '> . ' else: query += '<' + triple[0] + '> <' + triple[1] + '> "' + triple[2].replace('"','').replace("'","") + '" . ' ''' if isinstance( triple[2], basestring) and "http" in triple[2] and not (".jpg" in triple[2]): query += '<' + triple[0] + '> <' + triple[1] + '> <' + triple[ 2] + '> . ' elif isinstance(triple[2], basestring): query += '<' + triple[0] + '> <' + triple[1] + '> "' + triple[ 2].replace('"', '').replace("'", "") + '" . ' else: query += '<' + triple[0] + '> <' + triple[1] + '> ' + str( triple[2]) + '' query += '}' # launch try: result = sparql.query(self.VIRTUOSO_SPARQL_ENDPOINT, query) print query except sparql.SparqlException as e: print 'Exception: ' print e.message print 'Query: ' print query return -1 return 0
def query(): query = flask.request.args.get('query') rows = None error = None if query: try: rows = sparql.query(ENDPOINT, query) except urllib2.HTTPError, e: if 400 <= e.code < 600: error = e.fp.read() else: raise
def getLabel (uri): labelQuery = "SELECT ?label WHERE { <"+uri+"> <http://www.w3.org/2000/01/rdf-schema#label> ?label} limit 1" result = sparql.query(endpoint, labelQuery) for row in result: return sparql.unpack_row(row)[0] label = str(urlparse(uri).fragment) if not label: label = uri.rsplit('/', 1)[-1] if not label: return uri return label
def negative_relations(predicate): query = "SELECT DISTINCT ?subject <notfounders> ?object WHERE { ?object <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> \ <http://dbpedia.org/ontology/Person>. ?subject <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> \ <http://dbpedia.org/ontology/Company>. {{?subject ?targetRelation ?realObject.} UNION \ {?realSubject ?targetRelation ?object.}} ?subject ?otherRelation ?object. \ FILTER (?targetRelation = <http://dbpedia.org/property/" + predicate + ">) \ FILTER (?otherRelation != <http://dbpedia.org/property/" + predicate + ">) \ FILTER NOT EXISTS {?subject <http://dbpedia.org/property/" + predicate + "> ?object.} }" sparql_endpoint = sparql_dbpedia print query try: result = sparql.query(sparql_endpoint, query) q1_values = [sparql.unpack_row(row_result) for row_result in result] except: q1_values = [] return q1_values
def sparql_query(query, endpoint): """ Runs the given (string) query against the given endpoint, returns a list of dicts with the variables' values. """ result = sparql.query(endpoint, query) data = [] for row in result: values = sparql.unpack_row(row) d = {} for i, v in enumerate(values): d[result.variables[i]] = v data.append(d) return data
def getResource(self, uri): q = (""" PREFIX foaf: <http://xmlns.com/foaf/0.1/> PREFIX schema: <http://schema.org/> SELECT DISTINCT ?p ?o WHERE { <""" + uri + """> ?p ?o . FILTER (regex(?o, "http") || lang(?o) = 'en' || datatype(?o) != xsd:string ) } """) print q result = sparql.query(DBPEDIA_ENDPOINT, q) return result
def getTSS (self, endpoint, variantStart, variantEnd, variantChromosome): file = open("fantom5TSSForGenomicLocation", "r") query = file.read() query = query.replace("?variantStart", str(variantStart)) query = query.replace("?variantEnd", str(variantEnd)) query = query.replace("?variantChromosome", str("hg19:"+variantChromosome)) #print query result = sparql.query(endpoint, query) #for row in result: #print 'row:', row # values = sparql.unpack_row(row) # print values[1], "-", values[2], "-", values[3], "-", values[4] return result
def query(self, query): # launchVIRTUOSO_doDataLakeTransformations(): try: result = sparql.query(self.VIRTUOSO_SPARQL_ENDPOINT, query) print query print "query succeded, result: " + result.variables[0] triples = [] for row in result: values = sparql.unpack_row(row) triples.append([values[0], values[1], values[2]]) return triples except sparql.SparqlException as e: print 'Exception: ' print e.message print 'Query: ' print query pass
def count_result_from_query(subset): value = 0; endpoint = 'http://130.235.17.116:8000/openrdf-sesame/repositories/AAOT' statement_beginning = """PREFIX aaot:<http://cs.lth.se/ontologies/aaot.owl#> select (count(*) as ?count) WHERE { \n""" statement_end = """ ?name aaot:survival_time ?survival_time . }""" statement_query = "" for i in subset: statement_query = statement_query + "?name aaot:"+i+" ?"+i+" .\n" statement = statement_beginning + statement_query + statement_end result = sparql.query(endpoint, statement) for row in result.fetchall(): values = sparql.unpack_row(row) value = values[0] return value
def add(self, in_RDFGraph, in_RDFGraph2): print '\n' + '*'*40 print 'VIRTUOSO CONNECTOR add' print '*'*40 query = 'ADD <' + in_RDFGraph + '> TO <' + in_RDFGraph2 + '>' # launch try: result = sparql.query(self.VIRTUOSO_SPARQL_ENDPOINT, query) print query except sparql.SparqlException as e: print 'Exception: ' print e.message print 'Query: ' print query return -1 return 0
def getURIs(self, name): q = (""" PREFIX foaf: <http://xmlns.com/foaf/0.1/> PREFIX schema: <http://schema.org/> SELECT DISTINCT ?uri ?name WHERE { ?uri a schema:Movie . ?uri foaf:name ?name . FILTER contains(?name, """ + '"' + name + '"' + """ ) } """) print q try: result = sparql.query(DBPEDIA_ENDPOINT, q) return result except sparql.SparqlException as e: print 'Exception: ' print e.message print 'Query: ' print q pass
def print_result(attributes,offset,f): endpoint='http://130.235.17.116:8000/openrdf-sesame/repositories/AAOT' statement_beginning="""PREFIX aaot:<http://cs.lth.se/ontologies/aaot.owl#> select * WHERE { """ statement_ending=""" ?name aaot:survival_time ?survival_time . } limit 1000 offset""" statement_query = "" for i in attributes: statement_query = statement_query + "?name aaot:"+i+" ?"+i+" ." statement = (statement_beginning + statement_query + statement_ending + str(offset)) result = sparql.query(endpoint,statement) variables = result.variables for row in result.fetchall(): values=sparql.unpack_row(row) i=1 while i<len(values): print >> f,values[i],'\t', i+=1 print >> f
def insert(self, in_triples, in_RDFGraph): print '\n' + '*'*40 print 'VIRTUOSO CONNECTOR insert' print '*'*40 for triple in in_triples: ''' query = 'PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> ' query += 'PREFIX foaf: <http://xmlns.com/foaf/0.1/> ' ''' query = 'INSERT IN GRAPH <' + in_RDFGraph + '> ' query += '{ ' if isinstance(triple[2], (int, long, float, complex)): query += '<' + triple[0] + '> <' + triple[1] + '> ' + str(triple[2]) + ' . ' elif "http" in triple[2]: query += '<' + triple[0] + '> <' + triple[1] + '> <' + triple[2] + '> . ' else: query += '<' + triple[0] + '> <' + triple[1] + '> "' + triple[2].replace('"','') + '" . ' query += '}' # launch try: result = sparql.query(self.VIRTUOSO_SPARQL_ENDPOINT, query) print query except sparql.SparqlException as e: print 'Exception: ' print e.message print 'Query: ' print query return -1 return 0
def __call__(self, **kwargs): missing, arg_values = bits.map_arg_values(self.arg_map, kwargs) assert not missing query_string = bits.interpolate_query(self.query_template, arg_values) return SparqlResult(sparql.query(self.endpoint, query_string))
def query_for_rows(endpoint, query, prefixes = ''): return [[value(val) for val in row] for row in sparql.query(endpoint, prefixes + query)]
def query_for_list(endpoint, query, prefixes = ''): return [row[0].value for row in sparql.query(endpoint, prefixes + query)]
def multiple_query(): endpoint = 'http://130.235.17.116:8000/openrdf-sesame/repositories/AAOT' f_properties = open('listProperties.txt','r') statement_beginning = """PREFIX aaot:<http://cs.lth.se/ontologies/aaot.owl#> select (count(*) as ?count) WHERE { \n""" statement_end = """ ?name aaot:survival_time ?survival_time . }""" offset = 0; max_list_attributes = [] treshold = 10000 line=f_properties.readlines() for i in range(0, len(line)): list_attributes = [] list_attributes.append(line[i].strip()) statement_query = "" statement_query = statement_query + "?name aaot:"+line[i].strip()+" ?"+line[i].strip()+" ." statement = statement_beginning + statement_query + statement_end +"\n" result = sparql.query(endpoint,statement) for row in result.fetchall(): values = sparql.unpack_row(row) if (values[0] > 50000): print values[0], print list_attributes ''' if ((values[0] > treshold) and (len(list_attributes) > len(max_list_attributes))): max_list_attributes = list_attributes print str(values[0]), print max_list_attributes ''' for j in range(i+1, len(line)): latest_value = 0 list_attributes.append(line[j].strip()) statement_query = statement_query + "?name aaot:"+line[j].strip()+" ?"+line[j].strip()+" ." statement = statement_beginning + statement_query + statement_end+ "\n" result = sparql.query(endpoint,statement) for row in result.fetchall(): values = sparql.unpack_row(row) latest_value = values[0] if (latest_value >= 50000): print values[0], print list_attributes else: break if (latest_value < 50000): break ''' latest_value = values[0] print values[0], print len(list_attributes), print len(max_list_attributes) if ((values[0] > treshold) and (len(list_attributes) > len(max_list_attributes))): print "condition checked" max_list_attributes = list_attributes print str(values[0]), print list_attributes if(latest_value == 0): break ''' return max_list_attributes
def query(query_str): result = sparql.query(localhost_endpoint, query_str) row_tuple = namedtuple('row', ' '.join(result.variables)) return [row_tuple(*sparql.unpack_row(row)) for row in result]
def queryObsoleteDS(self): result = sparql.query("http://semantic.eea.europa.eu/sparql", self._listObsoleteDS) for row in result.fetchall(): print "\t".join(map(str,row)) self.createRemoveLine(str(row[0]),str(row[1]))
def query(self, q): print q result = sparql.query(DBPEDIA_ENDPOINT, q) return result