-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluation_function.wl
More file actions
179 lines (147 loc) · 5.44 KB
/
evaluation_function.wl
File metadata and controls
179 lines (147 loc) · 5.44 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
(* ::Package:: *)
(* The basic evaluation function code*)
equalQNumeric[answer_, response_, params_] := Module[{tolerance},
Print["Evaluating Equal Numeric"];
tolerance = If[Lookup[params, "tolerance_is_absolute", False],
Lookup[params, "tolerance", 0],
Lookup[params, "tolerance", 0] * answer
];
error = Abs[answer - response];
<|
"error" -> error,
"is_correct" -> TrueQ[error <= tolerance]
|>
]
equalQOther[answer_, response_, params_] := Module[{correctQ},
Print["Evaluating Equal Other"];
<|
"error" -> Null,
"is_correct" -> TrueQ[answer == response]
|>
];
(* Patternize: a function that takes an expression and a list of \
named variables, and converts all unnamed symbols in the expression \
into Optional[..] patterns *)
Options[PatternizeSymbol] = {Atomic -> False};
PatternizeSymbol[a_Symbol, namedVariables_, OptionsPattern[]] /;
Not[MemberQ[namedVariables, a]] := \!\(\*
TagBox[
StyleBox[
RowBox[{"If", "[",
RowBox[{
RowBox[{"OptionValue", "[", "Atomic", "]"}], ",",
RowBox[{"(",
RowBox[{"Optional", "[",
RowBox[{"PatternTest", "[",
RowBox[{
RowBox[{"pattern", "[",
RowBox[{"a", ",",
RowBox[{"Blank", "[", "]"}]}], "]"}], ",", "AtomQ"}], "]"}], "]"}], ")"}], ",",
RowBox[{"(",
RowBox[{"Optional", "[",
RowBox[{"pattern", "[",
RowBox[{"a", ",",
RowBox[{"Blank", "[", "]"}]}], "]"}], "]"}], ")"}]}], "]"}],
ShowSpecialCharacters->False,
ShowStringCharacters->True,
NumberMarks->True],
FullForm]\) /. pattern -> Pattern
PatternizeSymbol[a_, namedVariables_, OptionsPattern[]] := a
ComplexResolve[Optional[a_Symbol] + I Optional[b_Symbol]] :=
Complex[a, b]
ComplexResolve[I Optional[b_Symbol]*Pi] := Complex[0, b]*Pi
ComplexResolve[Complex[0, Optional[b_Symbol]] + Optional[a_Symbol]] :=
Complex[a, b]
ComplexResolve[a_] := a
DepatternizePattern[pattern_Optional] := pattern[[1, 1, 1]]
DepatternizePattern[pattern_] := pattern
ComplexResolve[Optional[a_Symbol] + I Optional[b_Symbol]] :=
Complex[a, b]
ComplexResolve[I Optional[b_Symbol]*Pi] := Complex[0, b]*Pi
ComplexResolve[Complex[0, Optional[b_Symbol]] + Optional[a_Symbol]] :=
Complex[a, b]
ComplexResolve[a_] := a
Options[Patternize] = {Atomic -> False};
Patternize[expression_, namedVariables_, OptionsPattern[]] :=
Map[PatternizeSymbol[#, namedVariables,
Atomic -> OptionValue[Atomic]] &,
MapAll[ComplexResolve, expression], {-1}]
Depatternize[pattern_] := MapAll[DepatternizePattern, pattern]
(*StructureMatchQ: a function that checks whether a user's response \
has the same structure as a given answer template, given a set of \
named variables.*)
inertFunctionRules = {Sin -> fSin, Cos -> fCos, Tan -> fTan,
Sec -> fSec, Csc -> fCsc, Cot -> fCot, ArcSin -> fArcSin,
ArcCos -> fArcCos, ArcTan -> fArcTan, ArcSec -> fArcSec,
ArcCsc -> fArcCsc, ArcCot -> fArcCot, Sinh -> fSinh, Cosh -> fCosh,
Tanh -> fTanh, Sech -> fSech, Csch -> fCsch, Coth -> fCoth,
ArcSinh -> fArcSinh, ArcCosh -> fArcCosh, ArcTanh -> fArcTanh,
ArcSech -> fArcSech, ArcCsch -> fArcCsch, ArcCoth -> fArcCoth,
Exp -> fExp, Log -> fLog};
Options[StructureMatchQ] = {Atomic -> False};
StructureMatchQ[response_, answerTemplate_, namedVariables_,
OptionsPattern[]] :=
Module[{response2, answerTemplate2},
response2 = ReplaceAll[response, inertFunctionRules];
answerTemplate2 = ReplaceAll[answerTemplate, inertFunctionRules];
MatchQ[response2,
Patternize[answerTemplate2, namedVariables,
Atomic -> OptionValue[Atomic]]]]
equalQStructure[answer_, response_, params_] := Module[{namedVariables,correctQ},
Print["Evaluating Structure"];
namedVariables = ToExpression[Lookup[params,"named_variables",{}],TraditionalForm];
correctQ = StructureMatchQ[
ToExpression[ToString[response],TraditionalForm],
ToExpression[ToString[answer],TraditionalForm],
namedVariables];
<|
"error" -> Null,
"is_correct" -> correctQ
|>
]
(* The evaluation function itself *)
evalQ[type_, answer_, response_, params_] := Module[{},
Which[
type == "structure",
equalQStructure[answer,response,params],
NumericQ[answer],
equalQNumeric[answer, response, params],
True,
equalQOther[answer, response, params]
]
];
EvaluationFunction[type_, answer_, response_, params_] := Module[{tolerance, correctQ, error},
Print["Running Evaluation Function"];
result = evalQ[type, answer, response, params];
Print["Results"];
Print[result];
feedback = If[result["is_correct"],
Lookup[params, "correct_response_feedback", "Correct!"],
Lookup[params, "incorrect_response_feedback", "Incorrect!"]
];
<|
"command" -> "eval",
"result" -> <|
"is_correct" -> result["is_correct"],
"feedback" -> feedback,
"error" -> result["error"]
|>
|>
];
evalQuestionIO = Function[
Module[{jsonData, result},
jsonData = Import[#1, "JSON"] //. List :> Association;
requestData = jsonData["params"];
answer = requestData["answer"];
response = requestData["response"];
params = requestData["params"];
type = params["comparisonType"];
Print["Evaluating Response Against Answer"];
result = EvaluationFunction[type, answer, response, params];
Print["Response"];
Print[result];
Export[#2, result, "JSON", "Compact" -> True]
]
];
argv = Rest[$ScriptCommandLine];
evalQuestionIO[argv[[1]], argv[[2]]]