22from lf_toolkit .evaluation import Result , Params
33
44from evaluation_function .domain .formula import *
5+ from parsing .tokenizer import *
6+ from parsing .tree_builder import *
57
68
7- def parse_response (response : str ) -> tuple [bool , Formula | str ]:
9+ # def parse_response(response: str) -> tuple[bool, Formula | str]:
810
9- response = response .strip ()
11+ # response = response.strip()
1012
11- # binaryOperators = ["↔","→","∨","∧"]
12- # TODO: keep this mapping somewhere else for maintainability
13- binaryOperators = {
14- "↔" : Biconditional ,
15- "→" : Implication ,
16- "∨" : Disjunction ,
17- "∧" : Conjunction
18- }
19-
20- for binaryOperator in binaryOperators .keys ():
21-
22- if binaryOperator in response :
23- split_index = response .rindex (binaryOperator )
24-
25- left = response [:split_index ]
26- right = response [split_index + 1 :]
27-
28- # check left and right not empty strings
29- if not left :
30- return (False , f"missing text on left of { binaryOperator } " )
31- elif not right :
32- return (False , f"missing text on right of { binaryOperator } " )
13+ # # binaryOperators = ["↔","→","∨","∧"]
14+ # # TODO: keep this mapping somewhere else for maintainability
15+ # binaryOperators = {
16+ # "↔" : Biconditional,
17+ # "→" : Implication,
18+ # "∨" : Disjunction,
19+ # "∧" : Conjunction
20+ # }
21+
22+ # for binaryOperator in binaryOperators.keys():
23+
24+ # if binaryOperator in response:
25+ # split_index = response.rindex(binaryOperator)
26+
27+ # left = response[:split_index]
28+ # right = response[split_index+1:]
29+
30+ # # check left and right not empty strings
31+ # if not left:
32+ # return (False, f"missing text on left of {binaryOperator}")
33+ # elif not right:
34+ # return (False, f"missing text on right of {binaryOperator}")
3335
34- parse_left = parse_response (left )
35- parse_right = parse_response (right )
36+ # parse_left = parse_response(left)
37+ # parse_right = parse_response(right)
3638
37- error = False
38- err_msgs = []
39+ # error = False
40+ # err_msgs = []
3941
40- if not parse_left [0 ]:
41- error = True
42- err_msgs .append (parse_left [1 ])
42+ # if not parse_left[0]:
43+ # error = True
44+ # err_msgs.append(parse_left[1])
4345
44- if not parse_right [0 ]:
45- error = True
46- err_msgs .append (parse_right [1 ])
46+ # if not parse_right[0]:
47+ # error = True
48+ # err_msgs.append(parse_right[1])
4749
48- if error :
49- return (False , err_msgs .join ("\n " ))
50+ # if error:
51+ # return (False, err_msgs.join("\n"))
5052
51- # both sides are find and valid
52- result = binaryOperators [binaryOperator ](parse_left [1 ], parse_right [1 ])
53- return (True , result )
53+ # # both sides are find and valid
54+ # result = binaryOperators[binaryOperator](parse_left[1], parse_right[1])
55+ # return (True, result)
5456
5557
56- # TODO: keep this mapping somewhere else for maintainability
57- unaryOperators = {
58- "¬" : Negation
59- }
58+ # # TODO: keep this mapping somewhere else for maintainability
59+ # unaryOperators = {
60+ # "¬" : Negation
61+ # }
6062
61- for unaryOperator in unaryOperators .keys ():
63+ # for unaryOperator in unaryOperators.keys():
6264
63- #unary operator must syntactically be at the start of the string
64- if response [0 ] == unaryOperator :
65+ # #unary operator must syntactically be at the start of the string
66+ # if response[0] == unaryOperator:
6567
66- right = response [1 :]
67- #check not empty
68- if not right :
69- return (False , f"missing text on right of { unaryOperator } " )
70-
71- parse_right = parse_response (right )
72- if not parse_right [0 ]:
73- return parse_right
68+ # right = response[1:]
69+ # #check not empty
70+ # if not right:
71+ # return (False, f"missing text on right of {unaryOperator}")
72+
73+ # parse_right = parse_response(right)
74+ # if not parse_right[0]:
75+ # return parse_right
7476
75- result = unaryOperators [unaryOperator ](parse_right [1 ])
76- return (True , result )
77+ # result = unaryOperators[unaryOperator](parse_right[1])
78+ # return (True, result)
7779
78- # check if the formual is just True or Falsity
80+ # # check if the formual is just True or Falsity
7981
80- singletons = { # not sure what the official term for these symbols is
81- "⊤" : Truth ,
82- "⊥" : Falsity
83- }
82+ # singletons = { # not sure what the official term for these symbols is
83+ # "⊤" : Truth,
84+ # "⊥" : Falsity
85+ # }
8486
85- for singleton in singletons :
87+ # for singleton in singletons:
8688
87- if len (response ) > 1 and singleton in response :
88- return (False , f"not allowed to use { singleton } in the atom identifier" )
89+ # if len(response) > 1 and singleton in response:
90+ # return (False, f"not allowed to use {singleton} in the atom identifier")
8991
90- elif response == singleton :
91- result = singletons [singleton ]()
92- return (True , result )
92+ # elif response == singleton:
93+ # result = singletons[singleton]()
94+ # return (True, result)
9395
94- # response is likely an atom identifier
96+ # # response is likely an atom identifier
9597
96- return (True , Atom (response ))
98+ # return (True, Atom(response))
9799
98100
99101
@@ -129,8 +131,8 @@ def evaluation_function(
129131 """
130132
131133
132- if not isinstance (answer , str ):
133- raise Exception ("Answer must be a string/text." )
134+ # if not isinstance(answer, str):
135+ # raise Exception("Answer must be a string/text.")
134136
135137
136138 if not isinstance (response , str ):
@@ -147,8 +149,44 @@ def evaluation_function(
147149 feedback = None
148150 is_correct = False
149151
150- pl_formula = parse_response (response )
152+ # tokenize response
153+ tokenizer = Tokenizer (response )
154+ tokens = []
151155
152- #swtich on action
156+ try :
157+ while True :
158+ token = tokenizer .next_token ()
159+ tokens .append (token )
160+ if token .type == TokenType .EOF :
161+ break
162+
163+ except ValueError as e :
164+ return Result (
165+ is_correct = False ,
166+ feedback = str (e )
167+ )
168+
169+
170+ # parse tokens into Formula
171+ try :
172+ builder = TreeBuilder (tokens )
173+ formula = builder .build ()
174+
175+ except BuildError as e :
176+ return Result (
177+ is_correct = False ,
178+ feedback = str (e )
179+ )
180+
181+ #swtich on action
182+
183+ match action :
184+ case "tautology" :
185+ is_correct = TautologyEvaluator (formula ).evaluate ()
186+
187+ case "satisfiability" :
188+ is_correct = SatisfiabilityEvaluator (formula ).evaluate ()
189+
190+ # equivalence
153191
154- return Result (is_correct = False )
192+ return Result (is_correct = is_correct )
0 commit comments