Share edit location and check protected names in full segments
This commit is contained in:
@@ -170,6 +170,35 @@ func TestApplyProposalsRequireUniqueAmbiguousSkip(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPreviewAndApplyHandleOverlappingOccurrences(t *testing.T) {
|
||||
transcript := &schema.Transcript{Segments: []schema.Segment{{ID: 71, Text: "banana"}}}
|
||||
edit := EnrichedCorrectionProposal{CorrectionProposal: CorrectionProposal{
|
||||
TargetSegmentID: 71, OriginalText: "ana", CorrectedText: "X",
|
||||
}}
|
||||
for _, tc := range []struct {
|
||||
policy ReplacementPolicy
|
||||
text string
|
||||
skip ProposalSkipReason
|
||||
}{
|
||||
{ReplacementPolicyRequireUnique, "banana", SkipReasonAmbiguousOriginal},
|
||||
{ReplacementPolicyReplaceAll, "bXna", ""},
|
||||
} {
|
||||
preview := PreviewProposalForSegment(&transcript.Segments[0], edit.CorrectionProposal, tc.policy)
|
||||
result := ApplyProposals(transcript, []EnrichedCorrectionProposal{edit}, tc.policy, nil)
|
||||
if result.Transcript.Segments[0].Text != tc.text {
|
||||
t.Fatalf("%s produced %q", tc.policy, result.Transcript.Segments[0].Text)
|
||||
}
|
||||
if tc.skip != "" {
|
||||
if preview.HasResult() || preview.SkipReason != tc.skip || len(result.Skipped) != 1 || result.Skipped[0].SkipReason != tc.skip {
|
||||
t.Fatalf("%s preview %+v and apply %+v disagree with expected skip", tc.policy, preview, result)
|
||||
}
|
||||
} else if !preview.Applicable || preview.CorrectedSegmentText != tc.text ||
|
||||
len(result.Applied) != 1 || result.Applied[0].ReplacementCount != 1 {
|
||||
t.Fatalf("%s preview %+v and apply %+v disagree with expected edit", tc.policy, preview, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyProposalsReplaceAllMultipleReplacements(t *testing.T) {
|
||||
transcript := &schema.Transcript{Segments: []schema.Segment{{ID: 1, Text: "uh uh uh"}}}
|
||||
proposals := []EnrichedCorrectionProposal{
|
||||
|
||||
@@ -48,42 +48,34 @@ func PreviewProposalForSegment(segment *schema.Segment, proposal CorrectionPropo
|
||||
return SegmentPreviewResult{SkipReason: SkipReasonMissingSegment}
|
||||
}
|
||||
|
||||
matchCount := strings.Count(segment.Text, proposal.OriginalText)
|
||||
if matchCount == 0 {
|
||||
first := strings.Index(segment.Text, proposal.OriginalText)
|
||||
if first < 0 {
|
||||
return SegmentPreviewResult{SkipReason: SkipReasonMissingOriginalText}
|
||||
}
|
||||
|
||||
if proposal.OriginalText == proposal.CorrectedText {
|
||||
return SegmentPreviewResult{SkipReason: SkipReasonNoEffect}
|
||||
}
|
||||
|
||||
switch policy {
|
||||
case ReplacementPolicyRequireUnique:
|
||||
if matchCount != 1 {
|
||||
count := 1
|
||||
var corrected string
|
||||
if policy == ReplacementPolicyRequireUnique {
|
||||
// Search after the first starting offset, including overlapping matches.
|
||||
if strings.Contains(segment.Text[first+1:], proposal.OriginalText) {
|
||||
return SegmentPreviewResult{SkipReason: SkipReasonAmbiguousOriginal}
|
||||
}
|
||||
|
||||
corrected := strings.Replace(segment.Text, proposal.OriginalText, proposal.CorrectedText, 1)
|
||||
if strings.TrimSpace(corrected) == "" {
|
||||
return SegmentPreviewResult{SkipReason: SkipReasonEmptyResultingText, CorrectedSegmentText: corrected}
|
||||
}
|
||||
return SegmentPreviewResult{
|
||||
Applicable: true,
|
||||
CorrectedSegmentText: corrected,
|
||||
ReplacementCount: 1,
|
||||
}
|
||||
|
||||
case ReplacementPolicyReplaceAll:
|
||||
corrected := strings.ReplaceAll(segment.Text, proposal.OriginalText, proposal.CorrectedText)
|
||||
if strings.TrimSpace(corrected) == "" {
|
||||
return SegmentPreviewResult{SkipReason: SkipReasonEmptyResultingText, CorrectedSegmentText: corrected}
|
||||
}
|
||||
return SegmentPreviewResult{
|
||||
Applicable: true,
|
||||
CorrectedSegmentText: corrected,
|
||||
ReplacementCount: matchCount,
|
||||
}
|
||||
corrected = strings.Replace(segment.Text, proposal.OriginalText, proposal.CorrectedText, 1)
|
||||
} else {
|
||||
count = strings.Count(segment.Text, proposal.OriginalText)
|
||||
corrected = strings.ReplaceAll(segment.Text, proposal.OriginalText, proposal.CorrectedText)
|
||||
}
|
||||
|
||||
return SegmentPreviewResult{SkipReason: SkipReasonInvalidProposal}
|
||||
if strings.TrimSpace(corrected) == "" {
|
||||
return SegmentPreviewResult{SkipReason: SkipReasonEmptyResultingText, CorrectedSegmentText: corrected}
|
||||
}
|
||||
return SegmentPreviewResult{Applicable: true, CorrectedSegmentText: corrected, ReplacementCount: count}
|
||||
}
|
||||
|
||||
// HasResult indicates that the replacement was located and yielded a preview,
|
||||
// including when a separate blank-result rule makes the edit inapplicable.
|
||||
func (r SegmentPreviewResult) HasResult() bool {
|
||||
return r.Applicable || r.SkipReason == SkipReasonEmptyResultingText
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@ package proposals
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"gitea.maximumdirect.net/eric/audita/v3/internal/core/schema"
|
||||
)
|
||||
|
||||
// CorrectionProposal is the base proposal emitted by correction modules.
|
||||
@@ -39,3 +41,12 @@ func (p CorrectionProposal) Validate() error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// SegmentIDAtReference maps a one-based model reference in the supplied
|
||||
// section to the transcript's segment ID. Zero represents an invalid reference.
|
||||
func SegmentIDAtReference(section *schema.Transcript, reference int) int {
|
||||
if section == nil || reference <= 0 || reference > len(section.Segments) {
|
||||
return 0
|
||||
}
|
||||
return section.Segments[reference-1].ID
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user