feat(assessment): grade choice answers with distractors from error models

This commit is contained in:
shira committed 2026-10-11 03:12:24 +03:00
1 parent efa41d5967
commit f1147b1885
22 files changed
+1346 -47

No files matched your search

+93 -22
View File
@@ -139,7 +139,37 @@ type assessmentAttemptVariantDocument struct {
Statement string `json:"Statement"`
ReferenceAnswer string `json:"ReferenceAnswer"`
AnswerFormat string `json:"AnswerFormat"`
Rubric AssessmentRubric `json:"Rubric"`
// Options is present only for a choice variant, so the documents of the
// other formats keep the layout they were stored with.
Options []assessmentAttemptChoiceOptionDocument `json:"Options,omitempty"`
Rubric AssessmentRubric `json:"Rubric"`
}
type assessmentAttemptChoiceOptionDocument struct {
ID string `json:"ID"`
Text string `json:"Text"`
}
func assessmentAttemptChoiceOptionDocuments(options []AssessmentChoiceOption) []assessmentAttemptChoiceOptionDocument {
if len(options) == 0 {
return nil
}
documents := make([]assessmentAttemptChoiceOptionDocument, len(options))
for index, option := range options {
documents[index] = assessmentAttemptChoiceOptionDocument(option)
}
return documents
}
func assessmentAttemptChoiceOptionsFromDocuments(documents []assessmentAttemptChoiceOptionDocument) []AssessmentChoiceOption {
if len(documents) == 0 {
return nil
}
options := make([]AssessmentChoiceOption, len(documents))
for index, document := range documents {
options[index] = AssessmentChoiceOption(document)
}
return options
}
func assessmentAttemptVariantDocumentFromVariant(variant AssessmentVariant) assessmentAttemptVariantDocument {
@@ -152,6 +182,7 @@ func assessmentAttemptVariantDocumentFromVariant(variant AssessmentVariant) asse
document.Statement = variant.Statement
document.ReferenceAnswer = variant.ReferenceAnswer
document.AnswerFormat = variant.AnswerFormat
document.Options = assessmentAttemptChoiceOptionDocuments(variant.Options)
document.Rubric = variant.Rubric
return document
}
@@ -166,6 +197,7 @@ func (document assessmentAttemptVariantDocument) variant() AssessmentVariant {
variant.Statement = document.Statement
variant.ReferenceAnswer = document.ReferenceAnswer
variant.AnswerFormat = document.AnswerFormat
variant.Options = assessmentAttemptChoiceOptionsFromDocuments(document.Options)
variant.Rubric = document.Rubric
return variant
}
@@ -243,13 +275,18 @@ type AssessmentAttemptPublicItem struct {
// AssessmentAttemptPublicItemContent is an allowlisted candidate-facing
// projection of the formats currently produced by the reviewed bank.
type AssessmentAttemptPublicItemContent struct {
Format string `json:"format"`
Unit *string `json:"unit,omitempty"`
Format string `json:"format"`
Unit *string `json:"unit,omitempty"`
// Elements are the elements of an order item or the options of a choice
// item, in the order of this instance.
Elements []AssessmentAttemptPublicOption `json:"elements,omitempty"`
// Multiple says whether a choice item accepts several options. Generated
// choice variants have one correct option and accept one selection.
Multiple bool `json:"multiple,omitempty"`
}
// AssessmentAttemptPublicOption is a generated task element, not a family or
// variant identifier.
// AssessmentAttemptPublicOption is a generated task element or choice option:
// an opaque ID and a text, not a family or variant identifier.
type AssessmentAttemptPublicOption struct {
ID string `json:"id"`
Text string `json:"text"`
@@ -636,8 +673,7 @@ func buildAssessmentAttemptItems(input []AssessmentAttemptItemInput) ([]Assessme
}
func validFrozenVariant(variant AssessmentVariant) bool {
if variant.FamilyID.IsZero() || variant.FamilyVersion < 1 ||
(variant.AnswerFormat != AnswerFormatInteger && variant.AnswerFormat != AnswerFormatIntegerSequence) ||
if variant.FamilyID.IsZero() || variant.FamilyVersion < 1 || !validFrozenVariantFormat(variant) ||
variant.ReferenceAnswer == "" || !utf8.ValidString(variant.ReferenceAnswer) ||
!utf8.ValidString(variant.Statement) || len(variant.Statement) == 0 || len(variant.Statement) > maxStatementBytes ||
!validStoredFamilyKey(variant.FamilyKey) || !validStoredVariantRubric(variant.Rubric) {
@@ -647,6 +683,20 @@ func validFrozenVariant(variant AssessmentVariant) bool {
return err == nil && len(encoded) <= maxAssessmentAttemptVariantBytes
}
// validFrozenVariantFormat accepts the answer formats that the bank
// produces. Only a choice variant carries options, and its key must name one
// of them.
func validFrozenVariantFormat(variant AssessmentVariant) bool {
switch variant.AnswerFormat {
case AnswerFormatInteger, AnswerFormatIntegerSequence:
return len(variant.Options) == 0
case AnswerFormatChoice:
return validFrozenChoiceVariant(variant)
default:
return false
}
}
const maxAssessmentAttemptVariantBytes = 1 << 20
func validStoredFamilyKey(value AssessmentFamilyKey) bool {
@@ -672,6 +722,7 @@ func validStoredVariantRubric(rubric AssessmentRubric) bool {
func cloneAssessmentVariant(variant AssessmentVariant) AssessmentVariant {
variant.Parameters = maps.Clone(variant.Parameters)
variant.Options = cloneChoiceOptions(variant.Options)
variant.Rubric = variant.Rubric.clone()
return variant
}
@@ -1064,6 +1115,17 @@ func publicItemContent(variant AssessmentVariant) (AssessmentAttemptPublicItemCo
switch variant.AnswerFormat {
case AnswerFormatInteger:
return AssessmentAttemptPublicItemContent{Format: "number"}, true
case AnswerFormatChoice:
if !validFrozenChoiceVariant(variant) {
return AssessmentAttemptPublicItemContent{}, false
}
// Only the opaque IDs and the texts leave the server: the index of
// the correct option and the way the distractors were made do not.
options := make([]AssessmentAttemptPublicOption, 0, len(variant.Options))
for _, option := range variant.Options {
options = append(options, AssessmentAttemptPublicOption(option))
}
return AssessmentAttemptPublicItemContent{Format: AnswerFormatChoice, Elements: options}, true
case AnswerFormatIntegerSequence:
taskCount, ok := variant.Parameters["tasks"]
if !ok || taskCount < 2 || taskCount > 20 {
@@ -1205,27 +1267,36 @@ func validAssessmentAttemptPublicState(snapshot AssessmentAttemptPublicSnapshot)
func validAssessmentAttemptPublicItemContent(content AssessmentAttemptPublicItemContent) bool {
switch content.Format {
case "number":
return len(content.Elements) == 0 && (content.Unit == nil || *content.Unit != "" && len(*content.Unit) <= 32)
return len(content.Elements) == 0 && !content.Multiple && (content.Unit == nil || *content.Unit != "" && len(*content.Unit) <= 32)
case AnswerFormatChoice:
return content.Unit == nil && validAssessmentAttemptPublicElements(content.Elements, minChoiceOptions, maxChoiceOptions, assessmentChoiceOptionIDPattern)
case "order":
if content.Unit != nil || len(content.Elements) < 2 || len(content.Elements) > 20 {
return false
}
seen := make(map[string]struct{}, len(content.Elements))
for _, element := range content.Elements {
if !assessmentPublicTaskElementIDPattern.MatchString(element.ID) || element.Text == "" || len(element.Text) > 2000 || !utf8.ValidString(element.Text) {
return false
}
if _, exists := seen[element.ID]; exists {
return false
}
seen[element.ID] = struct{}{}
}
return true
return content.Unit == nil && validAssessmentAttemptPublicElements(content.Elements, 2, 20, assessmentPublicTaskElementIDPattern)
default:
return false
}
}
// validAssessmentAttemptPublicElements checks the options of a choice item or
// the elements of an order item: a bounded count, IDs of the given shape
// without repeats, and non-empty bounded text.
func validAssessmentAttemptPublicElements(elements []AssessmentAttemptPublicOption, minCount, maxCount int, idPattern *regexp.Regexp) bool {
if len(elements) < minCount || len(elements) > maxCount {
return false
}
seen := make(map[string]struct{}, len(elements))
for _, element := range elements {
if !idPattern.MatchString(element.ID) || element.Text == "" || len(element.Text) > 2000 || !utf8.ValidString(element.Text) {
return false
}
if _, exists := seen[element.ID]; exists {
return false
}
seen[element.ID] = struct{}{}
}
return true
}
func (attempt AssessmentAttempt) String() string { return redactedAssessmentAttempt }
func (attempt AssessmentAttempt) GoString() string { return redactedAssessmentAttempt }
func (attempt AssessmentAttempt) Format(state fmt.State, _ rune) {