-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdlp_translater.py
176 lines (146 loc) · 5.4 KB
/
dlp_translater.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
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import traceback
import filtering
import os
def get_dlp_str(fact: list):
# fact[2]=fact[2].replace()
return fact[1] + "(" + fact[0] + "," + fact[2] + ") .\n"
def ttl2dlp(infile: str, outfile: str):
with open(infile, "r", encoding="utf-8") as f1, open(
outfile, "w", encoding="utf-8"
) as f2:
cnt = 0
for buf in f1:
fact = filtering.extract_triple_from_ttl(buf)
if fact is not None:
cnt += 1
f2.write(get_dlp_str(fact))
print("#facts: " + str(cnt))
def ttl2dlp_with_filtering(infile: str, outfile: str):
with open(infile, "r", encoding="utf-8") as f1, open(
outfile, "w", encoding="utf-8"
) as f2:
cnt = 0
tot = 0
for buf in f1:
tot += 1
fact = filtering.extract_triple_from_ttl(buf)
if fact is not None:
if filtering.filter_fact(fact):
cnt += 1
f2.write(get_dlp_str(fact))
print(f"after filtering: {cnt}/{tot}")
# deprecated
def rule2dlp(infilelist: list, outfile: str):
total = 0
num_valid_rules = 0
with open(outfile, "w", encoding="utf-8") as f2:
for infile in infilelist:
cur_num_vr = 0
with open(infile, "r", encoding="utf-8") as f1:
pt = f1.readline().split(" ")[0]
num = int(f1.readline().strip("\n").split(" ")[-1])
total += num
# print(pt, num)
for _ in range(num):
buf = f1.readline().split(" ")
atom = [buf[-6], buf[-3]]
is_inverse = ["^-1" in buf[x] for x in (-5, -2)]
params = ["(X,Z)", "(Z,Y)"]
# recursion is not allowed.
if atom[0] == pt or atom[1] == pt:
continue
# process the case that the predicate is inverse.
if is_inverse[0]:
params[0] = "(Z,X)"
if is_inverse[1]:
params[1] = "(Y,Z)"
num_valid_rules += 1
cur_num_vr += 1
f2.write(
pt
+ "(X,Y) :- "
+ atom[0]
+ params[0]
+ ", "
+ atom[1]
+ params[1]
+ ".\n"
)
print("%d/%d" % (cur_num_vr, num))
# vertebrate(X) :- animal(X), has_part(X,Y), skeleton(Y).
print("total: %d/%d" % (num_valid_rules, total))
def delete_illigal_lines(infile: str, outfile: str):
with open(infile, "r", encoding="utf-8") as f1, open(
outfile, "w", encoding="utf-8"
) as f2:
for buf in f1:
if "\\" not in buf:
f2.write(buf)
import re
# match rules of any length
pattern_rules = re.compile(r'(\S*?)\((.*?),(.*?)\)')
pattern_facts_dlp = re.compile(r'(<.*?>)\((<.*?>),(.*)\)')
# pattern_2 = re.compile(r'(.*?)\((.*?),(.*?)\) :- (.*?)\((.*?),(.*?)\)\.')
# pattern_3 = re.compile(r'(.*?)\((.*?),(.*?)\) :-[ (.*?)\((.*?),(.*?)\),]* (.*?)\((.*?),(.*?)\)\.')
# pattern_4 = re.compile(r'(.*?)\((.*?),(.*?)\) :- (.*?)\((.*?),(.*?)\), (.*?)\((.*?),(.*?)\), (.*?)\((.*?),(.*?)\)\.')
def dlp2fact(line: str):
# print(line)
result = pattern_facts_dlp.findall(line)
if len(result) > 0:
return [result[0][1], result[0][0], result[0][2]]
return None
def dlp2facts(infile: str):
facts = []
with open(infile, "r", encoding="utf-8") as f:
for buf in f:
fact = dlp2fact(buf.strip("\n"))
if fact is not None:
facts.append(fact)
return facts
def dlp_parser(fn: str, rl: int, limit_rules: int = 100000):
res = []
params = ["X"]
for i in range(1, rl - 1):
params.append("Z" + str(i))
with open(fn, "r", encoding="utf-8") as f:
# for buf in f:
file = f.readlines()
for i in range(min(len(file), limit_rules)):
buf = file[i]
result = pattern_rules.findall(buf.strip("\n"))
try:
item1 = [tup[0] for tup in result]
except Exception:
print("Error when matching rules in dlp_parser()!")
print(fn)
print(result)
exit(111)
item2 = []
for j in range(1, rl):
# print(result, params, i)
item2.append(result[j][1] == params[j - 1])
res.append([item1, item2])
return res
def dlp_writer(pt: str, pres: list, not_inverse: list):
path_len = len(pres)
if path_len == 1:
params = [["X", "Y"]]
else:
params = [["X", "Z1"]]
for i in range(1, path_len - 1):
params.append(["Z" + str(i), "Z" + str(i + 1)])
params.append(["Z" + str(path_len - 1), "Y"])
str_rule = pt + "(X,Y) :- "
for i in range(path_len):
str_rule += pres[i] + "("
if not not_inverse[i]:
params[i][0], params[i][1] = params[i][1], params[i][0]
str_rule += params[i][0] + "," + params[i][1]
if i == path_len - 1:
str_rule += ").\n"
else:
str_rule += "), "
return str_rule
if __name__ == "__main__":
# print(dlp_parser(os.path.join("test_v8","p0","rules.dlp"),2)[0][0])
pass