44 lines
1.3 KiB
Go
44 lines
1.3 KiB
Go
package aiprompts
|
|
|
|
import (
|
|
"regexp"
|
|
"strings"
|
|
)
|
|
|
|
// A1's historical content sometimes repeats the exact same H2/paragraph pair.
|
|
// Remove only identical pairs; different benefits, paragraph inclusion rules,
|
|
// technical specifications and all non-description instructions stay intact.
|
|
var a1HeadingParagraph = regexp.MustCompile(`(?is)<\s*h[1-4]\s*>.*?<\s*/\s*h[1-4]\s*>\s*<\s*p\s*>.*?<\s*/\s*p\s*>`)
|
|
var a1Whitespace = regexp.MustCompile(`\s+`)
|
|
|
|
func DedupeA1DescriptionBlocks(source string) (string, int) {
|
|
blocks := a1HeadingParagraph.FindAllStringIndex(source, -1)
|
|
if len(blocks) < 2 {
|
|
return source, 0
|
|
}
|
|
seen := make(map[string]bool, len(blocks))
|
|
var out strings.Builder
|
|
out.Grow(len(source))
|
|
cursor, removed := 0, 0
|
|
for _, b := range blocks {
|
|
part := source[b[0]:b[1]]
|
|
key := strings.ToLower(a1Whitespace.ReplaceAllString(strings.TrimSpace(part), " "))
|
|
// Keep non-description content exactly as it was. Removing an exact
|
|
// repeated pair must not also delete the next section or specs.
|
|
out.WriteString(source[cursor:b[0]])
|
|
if seen[key] {
|
|
removed++
|
|
} else {
|
|
seen[key] = true
|
|
out.WriteString(part)
|
|
}
|
|
cursor = b[1]
|
|
}
|
|
out.WriteString(source[cursor:])
|
|
if removed == 0 {
|
|
return source, 0
|
|
}
|
|
// Do not rewrite punctuation/case/brand-specific rules.
|
|
return out.String(), removed
|
|
}
|