feat(assessment): watermark the task text of real attempts
This commit is contained in:
1 parent
24b64d698a
commit
c975a0d506
57 files changed
+2429
-92
No files matched your search
@@ -0,0 +1,262 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
// wordingGroup lists interchangeable ways to write one phrase of a statement.
|
||||
// Every form means exactly the same, so choosing one never changes the task or
|
||||
// its reference answer. forms[0] is the canonical form: the attempt marker
|
||||
// replaces every form of a group in a statement by the form it picked for the
|
||||
// attempt, and the leak trace maps every form back to forms[0] before it
|
||||
// compares texts.
|
||||
//
|
||||
// The list is append-only. Reordering or removing a form, or inserting a group
|
||||
// before another, changes the choice of every attempt already served, and the
|
||||
// marks of those attempts could no longer be read back. A new group goes to the
|
||||
// end.
|
||||
type wordingGroup struct {
|
||||
forms []string
|
||||
// beforeNumber limits the group to places where the phrase is followed by a
|
||||
// space and a digit ("at most 5"). The forms read the same only there: "at
|
||||
// most" ends a sentence in "how many can it serve at most?", where "no more
|
||||
// than" would not.
|
||||
beforeNumber bool
|
||||
}
|
||||
|
||||
// assessmentWordingGroups are phrases of plain connective prose in the
|
||||
// statements: comparisons of counts, negations that can be contracted, and
|
||||
// spelling variants. They avoid the terms a statement defines and then reuses
|
||||
// ("processed", "dropped", "accepted"), because rewording one of several uses of
|
||||
// a defined term would confuse the task.
|
||||
//
|
||||
//nolint:gochecknoglobals // A fixed, append-only table that is read-only after initialization.
|
||||
var assessmentWordingGroups = []wordingGroup{
|
||||
{forms: []string{"at most", "no more than"}, beforeNumber: true},
|
||||
{forms: []string{"at least", "no fewer than"}, beforeNumber: true},
|
||||
{forms: []string{"fewer than", "less than"}, beforeNumber: true},
|
||||
{forms: []string{"is not", "isn't"}},
|
||||
{forms: []string{"are not", "aren't"}},
|
||||
{forms: []string{"does not", "doesn't"}},
|
||||
{forms: []string{"do not", "don't"}},
|
||||
{forms: []string{"cannot", "can't"}},
|
||||
{forms: []string{"non-decreasing", "nondecreasing"}},
|
||||
{forms: []string{"only when", "only if"}},
|
||||
{forms: []string{"half-open interval", "half open interval"}},
|
||||
{forms: []string{"one at a time", "one by one"}},
|
||||
{forms: []string{"Among all", "Of all"}},
|
||||
{forms: []string{"не более", "не больше"}, beforeNumber: true},
|
||||
{forms: []string{"не менее", "не меньше"}, beforeNumber: true},
|
||||
{forms: []string{"только если", "лишь если"}},
|
||||
{forms: []string{"в порядке неубывания", "в неубывающем порядке"}},
|
||||
{forms: []string{"Найдите", "Определите"}},
|
||||
}
|
||||
|
||||
// wordingOccurrence is one phrase found in a text.
|
||||
type wordingOccurrence struct {
|
||||
group int
|
||||
form int
|
||||
start int
|
||||
end int
|
||||
}
|
||||
|
||||
// findWordingOccurrences scans the text left to right and returns the
|
||||
// non-overlapping phrases of the table. Where several forms start at the same
|
||||
// place the longest wins.
|
||||
func findWordingOccurrences(text string) []wordingOccurrence {
|
||||
var found []wordingOccurrence
|
||||
for position := 0; position < len(text); {
|
||||
best := wordingOccurrence{group: -1}
|
||||
for groupIndex, group := range assessmentWordingGroups {
|
||||
for formIndex, form := range group.forms {
|
||||
if !wordingFormAt(text, position, form, group.beforeNumber) {
|
||||
continue
|
||||
}
|
||||
if best.group < 0 || len(form) > best.end-best.start {
|
||||
best = wordingOccurrence{group: groupIndex, form: formIndex, start: position, end: position + len(form)}
|
||||
}
|
||||
}
|
||||
}
|
||||
if best.group < 0 {
|
||||
_, size := utf8.DecodeRuneInString(text[position:])
|
||||
position += size
|
||||
continue
|
||||
}
|
||||
found = append(found, best)
|
||||
position = best.end
|
||||
}
|
||||
return found
|
||||
}
|
||||
|
||||
// wordingFormAt reports whether the form stands at the position as whole
|
||||
// words: not inside a longer word and, for a number group, before a number.
|
||||
func wordingFormAt(text string, position int, form string, beforeNumber bool) bool {
|
||||
if !strings.HasPrefix(text[position:], form) {
|
||||
return false
|
||||
}
|
||||
if previous, _ := utf8.DecodeLastRuneInString(text[:position]); position > 0 && wordRune(previous) {
|
||||
return false
|
||||
}
|
||||
end := position + len(form)
|
||||
if next, _ := utf8.DecodeRuneInString(text[end:]); end < len(text) && wordRune(next) {
|
||||
return false
|
||||
}
|
||||
if !beforeNumber {
|
||||
return true
|
||||
}
|
||||
rest := text[end:]
|
||||
if len(rest) < 2 || rest[0] != ' ' {
|
||||
return false
|
||||
}
|
||||
return rest[1] >= '0' && rest[1] <= '9'
|
||||
}
|
||||
|
||||
// wordRune reports whether the rune continues a word. The apostrophe and the
|
||||
// hyphen join words in the forms of the table, so a form never starts or ends
|
||||
// inside a hyphenated or contracted word.
|
||||
func wordRune(r rune) bool {
|
||||
return unicode.IsLetter(r) || unicode.IsDigit(r) || r == '\'' || r == '-'
|
||||
}
|
||||
|
||||
// rewriteWording replaces every phrase of the table by the form that choose
|
||||
// returns for its group.
|
||||
func rewriteWording(text string, choose func(group int) int) string {
|
||||
occurrences := findWordingOccurrences(text)
|
||||
if len(occurrences) == 0 {
|
||||
return text
|
||||
}
|
||||
var builder strings.Builder
|
||||
builder.Grow(len(text) + 16)
|
||||
copied := 0
|
||||
for _, occurrence := range occurrences {
|
||||
builder.WriteString(text[copied:occurrence.start])
|
||||
builder.WriteString(assessmentWordingGroups[occurrence.group].forms[choose(occurrence.group)])
|
||||
copied = occurrence.end
|
||||
}
|
||||
builder.WriteString(text[copied:])
|
||||
return builder.String()
|
||||
}
|
||||
|
||||
// canonicalWording writes every phrase of the table in its canonical form.
|
||||
func canonicalWording(text string) string {
|
||||
return rewriteWording(text, func(int) int { return 0 })
|
||||
}
|
||||
|
||||
// AssessmentTextPiecesBetweenPhrases cuts a text at every interchangeable
|
||||
// phrase and returns the pieces in between. The pieces read the same in every
|
||||
// copy of a statement, whichever form each attempt wrote the phrases in, so a
|
||||
// stored statement can be searched for a piece literally.
|
||||
func AssessmentTextPiecesBetweenPhrases(text string) []string {
|
||||
pieces := make([]string, 0, 8)
|
||||
copied := 0
|
||||
for _, occurrence := range findWordingOccurrences(text) {
|
||||
pieces = append(pieces, text[copied:occurrence.start])
|
||||
copied = occurrence.end
|
||||
}
|
||||
return append(pieces, text[copied:])
|
||||
}
|
||||
|
||||
// minAlignedPieceRunes is the shortest piece of a statement that the alignment
|
||||
// accepts as a landmark in a leak: a shorter one occurs anywhere.
|
||||
const minAlignedPieceRunes = 8
|
||||
|
||||
// LeakPhrase is a phrase of a statement as a leak writes it: the group and the
|
||||
// index of the form that the leak uses.
|
||||
type LeakPhrase struct {
|
||||
Group int
|
||||
Form int
|
||||
}
|
||||
|
||||
// LeakAlignment is how far a leaked text follows a stored statement: the
|
||||
// phrases it shows, in the forms it writes them, and the number of characters
|
||||
// of the statement that it repeats between its phrases.
|
||||
type LeakAlignment struct {
|
||||
Phrases []LeakPhrase
|
||||
// Runes is the length of the pieces of the statement that the leak repeats
|
||||
// in order. A leak of the whole statement repeats all of them, and the
|
||||
// statement of another variant of the same family repeats only the text of
|
||||
// the family up to the first number that differs.
|
||||
Runes int
|
||||
// Complete reports that the leak followed the statement to its end. Ended
|
||||
// reports that the leak stopped inside the statement, exactly where the
|
||||
// statement continues. A leak that has other words where the statement
|
||||
// continues does neither: it diverged from the statement, so the statement is
|
||||
// not its source, however long the text they share.
|
||||
Complete bool
|
||||
Ended bool
|
||||
}
|
||||
|
||||
// Follows reports whether the leak is a copy of the statement, whole or cut:
|
||||
// it either reached the end of the statement or ended inside it.
|
||||
func (alignment LeakAlignment) Follows() bool { return alignment.Complete || alignment.Ended }
|
||||
|
||||
// AssessmentLeakAlignment aligns a leaked text with a stored statement. It finds
|
||||
// a landmark of the statement in the leak (the first piece between phrases that
|
||||
// is long enough) and then follows the statement: after each piece, the leak
|
||||
// must continue with one form of the next phrase and then the next piece. It
|
||||
// stops at the first place where the leak does not follow the statement, so a
|
||||
// leak that holds only part of the statement, an edited one, or the statement of
|
||||
// another variant yields only the part that was aligned.
|
||||
func AssessmentLeakAlignment(statement, leak string) LeakAlignment {
|
||||
normalized := NormalizedAssessmentText(statement)
|
||||
occurrences := findWordingOccurrences(normalized)
|
||||
pieces := AssessmentTextPiecesBetweenPhrases(normalized)
|
||||
leak = NormalizedAssessmentText(leak)
|
||||
start, position := firstLandmark(pieces, leak)
|
||||
if start < 0 {
|
||||
return LeakAlignment{}
|
||||
}
|
||||
alignment := LeakAlignment{Runes: utf8.RuneCountInString(pieces[start]), Complete: true}
|
||||
for index := start; index < len(occurrences); index++ {
|
||||
group := assessmentWordingGroups[occurrences[index].group]
|
||||
next := pieces[index+1]
|
||||
matched := false
|
||||
for form, text := range group.forms {
|
||||
if strings.HasPrefix(leak[position:], text+next) {
|
||||
alignment.Phrases = append(alignment.Phrases, LeakPhrase{Group: occurrences[index].group, Form: form})
|
||||
alignment.Runes += utf8.RuneCountInString(next)
|
||||
position += len(text) + len(next)
|
||||
matched = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !matched {
|
||||
alignment.Complete = false
|
||||
alignment.Ended = leakEndsInside(leak[position:], group.forms, next)
|
||||
break
|
||||
}
|
||||
}
|
||||
return alignment
|
||||
}
|
||||
|
||||
// leakEndsInside reports whether the rest of the leak is the beginning of what
|
||||
// the statement has next (a form of the phrase, then the next piece), which is
|
||||
// how a leak that was cut ends. An empty rest, and a rest that holds only a cut
|
||||
// phrase or a cut piece, end the leak; any other word does not.
|
||||
func leakEndsInside(rest string, forms []string, next string) bool {
|
||||
if rest == "" {
|
||||
return true
|
||||
}
|
||||
for _, form := range forms {
|
||||
if strings.HasPrefix(form+next, rest) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// firstLandmark returns the index of the first piece that is long enough and
|
||||
// occurs in the leak, and the position in the leak just after it.
|
||||
func firstLandmark(pieces []string, leak string) (index, position int) {
|
||||
for index, piece := range pieces {
|
||||
if utf8.RuneCountInString(piece) < minAlignedPieceRunes {
|
||||
continue
|
||||
}
|
||||
if at := strings.Index(leak, piece); at >= 0 {
|
||||
return index, at + len(piece)
|
||||
}
|
||||
}
|
||||
return -1, 0
|
||||
}
|
||||
Reference in new issue
Block a user