-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCNFConverter
More file actions
executable file
·201 lines (188 loc) · 8.5 KB
/
CNFConverter
File metadata and controls
executable file
·201 lines (188 loc) · 8.5 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import sys
# Reduces the operators, if "and" is present inside "and" or "or" inside "or"
# Example : ["and", "A", ["and", "B", "C"]] should be ["and", "A", "B", "C"]
def reduceOperators(formula) :
if (isinstance(formula, str)) :
return formula
operator = formula[0]
literals = []
propositions = []
for index, item in enumerate(formula) :
if (index > 0) :
if (isinstance(item, str)) :
literals.append(item)
elif (isinstance(item, list)) :
propositions.append(reduceOperators(item))
newFormula = literals
for item in propositions :
if (isinstance(item, list) and item[0] == operator) :
for i,clause in enumerate(item) :
if (i > 0) :
newFormula.append(clause)
else :
newFormula.append(item)
newFormula.insert(0, operator)
return newFormula
# Removes duplicate elements from "and" and "or"
# Example : ["and", "A", "A"] should be "A"
def removeDuplicates(formula) :
if (isinstance(formula, str) or (isinstance(formula, list) and formula[0] == "not" and isinstance(formula[1], str))) :
return formula
for i, checkItem in enumerate(formula) :
if (i > 0) :
for j, item in reversed(list(enumerate(formula))) :
if (j > i) :
if (isinstance(item, list)) :
newItem = removeDuplicates(item)
formula.insert(j, newItem)
formula.remove(item)
if (checkItem == item) :
formula.remove(item)
if (isinstance(formula, list) and formula[0] != "not" and len(formula) < 3) :
return formula[1]
return formula
# Sorts the literals and lists in the formula (will be used for removing duplicate items in list)
# The literals are in the beginning followed by lists. And the "and" list is present in the end
def sort(formula) :
if (isinstance(formula, str)) :
return formula
operator = formula[0]
if (operator == "implies") :
return formula
literals = []
propositions = []
for index, item in enumerate(formula) :
if (index > 0) :
if (isinstance(item, str)) :
literals.append(item)
elif (isinstance(item, list)) :
propositions.append(sort(item))
if (len(literals) > 0) :
literals.sort()
if (len(propositions) > 0) :
propositions = sorted(propositions, key=lambda proposition: proposition[0], reverse=True)
newFormula = literals + propositions
newFormula.insert(0, operator)
return newFormula
# Converts to CNF by taking different cases separately
def convert(formula) :
if (isinstance(formula, str)):
return formula
elif(isinstance(formula,list)) :
# A => B ---> ~A | B
if (formula[0] == "implies") :
return convert(["or", convert(["not",convert(formula[1])]), convert(formula[2])])
# A <=> B ---> (~A | B) & (A | ~B)
elif (formula[0] == "iff") :
return convert("and", convert(["or", convert(["not", formula[1]]), formula[2]]), convert(["or", formula[1], convert(["not", formula[2]])]))
elif (formula[0] == "not") :
# ~p
if (isinstance(formula[1], str)) :
return formula
# ~~p ---> p
elif (isinstance(formula[1], list) and (formula[1])[0] == "not") :
return convert((formula[1])[1])
# ~(A & B & C & ...) ---> ~A | ~B | ~C | ....
elif (isinstance(formula[1], list) and (formula[1])[0] == "and") :
disjuncts = []
for index, item in enumerate(formula[1]) :
if (index > 0) :
disjuncts.append(convert(["not", item]))
disjuncts.insert(0, "or")
return convert(disjuncts)
# ~(A | B | C | ...) ---> ~A & ~B & ~C & ....
elif (isinstance(formula[1], list) and (formula[1])[0] == "or") :
conjuncts = []
for index, item in enumerate(formula[1]) :
if (index > 0) :
conjuncts.append(convert(["not", item]))
conjuncts.insert(0, "and")
return convert(conjuncts)
# ~(A => B) ---> A & ~B
elif (isinstance(formula[1], list) and ((formula[1])[0] == "implies")) :
return convert(["and", convert((formula[1])[1]), ["not", convert((formula[1][2]))]])
elif (isinstance(formula[1], list) and (formula[1])[0] == "iff") :
return convert(["not", convert(formula[1])])
elif (formula[0] == "or") :
# A | A ---> A
formula = sort(formula)
formula = removeDuplicates(formula)
# Handling the case ["or", "A", ["or", "B", "C"]]
formula = reduceOperators(formula)
# The order will be messed up when the redundant operators are removed
# Handling the case when the formula is reduced.
# For instance A or A is reduced to A
formula = sort(formula)
if (len(formula) == 1) :
return formula
if ((isinstance(formula[-1], list) and (formula[-1])[0] == "and")) :
# A | (B & C & D & ...) ---> (A | B) & (A | C) & (A | D) & ...
conjuncts = []
for i, item in enumerate(formula[-1]) :
if (i > 0) :
conjuncts.append(["or", formula[-2], item])
conjuncts.insert(0, "and")
# If only 2 items, then remove them and also remove OR
if (len(formula) < 4) :
return convert(conjuncts)
else :
formula.remove(formula[-1])
formula.remove(formula[-1])
formula.append(conjuncts)
return convert(formula)
# Case A OR B,
elif ((isinstance(formula[1], str) and isinstance(formula[2], str)) or (isinstance(formula[1], str) and isinstance(formula[2], list) and (formula[2])[0] == "not" and isinstance((formula[2])[1], str)) or (isinstance(formula[2], str) and isinstance(formula[1], list) and (formula[1])[0] == "not" and isinstance((formula[1])[1], str))) :
formula.append(["or", formula[1], formula[2]])
formula.remove(formula[1])
formula.remove(formula[1])
formula = reduceOperators(formula)
formula = sort(formula)
return formula
# Case !A OR !B
elif (isinstance(formula[1], list) and (formula[1])[0] == "not" and isinstance((formula[1])[1], str) and isinstance(formula[2], list) and (formula[2])[0] == "not" and isinstance((formula[2])[1], str)) :
return formula
# For any other operator compute the inner operator after or
else :
disjuncts = []
for i, item in enumerate(formula) :
if (i > 0) :
disjuncts.append(convert(item))
disjuncts.insert(0, "or")
return convert(disjuncts)
elif (formula[0] == "and") :
# Handling the case ["and", "A", ["or", "C", "D"], ["or", "D", "C"]]
formula = sort(formula)
formula = removeDuplicates(formula)
# Handling the case ["and", "A", ["and", "B", "C"]]
formula = reduceOperators(formula)
# The order will be messed up when the redundant operators are removed
formula = sort(formula)
if (len(formula) == 1) :
return formula
disjuncts = []
for i, item in enumerate(formula) :
if (i > 0) :
disjuncts.append(convert(item))
disjuncts.insert(0, "and")
disjuncts = reduceOperators(disjuncts)
return disjuncts
# Read the input file
inputFile = open(sys.argv[2])
numSentences = -1
sentences = []
for line in inputFile:
if (numSentences > 0):
sentences.append(eval(line.strip()))
else:
numSentences = int(line.strip())
outputFile = open('sentences_CNF.txt', 'w+')
# For each sentence convert it to CNF
for sentence in sentences:
formula = convert(sentence)
formula = sort(formula)
formula = removeDuplicates(formula)
formula = reduceOperators(formula)
formula = sort(formula)
outputFile.write (str(formula) + '\n')
inputFile.close()
outputFile.close()