Share edit location and check protected names in full segments
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user