forked from ParhamP/Natural_Logic_Interpreter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NLI
executable file
·890 lines (753 loc) · 31 KB
/
NLI
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
#!/usr/bin/env python3
"""
MIT License
Copyright (c) 2017 Parham Pourdavood
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import re
class Expression:
"""
Expression class takes a logical expression in form of string and creates
an object with useful methods to manipulate the expression or to get useful
information from it.
Attributes
----------
expression: str
A logical argument using OR, AND, and NOT operations
keywords: ["OR", "AND", "IF", "THEN"]
List of keywords used in this program
and_regex: str
Regular expression used to identify conjunctive arguments
or_regex: str
Regular expression used to identify disjunctive arguments
conditional_regex: str
Regular expression used to identify conditional arguments
Methods
-------
get()
set(new_expression)
recognizer()
valid_parentheses_checker()
expression_parser()
is_pure_proposition()
negative_inverter()
temp_negative_inverter()
"""
def __init__(self, expression):
self.expression = expression
self.keywords = ["OR", "AND", "IF", "THEN"]
self.and_regex = r"(\(+.*?\)+) AND (\(+.*\)+)( AND \(+.*\)+)*$"
self.or_regex = r"(\(+.*?\)+) OR (\(+.*\)+)( OR \(+.*\)+)*$"
self.conditional_regex = r"IF (\(.*\)) THEN (\(.*\))$"
def __eq__(self, other):
return self.get() == other.get()
def __ne__(self, other):
return not self.__eq__(other)
def __hash__(self):
return hash(self.expression)
def __str__(self):
return self.expression
def get(self):
"""
Returns the expression of the object in string.
:return: self.expression
:rtype: str
"""
return self.expression
def set(self, new_expression):
"""
Sets the self.expression to a new string
:param new_expression
:type new_expression: str
:return: -
"""
self.expression = new_expression
def recognizer(self):
"""
Recognizes the main type of a logical argument.
:return:
"AND", "OR", "Conditional", "Pure", "Broken": str
"""
if re.match(self.or_regex, self.expression):
p = re.compile(self.or_regex)
m = re.match(p, self.expression)
for i in m.groups()[:-1]:
if "))" in i and "((" not in i or "((" in i and "))" not in i:
return "AND"
return "OR"
elif re.match(self.and_regex, self.expression):
p = re.compile(self.and_regex)
m = re.match(p, self.expression)
for i in m.groups()[:-1]:
if "))" in i and "((" not in i or "((" in i and "))" not in i:
return "OR"
return "AND"
elif re.match(self.conditional_regex, self.expression):
return "Conditional"
else:
# Here we check whether the argument contains any keywords or it
# is just stating a pure expression as truth.
flag = True
for i in self.keywords:
if i in self.expression:
flag = False
break
if flag:
return "Pure"
else:
# If none was applied it means the expression is broken
return "Broken"
def valid_parentheses_checker(self):
"""
Checks whether expression of an object is using valid from parentheses
:return: True | False
:rtype: bool
"""
if "(" not in self.expression and ")" not in self.expression:
return False
if "(" not in self.expression or ")" not in self.expression:
return False
else:
return True
def expression_parser(self):
"""
Parses the expression of an Expression object to its main parts and
creates new objects with the sub-parts and places them in a list.
:return: parsed_expression_list
:rtype: list
"""
if self.recognizer() == "AND":
parsed_expression = []
match = re.match(self.and_regex, self.expression)
groups = match.groups()[:-1]
for expression in groups:
parsed_expression.append(expression)
for i in parsed_expression:
if "))" in i and "((" in i:
list_index = parsed_expression.index(i)
i = i[1:-1]
parsed_expression[list_index] = i
elif "((" in i:
list_index = parsed_expression.index(i)
double_paren_index = i.index("((")
i = i[:double_paren_index] + i[double_paren_index + 1:]
parsed_expression[list_index] = i
elif "))" in i:
list_index = parsed_expression.index(i)
double_paren_index = i.index("))")
i = i[:double_paren_index + 1] + i[double_paren_index + 2:]
parsed_expression[list_index] = i
parsed_expression_list = []
for i in parsed_expression:
new_expression_object = Definer(i)
parsed_expression_list.append(new_expression_object)
return parsed_expression_list
elif self.recognizer() == "OR":
parsed_expression = []
match = re.match(self.or_regex, self.expression)
groups = match.groups()[:-1]
for expression in groups:
parsed_expression.append(expression)
for i in parsed_expression:
if "))" in i and "((" in i:
list_index = parsed_expression.index(i)
i = i[1:-1]
parsed_expression[list_index] = i
elif "((" in i:
list_index = parsed_expression.index(i)
i = i[1:]
parsed_expression[list_index] = i
elif "))" in i:
list_index = parsed_expression.index(i)
i = i[:-1]
parsed_expression[list_index] = i
parsed_expression_list = []
for i in parsed_expression:
new_expression_object = Definer(i)
parsed_expression_list.append(new_expression_object)
return parsed_expression_list
elif self.recognizer() == "Conditional":
condt_matched = re.match(self.conditional_regex, self.expression)
condt_object1 = Definer(condt_matched.group(1))
condt_object2 = Definer(condt_matched.group(2))
parsed_expression = {"IF": condt_object1,
"THEN": condt_object2}
return parsed_expression
def is_pure_proposition(self):
"""
Checks whether a proposition is pure, meaning it is just stating the
truth of an expression without any keywords.
:return: True | False
:rtype: bool
"""
if self.recognizer() == "Conditional":
parsed_expression = self.expression_parser()
for expression in parsed_expression:
if parsed_expression[expression].recognizer() != "Pure":
return False
return True
else:
for i in self.expression_parser():
if i.recognizer() != "Pure":
return False
return True
def negative_inverter(self):
"""
Removes NOT from the expression of an Expression object and sets it.
:return: -
"""
expression = self.expression
expression = expression[0] + expression[5:]
self.expression = expression
def temp_negative_inverter(self):
"""
Returns a string that has the NOT in the expression of an Expression
object removed.
:return: temp_inverted_object
:rtype: str
"""
expression = self.expression
expression = expression[0] + expression[5:]
temp_inverted_object = Expression(expression)
return temp_inverted_object
# ------------------------------------------------------------------------------
class Definer(Expression):
"""
Definer class is a subclass of Expression that provides methods useful for
defining logical expressions into our knowledge dictionary.
Attributes
----------
expression: str
A logical argument using OR, AND, and NOT operations
Methods
-------
and_definer()
or_definer()
conditional_definer()
definer()
and_in_or_checker(or_expression)
and_temp_transformer()
"""
def __init__(self, expression):
Expression.__init__(self, expression)
def and_definer(self):
"""
Defines a conjunctive expression into the knowledge dictionary and set
the expression as True.
:return: True
:rtype: bool
"""
for expression in self.expression_parser():
if "NOT" not in expression.expression:
knowledge_dict[expression] = True
else:
# If NOT is in the expression we invert it and set it to False.
expression.negative_inverter()
knowledge_dict[expression] = False
return True
def or_definer(self):
"""
Defines a disjunctive expression into the knowledge dictionary and set
the expression to True or False based on evaluation of previously
entered expressions.
:return: True | False | None
:rtype: bool
"""
expression_in_dict = False
for expression in self.expression_parser():
if "NOT" in expression.get():
reversed_temp = expression.temp_negative_inverter()
if reversed_temp in knowledge_dict:
expression_in_dict = True
break
continue
if expression in knowledge_dict:
expression_in_dict = True
break
if expression_in_dict is True:
for expression in self.expression_parser():
if "NOT" in expression.get():
reversed_temp = expression.temp_negative_inverter()
if reversed_temp not in knowledge_dict:
knowledge_dict[reversed_temp] = None
else:
if expression not in knowledge_dict:
knowledge_dict[expression] = None
# count to check if all elements are false
count = 0
for expression in self.expression_parser():
if "NOT" in expression.get():
expression.negative_inverter()
if knowledge_dict[expression] is False:
return True
elif knowledge_dict[expression] is True:
count += 1
else:
if knowledge_dict[expression] is True:
return True
elif knowledge_dict[expression] is False:
count += 1
if count == len(self.expression_parser()):
return False
else:
return None
else:
for expression in self.expression_parser():
if "NOT" in expression.get():
reversed_temp = expression.temp_negative_inverter()
if reversed_temp not in knowledge_dict:
knowledge_dict[reversed_temp] = None
else:
if expression not in knowledge_dict:
knowledge_dict[expression] = None
return True
def conditional_definer(self):
"""
Defines a conditional expression into the knowledge dictionary and set
the expression to True or False based on evaluation of previously
entered expressions.
:return: True | False | None
:rtype: bool
"""
for expression in self.expression_parser():
if "NOT" in self.expression_parser()[expression].expression:
continue
if self.expression_parser()[expression] not in knowledge_dict:
knowledge_dict[self.expression_parser()[expression]] = None
if "NOT" in self.expression_parser()["IF"].expression:
if_proposition = self.expression_parser()["IF"]
if_proposition.negative_inverter()
if if_proposition not in knowledge_dict:
return None
if knowledge_dict[if_proposition] is False:
if "NOT" in self.expression_parser()["THEN"].expression:
then_proposition = self.expression_parser()["THEN"]
then_proposition.negative_inverter()
knowledge_dict[then_proposition] = False
else:
knowledge_dict[self.expression_parser()["THEN"]] = True
return True
elif knowledge_dict[if_proposition] is True:
return True
else:
return None
else:
if knowledge_dict[self.expression_parser()["IF"]] is True:
if "NOT" in self.expression_parser()["THEN"].expression:
then_proposition = self.expression_parser()["THEN"]
then_proposition.negative_inverter()
knowledge_dict[then_proposition] = False
else:
knowledge_dict[self.expression_parser()["THEN"]] = True
return True
elif knowledge_dict[self.expression_parser()["IF"]] is False:
return True
else:
return None
def definer(self):
"""
Used as a general definer to be used in interpreter function. For the
sake of simplicity, definer checks the type of the expression in its
body and uses the right definer accordingly.
:return: True | False | None
:rtype: bool
"""
if self.recognizer() == "AND":
return self.and_definer()
elif self.recognizer() == "OR":
return self.or_definer()
elif self.recognizer() == "Conditional":
return self.conditional_definer()
def special_definer(self):
"""
Special definer is used for defining the AND expressions that have been
previously been in an OR expression. It is special since we don't want
to set them to True.
:return: True | False | None
:rtype: bool
"""
for expression in self.expression_parser():
if expression not in knowledge_dict:
knowledge_dict[expression] = None
true_count = 0
for expression in self.expression_parser():
if knowledge_dict[expression] is None:
return None
if knowledge_dict[expression] is False:
return False
if knowledge_dict[expression] is True:
true_count += 1
if true_count == len(self.expression_parser()):
return True
def and_in_or_checker(self, main_expression):
"""
Checks whether the expression we are looking is an AND expression that
is part of an OR expression.
:param main_expression:
:type main_expression: Expression
:return: True | False
:rtype: bool
"""
if self.recognizer() == "AND" and main_expression.recognizer() == "OR":
return True
else:
return None
def and_temp_transformer(self):
"""
Sets a mark in the expression so we can recognize it's different later.
:return: -
"""
self.expression = self.expression + "@"
# ------------------------------------------------------------------------------
class Resolver(Expression):
"""
Resolver class is a subclass of Expression that provides methods useful for
resolving logical expressions in our knowledge dictionary into proof_dict.
Attributes
----------
expression: str
A logical argument using OR, AND, and NOT operations
Methods
-------
and_resolver()
or_resolver()
conditional_resolver()
general_resolver()
"""
def __init__(self, expression):
Expression.__init__(self, expression)
def and_resolver(self):
"""
Resolves an AND expression based on other expressions stored in
proof_dict.
:return: True | False | None
:rtype: bool
"""
for expression in self.expression_parser():
if "NOT" in expression.get():
continue
if expression not in proof_dict or expression is None:
proof_dict[expression] = None
return None # Can't be determined
true_count = 0
for expression in self.expression_parser():
if "NOT" in expression.get():
expression.negative_inverter()
if proof_dict[expression] is True:
return False
if proof_dict[expression] is False:
true_count += 1
else:
if proof_dict[expression] is False:
return False
if proof_dict[expression] is True:
true_count += 1
if true_count == len(self.expression_parser()):
return True
def or_resolver(self):
"""
Resolves an OR expression based on other expressions stored in
proof_dict.
:return: True | False | None
:rtype: bool
"""
for expression in self.expression_parser():
if "NOT" in expression.get():
continue
if expression not in proof_dict or proof_dict[expression] is None:
proof_dict[expression] = None
# count to check if all elements are false
count = 0
for expression in self.expression_parser():
if "NOT" in expression.get():
expression.negative_inverter()
if expression not in proof_dict:
proof_dict[expression] = None
if proof_dict[expression] is False:
return True
if proof_dict[expression] is True:
count += 1
else:
if proof_dict[expression] is True:
return True
if proof_dict[expression] is False:
count += 1
if count == len(self.expression_parser()):
return False
else:
return None
def conditional_resolver(self):
"""
Resolves a conditional expression based on other expressions stored in
proof_dict.
:return: True | False | None
:rtype: bool
"""
if_statement = self.expression_parser()["IF"]
then_statement = self.expression_parser()["THEN"]
if then_statement not in proof_dict:
proof_dict[then_statement] = None
if "NOT" in if_statement.get() and "NOT" not in then_statement.get():
if_statement.negative_inverter()
if if_statement not in proof_dict:
proof_dict[if_statement] = None
return None
if proof_dict[if_statement] is False and proof_dict[
then_statement] is False:
return False
elif proof_dict[if_statement] is True:
return True
elif proof_dict[if_statement] is False and proof_dict[
then_statement] is True:
return True
else:
return None
elif "NOT" in if_statement.get() and "NOT" in then_statement.get():
if_statement.negative_inverter()
then_statement.negative_inverter()
if if_statement not in proof_dict:
proof_dict[if_statement] = None
return None
if proof_dict[if_statement] is False and proof_dict[
then_statement] is True:
return False
elif proof_dict[if_statement] is True:
return True
elif proof_dict[if_statement] is False and proof_dict[
then_statement] is False:
return True
else:
return None
elif "NOT" not in if_statement.get() and "NOT" in then_statement.get():
then_statement.negative_inverter()
if if_statement not in proof_dict:
proof_dict[if_statement] = None
return None
if proof_dict[if_statement] is True and proof_dict[
then_statement] is True:
return False
elif proof_dict[if_statement] is False:
return True
elif proof_dict[if_statement] is True and proof_dict[
then_statement] is False:
return True
else:
return None
else:
if if_statement not in proof_dict:
proof_dict[if_statement] = None
return None
if proof_dict[if_statement] is True and proof_dict[
then_statement] is False:
return False
elif proof_dict[if_statement] is False:
return True
elif proof_dict[if_statement] is True and proof_dict[
then_statement] is True:
return True
else:
return None
def general_resolver(self):
"""
Used as a general resolver to be used in validator function. For the
sake of simplicity, general_resolver checks the type of the expression
in its body and uses the right resolver accordingly.
:return: True | False | None
:rtype: bool
"""
if self.recognizer() == "AND":
return self.and_resolver()
elif self.recognizer() == "OR":
return self.or_resolver()
elif self.recognizer() == "Conditional":
return self.conditional_resolver()
# ------------------------------------------------------------------------------
def interpreter(expression):
"""
interpreter function is a recursive function that uses divide and conquer
to go through nested arguments of the input expression and defines them
accordingly.
:param expression:
:type expression: str
:return: -
"""
# Let's check to see if we have an AND operator that was part of an AND
flag = False
if expression[-1] == "@":
# When it was delete the mark but set the flag to True so we will treat
# it differently in the future.
flag = True
# Flag has become True and we can normalize the expression again
expression = expression[0:-1]
# Create an expression class with the expression string.
expression_object = Definer(expression)
# First base case
if expression_object.recognizer() == "Pure":
if "NOT" not in expression_object.get():
knowledge_dict[expression_object] = True
else:
expression_object.negative_inverter()
knowledge_dict[expression_object] = False
# Second base case
elif expression_object.is_pure_proposition():
if flag:
knowledge_dict[
expression_object] = expression_object.special_definer()
else:
knowledge_dict[expression_object] = expression_object.definer()
# Third base case
else:
parsed_expression = expression_object.expression_parser()
if flag:
knowledge_dict[
expression_object] = expression_object.special_definer()
else:
knowledge_dict[expression_object] = expression_object.definer()
# Check to see if it is conditional so we can mark its IF proposition
if expression_object.recognizer() == "Conditional":
for expression in parsed_expression.values():
expression_type = expression.recognizer()
if expression_type == "AND":
parsed_expression["IF"].and_temp_transformer()
# Recursive step
if expression_type != "Pure" and expression_type != "Broken":
interpreter(expression.get())
else:
for expression in parsed_expression:
expression_type = expression.recognizer()
# Check to see if any AND was part of an OR proposition
if expression.and_in_or_checker(expression_object) is True:
expression.and_temp_transformer()
# Recursive step
if expression_type != "Pure" and expression_type != "Broken":
interpreter(expression.get())
# ------------------------------------------------------------------------------
def validator(expression):
"""
validator function is a recursive function that uses divide and conquer
to go through nested arguments of the input expression and validates them
accordingly. (Uses arguments previously defined in knowledge dictionary that
was later on copied into proof_dict. validator stores the result in
proof_dict.
:param expression:
:type expression: str
:return: -
"""
# Create an object with the expression string.
expression_object = Resolver(expression)
expression_object_type = expression_object.recognizer()
parsed_expression = expression_object.expression_parser()
# First base case
if expression_object_type == "Pure":
if "NOT" in expression_object.get():
temp_inverted = expression_object.temp_negative_inverter()
if temp_inverted not in knowledge_dict:
proof_dict[expression_object] = None
return None
elif knowledge_dict[temp_inverted] is True:
proof_dict[expression_object] = False
return False
else:
proof_dict[expression_object] = True
return True
else:
if expression_object not in knowledge_dict:
proof_dict[expression_object] = None
return None
else:
proof_dict[expression_object] = knowledge_dict[
expression_object]
return proof_dict[expression_object]
# Second base case
elif expression_object.is_pure_proposition() is True:
proof_dict[expression_object] = expression_object.general_resolver()
else:
proof_dict[expression_object] = expression_object.general_resolver()
if expression_object.recognizer() == "Conditional":
for expression in parsed_expression.values():
expression_type = expression.recognizer()
# Recursive step
if expression_type != "Pure" and expression_type != "Broken":
validator(expression.get())
else:
for expression in parsed_expression:
expression_type = expression.recognizer()
# Recursive step
if expression_type != "Pure" and expression_type != "Broken":
validator(expression.get())
# ------------------------------------------------------------------------------
if __name__ == '__main__':
# Knowledge dict is where we store all the definitions defined by definer
# function.
knowledge_dict = dict()
user_input = ""
print("Please keep entering the logical arguments you would like to" +
" define.\nTo see the results and further validate new arguments" +
" based on your arguments enter -1.")
input_list = list()
while user_input != "-1":
user_input = input("\nNew argument:\t")
if user_input != "-1":
expression_object = Expression(user_input)
if expression_object.recognizer() == "Broken":
print("Incorrect Syntax. Please try again: ")
continue
elif expression_object.recognizer() == "Pure":
if expression_object.valid_parentheses_checker() is False:
print("Parentheses do not exist or aren't in a valid form.")
continue
input_list.append(user_input)
failed_expression = set()
for count in range(2):
# We repeat the procedure of defining to make sure all the elements get
# updated.
for expression in input_list:
try:
interpreter(expression)
except Exception:
failed_expression.add(expression)
for expression in failed_expression:
print("\nThis expression could not be submitted due to a problem: ",
expression)
print(40 * "-" + "\nExpressions and arguments you defined: ")
for expression in knowledge_dict:
print(expression.get(), "--->", knowledge_dict[expression])
print(40 * "-" + "\nEnter the new argument you would like to validate: " +
"\nEnter 'view' at any time to see the full list of arguments and" +
" their results\nEnter 'exit' to quit.")
# proof_dict is where we store all the validation results we resolved.
# At the beelining we copy all the elements of knowledge_dict so resolver
# can use previously defined arguments.
proof_dict = knowledge_dict.copy()
while user_input != "exit":
user_input = input("\nValidate:\t")
if user_input != "exit":
if user_input == "view":
for expression in proof_dict:
print(expression.get(), "--->", proof_dict[expression])
else:
for i in range(2):
# We repeat the procedure of defining to make sure all the
# elements get validated.
try:
validator(user_input)
except Exception:
"There is a problem with this argument."
continue
for expression in proof_dict:
if expression.get() == user_input:
print(user_input, "----->", proof_dict[expression])