Skip to content

Commit 5e14cd9

Browse files
committed
Add new default answer pattern for string type
1 parent b36caa3 commit 5e14cd9

File tree

4 files changed

+58
-22
lines changed

4 files changed

+58
-22
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,11 @@ Numerical questions consist of a Question, an optional Precision, and one or mor
233233

234234
## String Questions
235235

236-
String questions are specified by setting the "type" property to "string". These questions offer a single question prompt that is specified by the "question" property, but may have multiple possible answers specified by the Answers array. Each Answer in the Answers array is an object that consists of an "answer" (string), a Boolean called "correct", and several optional properties. By default answers case is ignored in comparing submissions to the Answers; however, this can be changed using the boolean "match_case" property. Each answer can have a string "feedback" that is displayed when this answer is matched. Fuzzy matching can be used by specifying a value for the "fuzzy_threshold" property, which should take values between 0 and 1. Fuzzy matching calculates the Levenshtein distance, dividing that by the string length, and then subtracting the result from 1. The resulting value is 1 when the strings match exactly and decreases when the strings are more different. The schema for String Questions is shown below:
236+
String questions are specified by setting the "type" property to "string". These questions offer a single question prompt that is specified by the "question" property, but may have multiple possible answers specified by the Answers array. Each Answer in the Answers array is an object that consists of an "answer" (string), a Boolean called "correct", and several optional properties. By default answers case is ignored in comparing submissions to the Answers; however, this can be changed using the boolean "match_case" property. Each answer can have a string "feedback" that is displayed when this answer is matched. Fuzzy matching can be used by specifying a value for the "fuzzy_threshold" property, which should take values between 0 and 1. Fuzzy matching calculates the Levenshtein distance, dividing that by the string length, and then subtracting the result from 1. The resulting value is 1 when the strings match exactly and decreases when the strings are more different.
237+
238+
You can optionally include a default answer pattern by specifying an object with a "type" property set to "default" and a "feedback" string. If no other answer patterns match the user's submission, the question will be marked incorrect and the provided feedback will be displayed.
239+
240+
The schema for String Questions is shown below:
237241

238242
![Schema for String Questions](schema/string_schema.png)
239243

jupyterquiz/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
All files in the package are distributed under the MIT License
1212
'''
1313

14-
__version__ = '2.9.5.2'
14+
__version__ = '2.9.6'
1515
from .dynamic import display_quiz, capture_responses

jupyterquiz/js/string.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,17 @@ function check_string(ths, event) {
5151
var correct;
5252
var done = false;
5353

54+
// Handle default answer pattern: filter out and capture default feedback
55+
var filteredAnswers = [];
56+
answers.forEach(answer => {
57+
if (answer.type === "default") {
58+
defaultFB = answer.feedback;
59+
} else {
60+
filteredAnswers.push(answer);
61+
}
62+
});
63+
answers = filteredAnswers;
64+
5465
answers.every(answer => {
5566
correct = false;
5667

schema/string_schema.json

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,50 @@
2121
"items": { "$ref": "#/$defs/possible_answer" }
2222
}
2323
},
24-
"$defs": {
24+
"$defs": {
2525
"possible_answer": {
26-
"type": "object",
27-
"properties": {
28-
"answer": {
29-
"type": "string"
26+
"oneOf": [
27+
{
28+
"type": "object",
29+
"properties": {
30+
"type": {
31+
"const": "default"
32+
},
33+
"feedback": {
34+
"type": "string"
35+
}
36+
},
37+
"required": [
38+
"type",
39+
"feedback"
40+
],
41+
"additionalProperties": false
3042
},
31-
"correct": {
32-
"type": "boolean"
33-
},
34-
"feedback": {
35-
"type": "string"
36-
},
37-
"match_case": {
38-
"type": "boolean"
39-
},
40-
"fuzzy_threshold": {
41-
"type": "number"
43+
{
44+
"type": "object",
45+
"properties": {
46+
"answer": {
47+
"type": "string"
48+
},
49+
"correct": {
50+
"type": "boolean"
51+
},
52+
"feedback": {
53+
"type": "string"
54+
},
55+
"match_case": {
56+
"type": "boolean"
57+
},
58+
"fuzzy_threshold": {
59+
"type": "number"
60+
}
61+
},
62+
"required": [
63+
"answer",
64+
"correct"
65+
],
66+
"additionalProperties": false
4267
}
43-
},
44-
"required": [
45-
"answer",
46-
"correct"
4768
]
4869
}
4970
}

0 commit comments

Comments
 (0)