def main(argv): idir = '' ofile = '' default_ns = 'http://www.example.org/' sformat = 'turtle' gen_ontology = False try: opts, args = getopt.getopt(argv, "d:f:hi:o:", ["dir=", "of=", "format=", "default_namespace=", \ "generate_ontology"]) except getopt.GetoptError: print( 'mag2rdf -i <input directory> [-d <default namespace> -o <outputfile> -f <serialization format>' + '--generate_ontology]') sys.exit(2) for opt, arg in opts: if opt == '-h': print( str('A tool to translate the Microsoft Academic Graph, to its Semantic Web equivalent.\nUsage:\n\t' + 'mag2rdf.py -i <input directory> [-d <default namespace> -o <outputfile> -f <serialization format>' + '--generate_ontology]')) sys.exit(0) elif opt in ("-i", "--dir"): idir = arg elif opt in ("-o", "--of"): ofile = arg elif opt in ("-d", "--default_namespace"): default_ns = arg elif opt in ("--generate_ontology"): gen_ontology = True elif opt in ("-f", "--format"): sformat = arg if idir == '' and gen_ontology is False: print( 'Missing required input (flags).\nUse \'MAG2RDF.py -h\' for help.') sys.exit(1) if not idir.endswith('/'): idir = idir + '/' if ofile == '': ofile = os.getcwd() + '/' + 'output-{}'.format(str(datetime.now())) dataGraph = None schemaGraph = None if idir != '': dataGraph = translate(idir, default_ns) if gen_ontology: schemaGraph = writeOntologyGraph(default_ns) if dataGraph is not None: writer.write(dataGraph, ofile + extOf(sformat), sformat) if schemaGraph is not None: writer.write(schemaGraph, ofile + '_Ontology' + extOf(sformat), sformat)
def main(argv): idir = '' ofile = '' default_ns = 'http://www.example.org/' sformat = 'turtle' gen_ontology = False try: opts, args = getopt.getopt(argv, "d:f:hi:o:", ["dir=", "of=", "format=", "default_namespace=", \ "generate_ontology"]) except getopt.GetoptError: print('mag2rdf -i <input directory> [-d <default namespace> -o <outputfile> -f <serialization format>' + '--generate_ontology]') sys.exit(2) for opt, arg in opts: if opt == '-h': print(str('A tool to translate the Microsoft Academic Graph, to its Semantic Web equivalent.\nUsage:\n\t' + 'mag2rdf.py -i <input directory> [-d <default namespace> -o <outputfile> -f <serialization format>' + '--generate_ontology]')) sys.exit(0) elif opt in ("-i", "--dir"): idir = arg elif opt in ("-o", "--of"): ofile = arg elif opt in ("-d", "--default_namespace"): default_ns = arg elif opt in ("--generate_ontology"): gen_ontology = True elif opt in ("-f", "--format"): sformat = arg if idir == ''and gen_ontology is False: print('Missing required input (flags).\nUse \'MAG2RDF.py -h\' for help.') sys.exit(1) if not idir.endswith('/'): idir = idir + '/' if ofile == '': ofile = os.getcwd() + '/' + 'output-{}'.format(str(datetime.now())) dataGraph = None schemaGraph = None if idir != '': dataGraph = translate(idir, default_ns) if gen_ontology: schemaGraph = writeOntologyGraph(default_ns) if dataGraph is not None: writer.write(dataGraph, ofile + extOf(sformat), sformat) if schemaGraph is not None: writer.write(schemaGraph, ofile + '_Ontology' + extOf(sformat), sformat)
def main(argv): ifile = '' ofile = '' default_ns = 'http://www.example.org/' sformat = 'turtle' try: opts, args = getopt.getopt( argv, "d:f:hi:o:", ["if=", "of=", "format=", "default_namespace="]) except getopt.GetoptError: print( 'mag2rdf -i <inputfile> [-d <default namespace> -o <outputfile> -f <serialization format>]' ) sys.exit(2) for opt, arg in opts: if opt == '-h': print( str('A tool to translate the Microsoft Academic Graph, to its Semantic Web equivalent.\nUsage:\n\t' + 'mag2rdf.py -i <inputfile> [-d <default namespace> -o <outputfile> -f <serialization format>]' )) sys.exit(0) elif opt in ("-i", "--ifile"): ifile = arg elif opt in ("-o", "--ofile"): ofile = arg elif opt in ("-d", "--default_namespace"): default_ns = arg elif opt in ("-f", "--format"): sformat = arg if ifile == '': print( 'Missing required input (flags).\nUse \'MAG2RDF.py -h\' for help.') sys.exit(1) if ofile == '' and ifile != '': ofile = os.getcwd() + '/' + re.sub(r'^(?:.*/)?(.*)\..*$', r'\1', ifile) + '-{}'.format( str(datetime.now())) else: ofile = os.getcwd() + '/' + 'output-{}'.format(str(datetime.now())) graph = translate(ifile, default_ns) if graph is not None: writer.write(graph, ofile + extOf(sformat), sformat) else: raise Exception
def writeSubGraph(graph, partial=True, sformat='nt'): ofile = graph.identifier.toPython() part = re.sub(r'.*\.([0-9]*$)', r'\1', ofile) if not part.isdigit() and partial: ofile = ofile + '.0' writer.write(graph, ofile + extOf(sformat)) if partial: newID = ofile.rstrip(part) + str(int(part) + 1) if part.isdigit() else ofile # create graph instance graph = rdflib.Graph(identifier=newID) graph.open('./store', create = True) return graph
def main(argv): ifile = '' ofile = '' default_ns = 'http://www.example.org/' sformat = 'turtle' try: opts, args = getopt.getopt(argv, "d:f:hi:o:", ["if=", "of=", "format=", "default_namespace="]) except getopt.GetoptError: print('mag2rdf -i <inputfile> [-d <default namespace> -o <outputfile> -f <serialization format>]') sys.exit(2) for opt, arg in opts: if opt == '-h': print(str('A tool to translate the Microsoft Academic Graph, to its Semantic Web equivalent.\nUsage:\n\t' + 'mag2rdf.py -i <inputdir> [-d <default namespace> -o <outputfile> -f <serialization format>]')) sys.exit(0) elif opt in ("-i", "--ifile"): ifile = arg elif opt in ("-o", "--ofile"): ofile = arg elif opt in ("-d", "--default_namespace"): default_ns = arg elif opt in ("-f", "--format"): sformat = arg if ifile == '': print('Missing required input (flags).\nUse \'MAG2RDF.py -h\' for help.') sys.exit(1) if ofile == '' and ifile != '' : ofile = os.getcwd() + '/' + re.sub(r'^(?:.*/)?(.*)\..*$', r'\1', ifile) + '-{}'.format(str(datetime.now())) else: ofile = os.getcwd() + '/' + 'output-{}'.format(str(datetime.now())) graph = translate(ifile, default_ns) if graph is not None: writer.write(graph, ofile + extOf(sformat), sformat) else: raise Exception