Skip to content

Commit d9d395d

Browse files
committed
Merge branch 'ciromoraismedeiros-master'
2 parents f7933e4 + 41d5800 commit d9d395d

4 files changed

Lines changed: 56 additions & 2 deletions

File tree

init.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,16 @@ def install_gtgraph():
118118
print('Installation of GTgraph is finished.')
119119

120120

121+
def to_file(filepath, graph):
122+
with open(filepath, 'w') as out_file:
123+
for t in graph:
124+
s = t[0]
125+
p = t[1]
126+
o = t[2]
127+
out_file.write('%s %s %s\n'%(s,p,o))
128+
121129
def unpack_graphs(graph_key):
122-
to = os.path.join(DATA_ROOT_DIR, graph_key)
130+
to = os.path.join(DATA_ROOT_DIR, graph_key)
123131
arch = os.path.join(to, '%s.tar.xz' % MATRICES_DIR)
124132
print('Unpack ', arch, ' to ', to)
125133
unpack(arch, to)

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
numpy
2-
rdflib
2+
rdflib

tools/RDF_to_triple/convconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
http://www.w3.org/2000/01/rdf-schema#subClassOf SCO
2+
http://www.w3.org/1999/02/22-rdf-syntax-ns#type T

tools/RDF_to_triple/converter.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""
2+
RDF converter from popular formats (XML, Turtle, etc.) to simple triple
3+
format (.txt).
4+
5+
Usage:
6+
- Create a conversion configuration file. Each line must contain an IRI, a
7+
whitespace character and a string to replace the IRI by.
8+
- Run converter.py <data> <config>
9+
- Result will have the same name of the input data, with .txt extension.
10+
The graph will contain explicit inverted edges added an 'R'.
11+
"""
12+
13+
import rdflib, sys, os
14+
15+
if len(sys.argv) < 2:
16+
print('Usage: converter.py <data> <config>')
17+
exit()
18+
19+
replace = {} # map for replacing predicates
20+
config = sys.argv[2]
21+
for l in open(config,'r').readlines():
22+
pair = l.split(' ')
23+
old = rdflib.URIRef(pair[0].strip(' '))
24+
new = pair[1].strip('\n').strip(' ')
25+
replace[old] = new
26+
27+
res = {} # map from resources to integer ids
28+
next_id = 0 # id counter
29+
30+
graph = rdflib.Graph()
31+
graph.parse(sys.argv[1])
32+
33+
out = open(os.path.splitext(sys.argv[1])[0]+'.txt','w') # output file
34+
for s,p,o in graph:
35+
for r in [s,o]:
36+
if r not in res:
37+
res[r] = str(next_id)
38+
next_id += 1
39+
40+
if p in replace:
41+
out.write(res[s]+' '+replace[p]+' ' +res[o]+'\n')
42+
out.write(res[o]+' '+replace[p]+'R '+res[s]+'\n')
43+
else:
44+
out.write(res[s]+' OTHER '+res[o]+'\n')

0 commit comments

Comments
 (0)