-
Notifications
You must be signed in to change notification settings - Fork 0
/
RDF2JSON.py
39 lines (39 loc) · 1.37 KB
/
RDF2JSON.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
out_arquivo = open("output_final.json", "w+")
in_arquivo = open("own-pt.nt", "r")
out_arquivo.write("{\"dados\":[")
fix_control = 0
for linha in in_arquivo:
fix_control += 1
valores = linha.split()
control = -1
num = 0
string = ["SUJEITO", "OBJETO", "PREDICADO"]
for n in range( len(valores) ):
if num < 3 :
if valores[n].find("\"") == -1 & control<0:
string[num] = valores[n]
num += 1
elif valores[n].find("\"") != -1:
if valores[n].count("\"") == 1:
if control < 0:
string[num] = valores[n].replace("\"","").replace("\\","")
control = +1
else:
string[num] = string[num]+" "+valores[n].replace("\"","").replace("\\","")
control = -1
num += 1
elif valores[n].count("\"") == 2:
string[num] = valores[n].replace("\"","").replace("\\","")
num += 1
else:
valores[n].find("\"") == -1 & control>0
string[num] = string[num] +" "+valores[n]
if (fix_control == 1):
out_arquivo.write("\n{\"Subject\":\""+string[0]+"\",\"Predicado\":\""+string[1]+"\",\"Object\":\""+string[2]+"\"},")
elif (fix_control == 2):
out_arquivo.write("\n{\"Subject\":\""+string[0]+"\",\"Predicado\":\""+string[1]+"\",\"Object\":\""+string[2]+"\"}")
else:
out_arquivo.write(",\n{\"Subject\":\""+string[0]+"\",\"Predicado\":\""+string[1]+"\",\"Object\":\""+string[2]+"\"}")
out_arquivo.write("\n\n]}")
in_arquivo.close()
out_arquivo.close()