Share section lookups and replacement previews across validators
This commit is contained in:
@@ -3,11 +3,9 @@ package validators
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"math"
|
||||
"strings"
|
||||
|
||||
"gitea.maximumdirect.net/eric/audita/v3/internal/core/modulecatalog"
|
||||
"gitea.maximumdirect.net/eric/audita/v3/internal/core/schema"
|
||||
"gitea.maximumdirect.net/eric/audita/v3/internal/framework/proposals"
|
||||
)
|
||||
|
||||
@@ -60,21 +58,16 @@ type OriginalTextPresenceValidator struct{}
|
||||
func (v OriginalTextPresenceValidator) Name() string { return "original_text_presence" }
|
||||
|
||||
func (v OriginalTextPresenceValidator) Validate(_ context.Context, req Request) (Result, error) {
|
||||
byID := make(map[int]string)
|
||||
if req.WorkingTranscript != nil {
|
||||
for _, seg := range req.WorkingTranscript.Segments {
|
||||
byID[seg.ID] = seg.Text
|
||||
}
|
||||
}
|
||||
section := req.sectionLookup()
|
||||
|
||||
decisions := make([]Decision, 0, len(req.CandidateProposal))
|
||||
for _, c := range req.CandidateProposal {
|
||||
text, ok := byID[c.TargetSegmentID]
|
||||
position, ok := section.Position(c.TargetSegmentID)
|
||||
if !ok {
|
||||
decisions = append(decisions, rejection(c.ProposalIndex, ReasonMissingTargetSegment, "target segment was not found"))
|
||||
continue
|
||||
}
|
||||
if !strings.Contains(text, c.OriginalText) {
|
||||
if !strings.Contains(section.segments[position].Text, c.OriginalText) {
|
||||
decisions = append(decisions, rejection(c.ProposalIndex, ReasonMissingOriginalText, "original_text was not found in current segment text"))
|
||||
continue
|
||||
}
|
||||
@@ -91,21 +84,11 @@ type NonEmptyResultingSegmentValidator struct{}
|
||||
func (v NonEmptyResultingSegmentValidator) Name() string { return "non_empty_resulting_segment" }
|
||||
|
||||
func (v NonEmptyResultingSegmentValidator) Validate(_ context.Context, req Request) (Result, error) {
|
||||
segmentsByID := make(map[int]schema.Segment)
|
||||
if req.WorkingTranscript != nil {
|
||||
for _, seg := range req.WorkingTranscript.Segments {
|
||||
segmentsByID[seg.ID] = seg
|
||||
}
|
||||
}
|
||||
req.Section = req.sectionLookup()
|
||||
|
||||
decisions := make([]Decision, 0, len(req.CandidateProposal))
|
||||
for _, c := range req.CandidateProposal {
|
||||
segment, ok := segmentsByID[c.TargetSegmentID]
|
||||
if !ok {
|
||||
decisions = append(decisions, approval(c.ProposalIndex))
|
||||
continue
|
||||
}
|
||||
preview := previewForEvidence(req, &segment, c.CorrectionProposal)
|
||||
preview := req.Preview(c)
|
||||
if preview.SkipReason == proposals.SkipReasonEmptyResultingText {
|
||||
decisions = append(decisions, rejection(c.ProposalIndex, ReasonEmptyResultingText, "proposal would leave the segment empty"))
|
||||
continue
|
||||
@@ -149,13 +132,11 @@ func ValidateProtectedTerms(req Request) (Result, error) {
|
||||
for _, c := range req.CandidateProposal {
|
||||
reason := vocab.violationReason(c.OriginalText, c.CorrectedText)
|
||||
repetitionCleanup := reason != "" && req.ModuleKey == modulecatalog.KeySpokenWord
|
||||
if vocab.pattern != nil && (glossaryStage || repetitionCleanup) && req.WorkingTranscript != nil {
|
||||
for i := range req.WorkingTranscript.Segments {
|
||||
segment := &req.WorkingTranscript.Segments[i]
|
||||
if segment.ID != c.TargetSegmentID {
|
||||
continue
|
||||
}
|
||||
preview := previewForEvidence(req, segment, c.CorrectionProposal)
|
||||
if vocab.pattern != nil && (glossaryStage || repetitionCleanup) {
|
||||
req.Section = req.sectionLookup()
|
||||
if position, ok := req.Section.Position(c.TargetSegmentID); ok {
|
||||
segment := &req.Section.segments[position]
|
||||
preview := req.Preview(c)
|
||||
if preview.Applicable || (req.IndependentEvidence && preview.SkipReason == proposals.SkipReasonEmptyResultingText) {
|
||||
if glossaryStage {
|
||||
// A correction span may sit inside a longer exempt name.
|
||||
@@ -164,7 +145,6 @@ func ValidateProtectedTerms(req Request) (Result, error) {
|
||||
reason = ""
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
if reason != "" {
|
||||
@@ -178,10 +158,3 @@ func ValidateProtectedTerms(req Request) (Result, error) {
|
||||
}
|
||||
return Result{ValidatorName: "protected_terms", Decisions: decisions}, nil
|
||||
}
|
||||
|
||||
func previewForEvidence(req Request, segment *schema.Segment, correction proposals.CorrectionProposal) proposals.SegmentPreviewResult {
|
||||
if req.IndependentEvidence && (math.IsNaN(correction.Confidence) || correction.Confidence < 0 || correction.Confidence > 1) {
|
||||
correction.Confidence = 0.5
|
||||
}
|
||||
return proposals.PreviewProposalForSegment(segment, correction, req.ReplacementPolicy)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user