def test_graph(): filename = "../../../../../../output/domain-lookup/yagoProcessed/book_strategy1.ttl" uri = "Single-entry_bookkeeping_system" # Graph.query() can be used now rdfextras.registerplugins() graph = g.Graph() graph.parse(filename, format="turtle") results = graph.query(""" BASE <http://yago-knowledge.org/resource/> PREFIX dbp: <http://dbpedia.org/ontology/> PREFIX owl: <http://www.w3.org/2002/07/owl#> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX skos: <http://www.w3.org/2004/02/skos/core#> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> SELECT ?p ?o WHERE { <%s> ?p ?o. } ORDER BY (?p) """ % uri) for result in results: print(result)
def main(): print "[%s]: generate ontology hierarchy tree" % (time_utils._timestamp()) G = g.Graph() G.parse(config.ONTOLOGY, format="n3") q = ''' PREFIX rr: <http://www.w3.org/2000/01/rdf-schema#> SELECT ?child ?parent WHERE { ?child rr:subClassOf ?parent . }''' results = G.query(q) ontologyDict = {} for row in results: child = str(row[0]) parent = str(row[1]) if parent in ontologyDict: ontologyDict[parent].append(child) else: ontologyDict[parent] = [ child, ] pkl_utils._save(config.ONTOLOGY_TREE, ontologyDict) print "[%s]: generation complete" % time_utils._timestamp()
def info_monument(monument_id, search): result = g.Graph() result.parse("dataset.ttl", format="ttl") query_text = """ ask where { ?m a cis:CulturalInstituteOrSite ; dc:identifier """ + monument_id + """ ; """ + search + """ ?x . } """ query = result.query(query_text) for r in query: if r: query_text = """ select distinct ?x where { ?m a cis:CulturalInstituteOrSite ; dc:identifier """ + monument_id + """ ; """ + search + """ ?x . } """ query = result.query(query_text) result_query = {} result_query[monument_id] = list() for row in query: if row[0] == "": return -1 a = row[0] result_query[monument_id].append(a) return result_query else: return -1
def __read_file(self): """Reads the given file. Uses Graph class from rdflib library to read the file. A SPARQL query is used to get all the data from the file. Returns the results of the query after it is executed. """ g = graph.Graph() extension = self.file_name.split(".") extension = extension[-1] g.parse(self.file_name, format=extension) query = """ PREFIX owl: <http://www.w3.org/2002/07/owl#> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> SELECT ?sub ?pred ?obj WHERE { ?sub ?pred ?obj. } """ qres = g.query(query) return qres
def test_can_reverse(mocker): def mock_iter(a): yield (1, 3) yield (2, 4) g = graph.Graph() mocker.patch.object(g, 'subject_objects', mock_iter) res = t.extract_relation_by_uri(g, SKOS.broader, True) assert list(res) == [(3, 1), (4, 2)]
def getrdffromjson(filename): g = graph.Graph() f1 = open(filename, "r") s = f1.read() f1.close() js = json.loads(s) #loading the content of file into json getrdffromjsontriple(js, g, []) return g
def id_monument(monument_name): result = g.Graph() result.parse("dataset.ttl", format="ttl") result_query = {} result_query[monument_name] = list() query_text = """ select distinct ?id where { """ + monument_name + """ a cis:CulturalInstituteOrSite ; dc:identifier ?id } """ query = result.query(query_text) for row in query: result_query[monument_name].append(row[0]) return result_query
def getExistingMapping(lang="en"): print "[%s]: parse existing mapping for language %s" % ( time_utils._timestamp(), lang) G = g.Graph() G.parse(config.EXISTING_MAPPING[lang], format="n3") q = ''' PREFIX rr: <http://www.w3.org/ns/r2rml#> SELECT ?template ?class WHERE { ?template rr:subjectMap ?mapping . ?mapping rr:class ?class . } ''' results = G.query(q) mapping = [row[0] for row in results] ontology = [row[1] for row in results] df = pd.DataFrame({'mapping': mapping, 'ontology': ontology}) df["template"] = df["mapping"].apply( lambda x: config.TEMPLATE_NAME[lang] + x[47:]) df.to_csv(config.EXISTING_MAPPING_OUTPUT[lang], index=False) print "[%s]: parsing complete" % time_utils._timestamp()
import rdflib.graph as g from rake_nltk import Rake from textblob import TextBlob graph = g.Graph() graph.parse('./92771.ttl', format='ttl') #print(graph.serialize(format='pretty-xml')) wantsTalk = True; qres = graph.query( """SELECT ?person WHERE { ?person dbo:ReligiousBuilding :nave . }""") # for row in qres: # print(row[0])
import rdflib.graph as g from rdflib import URIRef, BNode, Literal from rdflib import Namespace from SPARQLWrapper import SPARQLWrapper, JSON if __name__ == "__main__": endpoint = SPARQLWrapper("http://dbpedia.org/sparql") endpoint.setReturnFormat(JSON) print "Graph Construction Starts!" G = g.Graph() G.parse("../cmput690_a3.ttl", format="n3") # Add relation: ?player fbk:nation ?nation infile = open("../output/nation.tsv") for line in infile.readlines(): player, nation = line.strip().split('\t') sub = URIRef("http://rdf.freebase.com/ns/" + player) pred = URIRef("http://rdf.freebase.com/key/nation") obj = Literal(nation) G.add((sub, pred, obj)) infile.close() # Add relation: ?team fbk:player_list ?player infile = open("../output/q2.tsv") for line in infile.readlines(): player, team = line.strip().split("\t") sub = URIRef(team) pred = URIRef("http://rdf.freebase.com/key/player_list") obj = URIRef(player) G.add((sub, pred, obj))
def test_extract_broader(mocker): g = graph.Graph() spy = mocker.spy(g, "subject_objects") t.extract_relation_by_uri(g, SKOS.broader, False) spy.assert_called_once_with(SKOS.broader)
def get_graph(self): graph = rg.Graph() for statement in self.statements: graph.add((self.subject, statement[0], statement[1])) return graph
import spotlight from rdflib import graph, Literal, Namespace, URIRef from rdflib.serializer import Serializer from rdflib.namespace import RDF, RDFS, XSD import requests import csv import re import hashlib import docker import time client = docker.from_env() container_it = client.containers.get("c9e896401036") container_en = client.containers.get("233d97c53c0a") g = graph.Graph() cro = Namespace("http://www.cityrank.org/ontology/") geo = Namespace("https://schema.org/GeoCoordinates") plc = Namespace("https://schema.org/Place") schema = Namespace("https://schema.org") owl = Namespace("http://www.w3.org/2002/07/owl") addr = Namespace("https://schema.org/PostalAddress") rdfs = Namespace("http://www.w3.org/2000/01/rdf-schema#") base = "http://www.cityrank.org/resource/" dbpediaurl = "http://dbpedia.org/resource/" not_found = {} g.bind("cro", cro) g.bind("geo", geo) g.bind("schema", schema) g.bind("addr", addr)