All checks were successful
ci/woodpecker/tag/release Pipeline was successful
82 lines
3.2 KiB
Go
82 lines
3.2 KiB
Go
package proposals
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"gitea.maximumdirect.net/eric/audita/v4/internal/core/schema"
|
|
)
|
|
|
|
// ProposalSkipReason is a stable, report-friendly reason code for
|
|
// unapplied/unsafe proposals.
|
|
type ProposalSkipReason string
|
|
|
|
const (
|
|
SkipReasonMissingSegment ProposalSkipReason = "missing_segment"
|
|
SkipReasonMissingOriginalText ProposalSkipReason = "missing_original_text"
|
|
SkipReasonAmbiguousOriginal ProposalSkipReason = "ambiguous_original_text"
|
|
SkipReasonNoEffect ProposalSkipReason = "no_effect"
|
|
SkipReasonEmptyResultingText ProposalSkipReason = "empty_resulting_segment"
|
|
SkipReasonInvalidProposal ProposalSkipReason = "invalid_proposal"
|
|
SkipReasonProtectedTerm ProposalSkipReason = "protected_glossary_term"
|
|
)
|
|
|
|
// SegmentPreviewResult is a deterministic preview of applying one proposal to
|
|
// one transcript segment.
|
|
type SegmentPreviewResult struct {
|
|
Applicable bool `json:"applicable"`
|
|
CorrectedSegmentText string `json:"corrected_segment_text,omitempty"`
|
|
ReplacementCount int `json:"replacement_count"`
|
|
SkipReason ProposalSkipReason `json:"skip_reason,omitempty"`
|
|
}
|
|
|
|
// PreviewProposalForSegment previews applying a proposal to a single segment
|
|
// under the given replacement policy without mutating the input segment.
|
|
func PreviewProposalForSegment(segment *schema.Segment, proposal CorrectionProposal, policy ReplacementPolicy) SegmentPreviewResult {
|
|
if segment == nil {
|
|
return SegmentPreviewResult{SkipReason: SkipReasonMissingSegment}
|
|
}
|
|
|
|
if err := proposal.Validate(); err != nil {
|
|
return SegmentPreviewResult{SkipReason: SkipReasonInvalidProposal}
|
|
}
|
|
|
|
if !policy.IsValid() {
|
|
return SegmentPreviewResult{SkipReason: SkipReasonInvalidProposal}
|
|
}
|
|
|
|
if proposal.TargetSegmentID != segment.ID {
|
|
return SegmentPreviewResult{SkipReason: SkipReasonMissingSegment}
|
|
}
|
|
|
|
first := strings.Index(segment.Text, proposal.OriginalText)
|
|
if first < 0 {
|
|
return SegmentPreviewResult{SkipReason: SkipReasonMissingOriginalText}
|
|
}
|
|
if proposal.OriginalText == proposal.CorrectedText {
|
|
return SegmentPreviewResult{SkipReason: SkipReasonNoEffect}
|
|
}
|
|
|
|
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)
|
|
} else {
|
|
count = strings.Count(segment.Text, proposal.OriginalText)
|
|
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: 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
|
|
}
|