blob: 632b5ea6cf1e2bc6e506386e72e024c3d6111dfe (
plain)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
import json
import os
import string
from copy import copy
dir = os.getenv("DIR")
if dir is None:
raise ValueError("DIR not specified")
collection = {}
files = [os.path.join(dir, file) for file in os.listdir(dir) if "ttl" in file]
def clean(text) -> str:
return text.strip(string.punctuation).strip()
for file in files:
with open(file) as f:
lines = f.readlines()
mem = ""
for ind, line in enumerate(lines):
if (not line.startswith("@")) and len(line) > 1:
if line.startswith("<"):
line = line.strip()
contents = line.split(" ")
key = (
clean(contents[0].split("/")[-1])
if "/" in contents[0]
else clean(contents[0].split(":")[-1])
)
mem = copy(key)
value = " ".join(
(
clean(content.split("/")[-1])
if "/" in content
else clean(content.split(":")[-1])
)
for content in contents[1:]
)
key = clean(key)
value = clean(value)
if key not in collection:
collection[key] = [value]
else:
if value not in collection[key]:
collection[key].append(value)
else:
line = line.strip()
key = copy(mem)
contents = line.split(" ")
value = " ".join(
(
clean(content.split("/")[-1])
if "/" in content
else clean(content.split(":")[-1])
)
for content in contents
)
key = clean(key)
value = clean(value)
if key not in collection:
collection[key] = [value]
else:
if value not in collection[key]:
collection[key].append(value)
with open(f"{dir}aggr_rdf.txt", "w") as new:
new.write(json.dumps(collection))
|