def sesame_is_sparql_test(self): url = 'http://dbpedia.org/sparql' g1 = SesameGraph(url) g2 = SPARQLGraph(url) q1 = "select distinct ?Concept where {[] a ?Concept} LIMIT 10" r1 = set(list(g1.query(q1,resultMethod='xml'))) r2 = set(list(g2.query(q1,resultMethod='xml'))) assert r1==r2
def find_relationships(person1, person2): graph = SPARQLGraph('http://dbpedia.org/sparql') query = ''' PREFIX dcterms: <http://purl.org/dc/terms/> PREFIX skos: <http://www.w3.org/2004/02/skos/core#> SELECT DISTINCT ?p ?o WHERE { <%s> ?p ?o . <%s> ?p ?o . FILTER (?p != <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> && ?p != <http://dbpedia.org/property/wikiPageUsesTemplate> && ?p != <http://dbpedia.org/property/wordnet_type> ) } ''' query = query % (person1, person2) relationships = [] for r in graph.query(query, resultMethod='json'): relationships.append((str(r[0]), str(r[1]))) return relationships
def find_related(self): """Find related Linked Data. Returns: A list of the URIs of the related Linked Data. """ related_concepts = [] if not self._is_dbpedia_uri(): return related_concepts graph = SPARQLGraph('http://dbpedia.org/sparql') # query = ''' # PREFIX dcterms: <http://purl.org/dc/terms/> # PREFIX skos: <http://www.w3.org/2004/02/skos/core#> # SELECT DISTINCT ?c WHERE { # { # <%s> dcterms:subject ?cat . # ?c dcterms:subject ?cat . # } UNION { # <%s> dcterms:subject ?cat . # ?n_cat skos:broader ?cat . # ?c dcterms:subject ?n_cat . # } UNION { # <%s> dcterms:subject ?cat . # ?cat skos:broader ?n_cat . # ?c dcterms:subject ?n_cat . # } # }''' # query = query % (self.uri, self.uri, self.uri) query = ''' PREFIX dcterms: <http://purl.org/dc/terms/> PREFIX skos: <http://www.w3.org/2004/02/skos/core#> SELECT DISTINCT ?c WHERE { { <%s> dcterms:subject ?cat . ?c dcterms:subject ?cat . } }''' query = query % self.uri # i = 0 for r in graph.query(query, resultMethod='json'): # if i > 10: # break if str(r[0]) != self.uri: related_concepts.append(str(r[0])) # i += 1 return related_concepts
def find_persons_share_prop(person, preidicate, value): graph = SPARQLGraph('http://dbpedia.org/sparql') query = ''' PREFIX dcterms: <http://purl.org/dc/terms/> PREFIX skos: <http://www.w3.org/2004/02/skos/core#> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> SELECT DISTINCT ?other_person WHERE { <%s> <%s> %s . ?other_person a <http://dbpedia.org/ontology/Person> ; <%s> %s . } ''' query = query % (person, preidicate, value, preidicate, value) relationships = [] for r in graph.query(query, resultMethod='json'): relationships.append(str(r[0])) return relationships
def find_related_persons(person_uri): graph = SPARQLGraph('http://dbpedia.org/sparql') query = ''' PREFIX dcterms: <http://purl.org/dc/terms/> PREFIX skos: <http://www.w3.org/2004/02/skos/core#> SELECT DISTINCT ?other_person ?o WHERE { <%s> dcterms:subject ?o . ?other_person a <http://dbpedia.org/ontology/Person> ; dcterms:subject ?o . } LIMIT 1000 ''' # query = ''' # PREFIX dcterms: <http://purl.org/dc/terms/> # PREFIX skos: <http://www.w3.org/2004/02/skos/core#> # SELECT DISTINCT ?other_person ?p WHERE { # <%s> ?p ?other_person. # ?other_person a <http://dbpedia.org/ontology/Person> . # } # ''' query = query % person_uri related_concepts = [] for r in graph.query(query, resultMethod='json'): related_concepts.append((str(r[0]), str(r[1]))) return related_concepts
from rdfalchemy.sparql import SPARQLGraph url = 'http://bel-epa.com:8080/joseki/ukpp' g = SPARQLGraph(url) q1 = "select ?s ?p ?o where {?s ?p ?o} LIMIT 10" responses = {} x = set(list(g.query(q1, resultMethod='xml'))) j = set(list(g.query(q1, resultMethod='json'))) print(len(x)) def sizes_test(): assert len(x) == len(j) def eq_jx_test(): assert j == x