feat(assessment): grade choice answers with distractors from error models
This commit is contained in:
1 parent
efa41d5967
commit
f1147b1885
22 files changed
+1346
-47
No files matched your search
@@ -0,0 +1,166 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
const (
|
||||
// choiceOptionCount is the number of options that a generated choice
|
||||
// variant offers: the correct one and choiceOptionCount-1 distractors.
|
||||
choiceOptionCount = 4
|
||||
// minChoiceOptions and maxChoiceOptions bound the options of a frozen
|
||||
// choice variant. They are the bounds of the public contract.
|
||||
minChoiceOptions = 2
|
||||
maxChoiceOptions = 10
|
||||
// maxChoiceOptionBytes bounds the text of one option. The public contract
|
||||
// allows more; a task option is a short value or phrase.
|
||||
maxChoiceOptionBytes = 500
|
||||
)
|
||||
|
||||
// assessmentChoiceOptionIDPattern is the shape of an opaque option ID. It
|
||||
// matches the public contract of AssessmentOptionId.
|
||||
var assessmentChoiceOptionIDPattern = regexp.MustCompile(`^[A-Za-z0-9_-]{1,32}$`)
|
||||
|
||||
// AssessmentChoiceOption is one option of a choice variant as the server
|
||||
// froze it. The ID is opaque: it is drawn from the variant seed after the
|
||||
// options were put in order, so it says nothing about the position or the
|
||||
// correctness of the option.
|
||||
type AssessmentChoiceOption struct {
|
||||
ID string
|
||||
Text string
|
||||
}
|
||||
|
||||
// choiceTask is the output of a choice algorithm. The algorithm names the
|
||||
// correct value and the wrong values that typical mistakes produce; the
|
||||
// shared code removes duplicates, selects, orders by the seed, and assigns
|
||||
// the opaque IDs.
|
||||
type choiceTask struct {
|
||||
data map[string]string
|
||||
correct string
|
||||
// mistakes are the results of known error models, for example an
|
||||
// off-by-one count or a wrong boundary, in the order of the model list.
|
||||
// A seed selects among them.
|
||||
mistakes []string
|
||||
// nearby are plausible values close to the correct one. They top up the
|
||||
// distractors only when the mistakes yield too few distinct values, for
|
||||
// example when two error models coincide on a small input.
|
||||
nearby []string
|
||||
}
|
||||
|
||||
// newChoiceOptions builds the options of one variant from the correct text and
|
||||
// the candidate distractors. A candidate that repeats the correct text or an
|
||||
// earlier candidate is dropped, so no two options show the same text. The
|
||||
// seed selects wrongCount of the mistakes when there are more, puts the
|
||||
// options in order, and draws their IDs. It returns the options in
|
||||
// presentation order and the index of the correct one.
|
||||
func newChoiceOptions(random *seededRandom, correct string, mistakes, nearby []string, wrongCount int) ([]AssessmentChoiceOption, int, error) {
|
||||
const location = "domain.newChoiceOptions"
|
||||
if wrongCount < minChoiceOptions-1 || wrongCount > maxChoiceOptions-1 {
|
||||
return nil, 0, invalidAssessmentFamily(location, "choice variant must offer 2 to 10 options")
|
||||
}
|
||||
seen := map[string]bool{correct: true}
|
||||
primary := uniqueChoiceTexts(mistakes, seen)
|
||||
secondary := uniqueChoiceTexts(nearby, seen)
|
||||
if len(primary) > wrongCount {
|
||||
// A partial Fisher-Yates shuffle: the seed picks which mistakes appear.
|
||||
for position := range wrongCount {
|
||||
swap := position + random.below(len(primary)-position)
|
||||
primary[position], primary[swap] = primary[swap], primary[position]
|
||||
}
|
||||
primary = primary[:wrongCount]
|
||||
}
|
||||
wrong := primary
|
||||
for _, text := range secondary {
|
||||
if len(wrong) >= wrongCount {
|
||||
break
|
||||
}
|
||||
wrong = append(wrong, text)
|
||||
}
|
||||
if len(wrong) < wrongCount {
|
||||
return nil, 0, invalidAssessmentFamily(location, "choice algorithm produced too few distinct distractors")
|
||||
}
|
||||
texts := append([]string{correct}, wrong...)
|
||||
for position := len(texts) - 1; position > 0; position-- {
|
||||
swap := random.below(position + 1)
|
||||
texts[position], texts[swap] = texts[swap], texts[position]
|
||||
}
|
||||
options := make([]AssessmentChoiceOption, len(texts))
|
||||
usedIDs := make(map[string]bool, len(texts))
|
||||
correctIndex := -1
|
||||
for position, text := range texts {
|
||||
if text == correct {
|
||||
correctIndex = position
|
||||
}
|
||||
id := nextChoiceOptionID(random, usedIDs)
|
||||
options[position] = AssessmentChoiceOption{ID: id, Text: text}
|
||||
}
|
||||
return options, correctIndex, nil
|
||||
}
|
||||
|
||||
// uniqueChoiceTexts returns the candidates that are not in seen, without
|
||||
// repeats, in their order, and adds them to seen. Empty and oversized texts
|
||||
// are dropped as well, since they cannot be options.
|
||||
func uniqueChoiceTexts(candidates []string, seen map[string]bool) []string {
|
||||
unique := make([]string, 0, len(candidates))
|
||||
for _, text := range candidates {
|
||||
if text == "" || len(text) > maxChoiceOptionBytes || seen[text] {
|
||||
continue
|
||||
}
|
||||
seen[text] = true
|
||||
unique = append(unique, text)
|
||||
}
|
||||
return unique
|
||||
}
|
||||
|
||||
// nextChoiceOptionID draws an ID that no earlier option of the variant has.
|
||||
func nextChoiceOptionID(random *seededRandom, used map[string]bool) string {
|
||||
for {
|
||||
id := fmt.Sprintf("o%08x", random.next()>>32)
|
||||
if !used[id] {
|
||||
used[id] = true
|
||||
return id
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func cloneChoiceOptions(options []AssessmentChoiceOption) []AssessmentChoiceOption {
|
||||
if len(options) == 0 {
|
||||
return nil
|
||||
}
|
||||
return append([]AssessmentChoiceOption(nil), options...)
|
||||
}
|
||||
|
||||
// choiceCorrectIndex reads the index of the correct option from the reference
|
||||
// answer of a choice variant.
|
||||
func choiceCorrectIndex(variant AssessmentVariant) (int, bool) {
|
||||
index, err := strconv.Atoi(variant.ReferenceAnswer)
|
||||
if err != nil || index < 0 || index >= len(variant.Options) || strconv.Itoa(index) != variant.ReferenceAnswer {
|
||||
return 0, false
|
||||
}
|
||||
return index, true
|
||||
}
|
||||
|
||||
// validFrozenChoiceVariant checks the options and the key of a frozen choice
|
||||
// variant: 2 to 10 options with distinct opaque IDs and distinct non-empty
|
||||
// texts, and a key that is the decimal index of one of them.
|
||||
func validFrozenChoiceVariant(variant AssessmentVariant) bool {
|
||||
if len(variant.Options) < minChoiceOptions || len(variant.Options) > maxChoiceOptions {
|
||||
return false
|
||||
}
|
||||
if _, ok := choiceCorrectIndex(variant); !ok {
|
||||
return false
|
||||
}
|
||||
ids := make(map[string]bool, len(variant.Options))
|
||||
texts := make(map[string]bool, len(variant.Options))
|
||||
for _, option := range variant.Options {
|
||||
if !assessmentChoiceOptionIDPattern.MatchString(option.ID) || ids[option.ID] ||
|
||||
option.Text == "" || len(option.Text) > maxChoiceOptionBytes || !utf8.ValidString(option.Text) || texts[option.Text] {
|
||||
return false
|
||||
}
|
||||
ids[option.ID], texts[option.Text] = true, true
|
||||
}
|
||||
return true
|
||||
}
|
||||
Reference in new issue
Block a user