-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbooleanModel.py
More file actions
188 lines (166 loc) · 5.17 KB
/
booleanModel.py
File metadata and controls
188 lines (166 loc) · 5.17 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
import os
import re
dir=r"C:\Users\VEDANK\Desktop\Files2"
count=0
files = []
allwords = []
for r, d, f in os.walk(dir):
for file in f:
files.append(os.path.join(r, file))
count += 1
print(count)
table=dict()
for f in files:
file = open(f, 'r')
text = file.read().lower()
words=text.split()
for x in words:
allwords.append(x)
allwords=list(dict.fromkeys(allwords))
for key in allwords:
valueList=[]
for f in files:
file = open(f, 'r')
text = file.read().lower()
# found = re.search(' ' + key + ' ', ' ' + text + ' ')
if(f' {key} ' in f' {text} '):
valueList.append(1)
else:
valueList.append(0)
table[key] = valueList
print("{:<15} {:<8} {:<8} {:<8} {:<8} {:<8} ".format('Key','D1','D2','D3','D4','D5'))
for k, v in table.items():
d1, d2, d3, d4, d5 = v
print("{:<15} {:<8} {:<8} {:<8} {:<8} {:<8} ".format(k, d1, d2, d3, d4, d5))
print("============================================================")
i=0
result=[]
operators=[ '(', ')', 'OR', 'AND', 'NOT']
ops=[]
values=[]
operator=None
precedence = {
"NOT": 3,
"AND": 2,
"OR": 1,
"(": 0,
")": 0
}
query = input("Enter query: ")
query = query.split()
for i in range(len(query)):
print(i,query[i])
def performOp(v1,v2,op):
print("function")
for i in range(len(query)):
if (query[i] == '('):
ops.append(query[i])
elif (query[i] in allwords):
values.append(query[i])
elif (query[i] == ')'):
while len(ops) != 0 and ops[-1] != '(':
val2 = values.pop()
if (len(values) !=0):
val1 = values.pop()
if type(val1) == str:
vector1 = table[val1]
else:
vector1 = val1
operator = ops.pop()
if type(val2) == str:
vector2 = table[val2]
else:
vector2 = val2
print("v1")
print(vector1)
print("v2")
print(vector2)
if(operator == 'OR'):
currentVector = [vector1 or vector2 for vector1, vector2 in zip(vector1, vector2)]
values.append(currentVector)
print(currentVector)
if(operator == 'AND'):
currentVector = [vector1 and vector2 for vector1, vector2 in zip(vector1, vector2)]
values.append(currentVector)
print(currentVector)
if(operator == 'NOT'):
n=[]
for j in vector2:
if j == 1:
n.append(0)
else:
n.append(1)
values.append(n)
ops.pop()
# operator
else:
while (len(ops) != 0 and precedence[ops[-1]] >= precedence[query[i]]):
val2 = values.pop()
if (len(values) !=0):
val1 = values.pop()
if type(val1) == str:
vector1 = table[val1]
else:
vector1 = val1
operator = ops.pop()
if type(val2) == str:
vector2 = table[val2]
else:
vector2 = val2
print("v1")
# print(vector1)
print("v2")
print(vector2)
if(operator == 'OR'):
currentVector = [vector1 or vector2 for vector1, vector2 in zip(vector1, vector2)]
values.append(currentVector)
print(currentVector)
if(operator == 'AND'):
currentVector = [vector1 and vector2 for vector1, vector2 in zip(vector1, vector2)]
values.append(currentVector)
print(currentVector)
if(operator == 'NOT'):
n=[]
for j in vector2:
if j == 1:
n.append(0)
else:
n.append(1)
values.append(n)
ops.append(query[i])
i += 1
while (len(ops) != 0):
val2 = values.pop()
if (len(values) !=0):
val1 = values.pop()
if type(val1) == str:
vector1 = table[val1]
else:
vector1 = val1
operator = ops.pop()
if type(val2) == str:
vector2 = table[val2]
else:
vector2 = val2
print("v1")
print(vector1)
print("v2")
print(vector2)
if(operator == 'OR'):
currentVector = [vector1 or vector2 for vector1, vector2 in zip(vector1, vector2)]
values.append(currentVector)
print(currentVector)
if(operator == 'AND'):
currentVector = [vector1 and vector2 for vector1, vector2 in zip(vector1, vector2)]
values.append(currentVector)
print(currentVector)
if(operator == 'NOT'):
n=[]
for j in vector2:
if j == 1:
n.append(0)
else:
n.append(1)
values.append(n)
print("Result")
print(values)