def get_remote_label(uri): for db in [self.db, self.admin_db]: g = Graph() try: db.nsBindings = {} g += db.query('''select ?s ?p ?o where { hint:Query hint:optimizer "Runtime" . ?s ?p ?o.}''', initNs=self.NS.prefixes, initBindings=dict(s=uri)) db.nsBindings = {} except: pass resource_entity = g.resource(uri) if len(resource_entity.graph) == 0: #print "skipping", db continue for property in label_properties: labels = self.lang_filter(resource_entity[property]) if len(labels) > 0: return labels[0] if len(labels) == 0: name = [x.value for x in [resource_entity.value(self.NS.foaf.givenName), resource_entity.value(self.NS.foaf.familyName)] if x is not None] if len(name) > 0: label = ' '.join(name) return label try: label = self.db.qname(uri).split(":")[1].replace("_"," ") return ' '.join(camel_case_split(label)).title() except Exception as e: print(str(e), uri) return str(uri)
def _top_classes(self): import rdflib.plugin from rdflib.store import Store from rdflib.parser import Parser from rdflib.serializer import Serializer from rdflib.query import ResultParser, ResultSerializer, Processor, Result, UpdateProcessor from rdflib.exceptions import Error rdflib.plugin.register('sparql', Result, 'rdflib.plugins.sparql.processor', 'SPARQLResult') rdflib.plugin.register('sparql', Processor, 'rdflib.plugins.sparql.processor', 'SPARQLProcessor') rdflib.plugin.register('sparql', UpdateProcessor, 'rdflib.plugins.sparql.processor', 'SPARQLUpdateProcessor') retr_query = ''' construct { ?subClass rdfs:subClassOf ?id; a ?type; rdfs:label ?sclabel. ?id rdfs:label ?label; a ?idtype. } where { { ?subClass rdfs:subClassOf ?id. } optional { ?subClass a ?type. } optional { ?subClass rdfs:label ?sclabel. } optional { ?id rdfs:label ?label } optional { ?id a ?idtype. } }''' g = self.db.query(retr_query).graph #print list(g[::self.NS.sio.object]) query = ''' select distinct ?id where { { ?subClass rdfs:subClassOf ?id. } ?id rdfs:label ?label. optional { ?id rdfs:subClassOf ?superClass. } FILTER (!bound(?superClass)) FILTER (isURI(?id)) } order by ?label''' return [g.resource(i[0]) for i in g.query(query)]