def testContructorArguments(self): with self.assertRaises(TypeError): skos.RDFLoader('oops') graph = rdflib.Graph() with self.assertRaises(TypeError): skos.RDFLoader(graph, max_depth='oops') with self.assertRaises(TypeError): skos.RDFLoader(graph, normalise_uri='oops')
def load(self): url = self.options.url filename = self.options.filename if not url and not filename: print "No URL or FILENAME provided and one is required" print self.usage return if not self.options.name: print "No NAME provided and it is required" print self.usage return if not self.options.uri: print "No URI provided and it is required" print self.usage return print "Loading graph" graph = rdflib.Graph() result = graph.parse(url or filename) loader = skos.RDFLoader(graph, max_depth=float('inf'), flat=True, lang=self.options.lang) print "Processing concepts" concepts = loader.getConcepts() top_level = [] for _, v in concepts.iteritems(): if not v.broader: top_level.append(v) top_level.sort(key=lambda x: x.prefLabel) import ckan.model as model import ckan.logic as logic self.context = {'model': model, 'ignore_auth': True} try: current = logic.get_action('taxonomy_show')(self.context, { 'id': self.options.name }) logic.get_action('taxonomy_delete')(self.context, { 'id': self.options.name }) except logic.NotFound: pass tx = logic.get_action('taxonomy_create')( self.context, { 'title': self.options.title or self.options.name, 'name': self.options.name, 'uri': self.options.uri }) for t in top_level: self._add_node(tx, t) print 'Load complete'
def main(): """ Update the local vocabulary database """ parser = argparse.ArgumentParser( description= 'Update the Vocabulary SQLite database from the NERC Vocabulary Server.' ) parser.add_argument( '--vocabularies', type=argparse.FileType('r'), default=stdin, metavar='FILE', help= 'A file containing newline separated list of vocabulary resources to copy (defaults to STDIN)' ) parser.add_argument('file', metavar='FILE', nargs=1, help='The SQLite database file to update') args = parser.parse_args() logging.basicConfig(level=logging.INFO) filename = abspath(args.file[0]) # Annoyingly we have to register a plugin to deal with the # `text/xml` content type representing the RDF returned from the # NERC vocabulary server rdflib.plugin.register('text/xml', rdflib.plugin.Parser, 'rdflib.plugins.parsers.rdfxml', 'RDFXMLParser') try: engine = create_engine('sqlite:///%s' % filename) # get a handle on the local database # load the vocabularies vocabularies = getVocabularies(args.vocabularies) graph = getGraph(vocabularies) loader = skos.RDFLoader(graph, normalise_uri=normalise_uri) # get a session to the local database Session = sessionmaker(bind=engine) session = Session() # re-create the schema and load the vocabularies with session.begin(subtransactions=True): conn = session.connection() skos.Base.metadata.drop_all(conn) skos.Base.metadata.create_all(conn) session.add_all(loader.values()) session.commit() del engine logging.info("Finished") except KeyboardInterrupt: logging.warn("Interrupted!")
def testBuild(self): objects = self.getConcepts() collection = self.getCollection() objects.append(collection) graph = self.builder.build(objects) self.assertEqual(len(graph), 16) # round trip the data loader = skos.RDFLoader(graph) self.assertEqual(len(loader), 3) for obj in objects: self.assertIn(obj.uri, loader) self.assertEqual(loader[obj.uri], obj)
def __init__(self, rdf_file): self.rdf_file = rdf_file self.__g = rdflib.Graph() self.__g.parse(location=rdf_file, format='application/rdf+xml') self.__loader = skos.RDFLoader(self.__g, max_depth=1, flat=True) self.concepts = self.__loader.getConcepts( ) # composition of the 'collections.MutableSet' # setup separate dict (with same keys) with a skeleton to house our calculated metrics self.metrics = { uri: { PREF_LABEL: '', IMPORTANCE_SCORE: 0, NUM_RELATIONS: 0, NUM_EXTERNAL: 0, FREQUENCY: 0 } for uri in self.concepts }
import string import sys import rdflib import skos LCCO_URI = 'http://inkdroid.org/lcco/' LCC = namedtuple('LCC', ['cls', 'subcls', 'topic']) # create RDF graph graph = rdflib.Graph() with open('data/lcco.rdf') as skosfile: graph.parse('data/lcco.rdf') # create SKOS representation loader = skos.RDFLoader(graph) # Takes cls, subcls, topic returns next one up def parse_lcc(lcc): cls = lcc[0] if len(lcc) > 1: subcls = lcc[1] if lcc[1] in string.uppercase else None topic = lcc[2:] if subcls else lcc[1:] else: subcls = None topic = None return LCC(cls, subcls, topic) def get_closest(lcc): # FIRST SELECT CLOSEST
logging.basicConfig(level=logging.INFO) # filename = 'res/pp_project_carproject.rdf' filename = 'res/pp_project_manuterms.rdf' g = rdflib.Graph() g.load(filename) g = rdflib.Graph() g = g.parse(location=filename, format="application/rdf+xml") # TODO, play with these args. normalize_uri=unicode might do some good, but need to do testing ''' must have max_depth set to at least 1 in order to resolve external resources (i.e. exactMatch). going higher than 1 will add another layer of recursive calls per. ''' loaded = skos.RDFLoader(g, max_depth=1, flat=True) # loaded.flat = True # c = loaded['http://infoneer.poolparty.biz/Processes/347'] c = loaded[ 'http://infoneer.poolparty.biz/Casting/364'] # successfully gets a synonym print c.prefLabel print c.synonyms print c.broader print c.related class SkosTool(object): """ TODO doc """
def getLoader(self, graph): return skos.RDFLoader(graph, 0, lang='es')
def getLoader(self, graph): return skos.RDFLoader(graph, 0)
def getLoader(self, graph): def normalise_uri(uri): return uri.rstrip(u'/') return skos.RDFLoader(graph, normalise_uri=normalise_uri)
def getLoader(self, graph): return skos.RDFLoader(graph, 1) # a parsing depth of 1