|
| 1 | +""" |
| 2 | + RDF merger and converter for LUBM dataset. |
| 3 | + Database is stored in files <prefix><id>_<sub_id>.owl |
| 4 | + This files are merged for specified number of universities (ids range), |
| 5 | + and edges are replaced with specified mapping. |
| 6 | + Also vertices labels also replaces with integer based names. |
| 7 | + |
| 8 | + Usage: |
| 9 | + - Create a conversion configuration file. Each line must contain an IRI, |
| 10 | + a whitespace character and a string to replace the IRI by. |
| 11 | + - Run converter.py <prefix> <count> <config> |
| 12 | + - Result will have name <pefix><count><vertices count><indices count>.xml |
| 13 | + |
| 14 | + The graph will contain explicit inverted edges added an 'R'. |
| 15 | + """ |
| 16 | + |
| 17 | +import rdflib, sys, os |
| 18 | + |
| 19 | +URI_PREFIX = 'http://yacc/' |
| 20 | +MAX_FILES_PER_UNI = 30 |
| 21 | + |
| 22 | +# RDF serialization |
| 23 | +def write_to_rdf(target, graph): |
| 24 | + graph.serialize(target + '.xml', format='xml') |
| 25 | + |
| 26 | +# Edge addition (grapf constructing) |
| 27 | +def add_rdf_edge(subj, pred, obj, graph): |
| 28 | + s = rdflib.BNode('id-%s' % (subj)) |
| 29 | + p = rdflib.URIRef(URI_PREFIX + pred) |
| 30 | + o = rdflib.BNode('id-%s' % (obj)) |
| 31 | + graph.add((s, p, o)) |
| 32 | + |
| 33 | +if len(sys.argv) < 3: |
| 34 | + print('Usage: converter.py <prefix> <count> <config>') |
| 35 | + exit() |
| 36 | + |
| 37 | +replace = {} # map for replacing predicates |
| 38 | +config = sys.argv[3] |
| 39 | +for l in open(config,'r').readlines(): |
| 40 | + pair = l.split(' ') |
| 41 | + old = rdflib.URIRef(pair[0].strip(' ')) |
| 42 | + new = pair[1].strip('\n').strip(' ') |
| 43 | + replace[old] = new |
| 44 | + |
| 45 | +print(replace) |
| 46 | + |
| 47 | +res = {} # map from resources to integer ids |
| 48 | +next_id = 0 # id counter |
| 49 | +edges_count = 0 # Total edges |
| 50 | + |
| 51 | +graph = rdflib.Graph() |
| 52 | +prefix = sys.argv[1] |
| 53 | +count = int(sys.argv[2]) |
| 54 | + |
| 55 | +processed = [] |
| 56 | +notreplaced = set() |
| 57 | + |
| 58 | +for i in range(0,count): |
| 59 | + for j in range(0,MAX_FILES_PER_UNI): |
| 60 | + filename = prefix + str(i) + '_' + str(j) + '.owl' |
| 61 | + try: |
| 62 | + g = rdflib.Graph() |
| 63 | + g.parse(filename) |
| 64 | + |
| 65 | + for s,p,o in g: |
| 66 | + for r in [s,o]: |
| 67 | + if r not in res: |
| 68 | + res[r] = str(next_id) |
| 69 | + next_id += 1 |
| 70 | + |
| 71 | + if p in replace: |
| 72 | + add_rdf_edge(res[s], replace[p], res[o], graph) |
| 73 | + add_rdf_edge(res[s], replace[p] + 'R', res[o], graph) |
| 74 | + edges_count += 2 |
| 75 | + else: |
| 76 | + add_rdf_edge(res[s], 'OTHER', res[o], graph) |
| 77 | + edges_count += 1 |
| 78 | + notreplaced.add(p) |
| 79 | + |
| 80 | + processed.append(filename) |
| 81 | + print('Merged:', filename) |
| 82 | + except Exception: |
| 83 | + pass |
| 84 | + |
| 85 | +target = prefix + str(count) + 'v' + str(next_id) + 'e' + str(edges_count) # output file |
| 86 | +write_to_rdf(target,graph) |
| 87 | + |
| 88 | +print('Total vertices:', next_id) |
| 89 | +print('Total edges:', edges_count) |
| 90 | +print('Processed files:\n', processed) |
| 91 | +print('Not replaced labels:', notreplaced) |
0 commit comments