fix(assessment): require the leak to agree with a statement before its landmark

This commit is contained in:
shira committed 2026-10-11 15:03:14 +03:00
1 parent 754c745fba
commit 3de226b16c
3 files changed
+95 -6

No files matched your search

+26 -6
View File
@@ -194,18 +194,20 @@ func (alignment LeakAlignment) Follows() bool { return alignment.Complete || ali
// 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.
// is long enough), checks that the text before it agrees with the statement, 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. A leak that differs before its landmark yields
// nothing.
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 {
if start < 0 || !leakAgreesBeforeLandmark(normalized, occurrences, pieces, start, leak, position) {
return LeakAlignment{}
}
alignment := LeakAlignment{Runes: utf8.RuneCountInString(pieces[start]), Complete: true}
@@ -231,6 +233,24 @@ func AssessmentLeakAlignment(statement, leak string) LeakAlignment {
return alignment
}
// leakAgreesBeforeLandmark checks the text before the landmark, which the
// forward alignment never reads. The leak may start inside the statement or have
// other words before it, so the statement and the leak, both written up to the end
// of the landmark piece with their phrases in canonical form, must end with one
// another. A leak that merely shares a later piece with the statement, such as
// the closing sentence of a whole family of tasks, differs from the statement
// before that piece, in the numbers of its variant: it is the statement of
// another variant and not a copy.
func leakAgreesBeforeLandmark(statement string, occurrences []wordingOccurrence, pieces []string, landmark int, leak string, position int) bool {
landmarkStart := 0
if landmark > 0 {
landmarkStart = occurrences[landmark-1].end
}
statementHead := canonicalWording(statement[:landmarkStart+len(pieces[landmark])])
leakHead := canonicalWording(leak[:position])
return strings.HasSuffix(statementHead, leakHead) || strings.HasSuffix(leakHead, statementHead)
}
// 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