-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshowdeduced.py
executable file
·160 lines (129 loc) · 4.44 KB
/
showdeduced.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
#!/usr/bin/env python
import sys
import re
from itertools import permutations
from reasoner2 import *
def readFile(file):
d = {}
f = open(file, "r")
lines = f.readlines()
if sys.argv[2] in ['True', 'true', '1']:
return eliminateEquals(lines, d)
else:
return nonEliminateEquals(lines, d)
def completeDict(newAllConcepts, d):
indexList = []
pairs = []
pairs = list(permutations(newAllConcepts,2))
for pair in pairs:
reversePair = (pair[1], pair[0])
if pair in d:
pass
elif pair not in d and reversePair not in d:
d[pair] = "!<=>o"
elif pair not in d and reversePair in d:
symbol = d[reversePair]
if "<" in symbol and ">" not in symbol:
symbol = symbol.replace("<", ">")
elif ">" in symbol and "<" not in symbol:
symbol = symbol.replace(">", "<")
d[pair] = "".join(sorted(symbol))
diagnalConcepts = []
for k,v in d.iteritems():
if k[0] == k[1]:
diagnalConcepts.append(k)
for diagnalConcept in diagnalConcepts:
if diagnalConcept in d:
del d[diagnalConcept]
return d
def eliminateEquals(lines, d):
allConcepts = []
newAllConcepts = []
allRels = []
equalsPair = {}
for line in lines:
items = re.match("(\d+)\s+(\d+)\s+\[(.*)\]", line)
allConcepts.append(int(items.group(1)))
allConcepts.append(int(items.group(2)))
rel = "".join(sorted(items.group(3).replace(",","").replace(" ","")))
allRels.append([int(items.group(1)), int(items.group(2)), rel])
if rel == "=":
equalsPair[int(items.group(2))] = int(items.group(1))
for allRel in allRels:
for c1,c2 in equalsPair.iteritems():
if allRel[0] == c1:
allRel[0] = c2
if allRel[1] == c1:
allRel[1] = c2
allRels_set = set(map(tuple,allRels))
allRels = map(list,allRels_set)
allConcepts = list(set(allConcepts))
for allRel in allRels:
d[(allRel[0], allRel[1])] = allRel[2]
newAllConcepts.append(allRel[0])
newAllConcepts.append(allRel[1])
newAllConcepts = list(set(newAllConcepts))
d = completeDict(newAllConcepts, d)
return d
def nonEliminateEquals(lines, d):
allConcepts = []
allRels = []
for line in lines:
items = re.match("(\d+)\s+(\d+)\s+\[(.*)\]", line)
allConcepts.append(int(items.group(1)))
allConcepts.append(int(items.group(2)))
rel = "".join(sorted(items.group(3).replace(",","").replace(" ","")))
allRels.append([int(items.group(1)), int(items.group(2)), rel])
allRels_set = set(map(tuple,allRels))
allRels = map(list,allRels_set)
allConcepts = list(set(allConcepts))
for allRel in allRels:
d[(allRel[0], allRel[1])] = allRel[2]
d = completeDict(allConcepts, d)
return d
def main(d):
# test input
# d = {
# (0,1) : ">", (1,0) : "<",
# (0,2) : ">", (2,0) : "<",
# (0,3) : "=", (3,0) : "=",
# (0,4) : "!<=>o", (4,0) : "!<=>o",
# (0,5) : "!<=>o", (5,0) : "!<=>o",
# (1,2) : "!", (2,1) : "!",
# (1,3) : "!<=>o", (3,1) : "!<=>o",
# (1,4) : "<=", (4,1) : "=>",
# (1,5) : "!<=>o", (5,1) : "!<=>o",
# (2,3) : "!<=>o", (3,2) : "!<=>o",
# (2,4) : "!<=>o", (4,2) : "!<=>o",
# (2,5) : "=>", (5,2) : "<=",
# (3,4) : ">", (4,3) : "<",
# (3,5) : ">", (5,3) : "<",
# (4,5) : "!", (5,4) : "!",
# }
toDo = []
counter = {}
for k,v in d.iteritems():
if v != "!<=>o":
toDo.append(k)
d[k] = rcc[v]
relativeOfPair = {}
while len(toDo) > 0:
pair = toDo[0]
del toDo[0]
relativeOfPair = reasonOver(pair, d, toDo, counter)
d.update(relativeOfPair)
print "********"
for k,v in d.iteritems():
if k[0] < k[1]:
print k, findKey(rcc, v)
return
if __name__ == "__main__":
if len(sys.argv) != 5:
print "Usage: python <inputfile> <optimization_flag> <concept1> <concept2>"
exit(0)
d = readFile(sys.argv[1])
# for k,v in d.iteritems():
# if k[0] < k[1]:
# print k, v
# print "len(d)", len(d)
main(d)