Skip to content

Commit cd06a93

Browse files
committed
Strict structure matching implemented
Aimed at matching only when symbols in the response correspond to symbols in the answer; excludes when the student has used a number instead. This is a crude first go.
1 parent 25fb554 commit cd06a93

1 file changed

Lines changed: 60 additions & 0 deletions

File tree

evaluate.m

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,62 @@
218218
|>
219219
]
220220

221+
(* UnnamedSymbols: a function that takes an expression and a list of named variables,
222+
and returns all other symbolic quantities in the expression. *)
223+
224+
UnnamedSymbols[expression_,namedVariables_] :=
225+
Cases[Reap[Scan[Sow,expression,{-1}]][[2,1]],a_Symbol/;Not[MemberQ[namedVariables,a]]]
226+
227+
(* StrictStructureMatchQ: a function that matches structures more strictly,taking account of the unnamed symbols
228+
in each.*)
229+
230+
(* THIS FUNCTION IS FLAWED, AND REPRESENTS A CRUDE FIRST GO *)
231+
232+
(* IN PARTICULAR, THE COMPARISON OF THE LENGTHS OF THE SYMBOL LISTS IS VERY HAMFISTED *)
233+
234+
StrictStructureMatchQ[response_,answerTemplate_,namedVariables_] :=
235+
StructureMatchQ[response,answerTemplate,namedVariables]&&
236+
TrueQ[
237+
(Length[Union[UnnamedSymbols[response,namedVariables]]]==
238+
Length[Union[UnnamedSymbols[answerTemplate,namedVariables]]])]
239+
240+
(* SemanticAndStrictStructureMatchQ: a function that combines a strict structure comparison with a test of
241+
mathematical equivalence *)
242+
243+
SemanticAndStrictStructureMatchQ[response_,answer_,answerTemplate_,namedVariables_] :=
244+
TrueQ[SemanticMatchQ[response,answer]&&StrictStructureMatchQ[response,answerTemplate,namedVariables]]
245+
246+
equalQStrictStructure[answer_, response_, params_] := Module[{namedVariables,correctQ},
247+
Print["Evaluating Structure"];
248+
namedVariables = ToExpression[Lookup[params,"named_variables",{}],TraditionalForm];
249+
correctQ = StrictStructureMatchQ[
250+
ToExpression[ToString[response],TraditionalForm],
251+
ToExpression[ToString[answer],TraditionalForm],
252+
namedVariables];
253+
254+
<|
255+
"error" -> Null,
256+
"is_correct" -> correctQ
257+
|>
258+
]
259+
260+
equalQSemanticAndStrictStructure[answer_, response_, params_] := Module[{namedVariables,answerTemplate,correctQ},
261+
Print["Evaluating SemanticAndStructure"];
262+
namedVariables = ToExpression[Lookup[params,"named_variables",{}],TraditionalForm];
263+
answerTemplate = ToExpression[Lookup[params,"answer_template",{}],TraditionalForm];
264+
correctQ = SemanticAndStrictStructureMatchQ[
265+
ToExpression[ToString[response],TraditionalForm],
266+
ToExpression[ToString[answer],TraditionalForm],
267+
ToExpression[ToString[answerTemplate],TraditionalForm],
268+
namedVariables
269+
];
270+
271+
<|
272+
"error" -> Null,
273+
"is_correct" -> correctQ
274+
|>
275+
]
276+
221277
(* The evaluation function itself *)
222278

223279
evalQ[type_, answer_, response_, params_] := Module[{},
@@ -228,6 +284,10 @@
228284
equalQSemantic[answer,response,params],
229285
type = "semantic_and_structure",
230286
equalQSemanticAndStructure[answer,response,params],
287+
type = "strict_structure",
288+
equalQStrictStructure[answer,response,params],
289+
type = "semantic_and_strict_structure",
290+
equalQSemanticAndStrictStructure[answer,response,params],
231291
NumericQ[answer],
232292
equalQNumeric[answer, response, params],
233293
True,

0 commit comments

Comments
 (0)