fix
This commit is contained in:
1 parent
7537a41955
commit
ff6c2df618
14 files changed
+767
-349
No files matched your search
@@ -1,6 +1,8 @@
|
||||
package aiprompts
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
@@ -41,3 +43,201 @@ func DedupeA1DescriptionBlocks(source string) (string, int) {
|
||||
// Do not rewrite punctuation/case/brand-specific rules.
|
||||
return out.String(), removed
|
||||
}
|
||||
|
||||
// DedupeA1DescriptionTemplate removes repeated adjacent H2/P instruction pairs
|
||||
// from the structured formula as well as from legacy HTML prompts. Other JSON
|
||||
// fields, including metaTitle and metaDescription, are preserved unchanged.
|
||||
func DedupeA1DescriptionTemplate(raw string) (string, int, error) {
|
||||
if strings.TrimSpace(raw) == "" || strings.TrimSpace(raw) == "null" {
|
||||
return raw, 0, nil
|
||||
}
|
||||
var obj map[string]json.RawMessage
|
||||
if err := json.Unmarshal([]byte(raw), &obj); err != nil {
|
||||
return raw, 0, err
|
||||
}
|
||||
if len(obj) == 0 {
|
||||
return raw, 0, nil
|
||||
}
|
||||
var sections []map[string]json.RawMessage
|
||||
if err := json.Unmarshal(obj["sections"], §ions); err != nil {
|
||||
return raw, 0, nil
|
||||
}
|
||||
seen := map[string]bool{}
|
||||
result := make([]map[string]json.RawMessage, 0, len(sections))
|
||||
removed := 0
|
||||
for i := 0; i < len(sections); i++ {
|
||||
var typ, instruction string
|
||||
_ = json.Unmarshal(sections[i]["type"], &typ)
|
||||
_ = json.Unmarshal(sections[i]["instructions"], &instruction)
|
||||
if strings.EqualFold(typ, "h2") && i+1 < len(sections) {
|
||||
var nextType, nextText string
|
||||
_ = json.Unmarshal(sections[i+1]["type"], &nextType)
|
||||
_ = json.Unmarshal(sections[i+1]["instructions"], &nextText)
|
||||
if strings.EqualFold(nextType, "p") {
|
||||
key := strings.ToLower(a1Whitespace.ReplaceAllString(strings.TrimSpace(instruction+"\x00"+nextText), " "))
|
||||
if seen[key] {
|
||||
removed++
|
||||
i++
|
||||
continue
|
||||
}
|
||||
seen[key] = true
|
||||
}
|
||||
}
|
||||
result = append(result, sections[i])
|
||||
}
|
||||
if removed == 0 {
|
||||
return raw, 0, nil
|
||||
}
|
||||
encoded, err := json.Marshal(result)
|
||||
if err != nil {
|
||||
return raw, 0, err
|
||||
}
|
||||
obj["sections"] = encoded
|
||||
bytes, err := json.Marshal(obj)
|
||||
if err != nil {
|
||||
return raw, 0, err
|
||||
}
|
||||
return string(bytes), removed, nil
|
||||
}
|
||||
|
||||
// NormalizeA1RepeatedBenefits retains the number/order of A1 HTML sections,
|
||||
// but makes duplicate benefit instructions distinct instead of deleting copy.
|
||||
// It preserves title/meta instructions, original product-name inclusion intent,
|
||||
// and the technical-specifications block.
|
||||
var a1HeadingText = regexp.MustCompile(`(?is)(<\s*h[1-4]\s*>\s*\{?)(.*?)(\}?\s*<\s*/\s*h[1-4]\s*>)`)
|
||||
var a1ParagraphText = regexp.MustCompile(`(?is)(<\s*p\s*>\s*\{?)(.*?)(\}?\s*<\s*/\s*p\s*>)`)
|
||||
|
||||
func a1NormalizeInstruction(value string) string {
|
||||
return strings.ToLower(a1Whitespace.ReplaceAllString(strings.TrimSpace(strings.Trim(value, "{} \t\n")), " "))
|
||||
}
|
||||
|
||||
func a1UniqBenefit(heading, paragraph string, position int, seenHeadings, seenParagraphs map[string]bool) (string, string, bool) {
|
||||
headKey := a1NormalizeInstruction(heading)
|
||||
paragraphKey := a1NormalizeInstruction(paragraph)
|
||||
duplicate := (headKey != "" && seenHeadings[headKey]) || (paragraphKey != "" && seenParagraphs[paragraphKey])
|
||||
if headKey != "" {
|
||||
seenHeadings[headKey] = true
|
||||
}
|
||||
if paragraphKey != "" {
|
||||
seenParagraphs[paragraphKey] = true
|
||||
}
|
||||
if !duplicate {
|
||||
return heading, paragraph, false
|
||||
}
|
||||
newHeading := fmt.Sprintf("Izpostavi %d. drugačno in preverjeno prednost izdelka. Ne ponavljaj prejšnjih prednosti ali celotnega imena izdelka.", position)
|
||||
lower := strings.ToLower(paragraph)
|
||||
includeName := strings.Contains(lower, "vključi") || strings.Contains(lower, "vkljuci") || strings.Contains(lower, "vsebuje novo ime") && !strings.Contains(lower, "ne vsebuje")
|
||||
newParagraph := fmt.Sprintf("Napiši približno 100 besed o %d. samostojni, z dejanskimi podatki podprti prednosti. Ne ponavljaj informacij iz prejšnjih odstavkov.", position)
|
||||
if includeName {
|
||||
newParagraph += " Novo ime izdelka vključi naravno natanko enkrat."
|
||||
} else {
|
||||
newParagraph += " Ne uporabi celotnega novega imena izdelka."
|
||||
}
|
||||
newParagraph += " Če prednosti ni mogoče potrditi z vhodnimi podatki, je ne izmišljaj."
|
||||
return newHeading, newParagraph, true
|
||||
}
|
||||
|
||||
func a1ReplaceTagContent(source string, expression *regexp.Regexp, newInstruction string) string {
|
||||
matches := expression.FindStringSubmatchIndex(source)
|
||||
if len(matches) < 8 {
|
||||
return source
|
||||
}
|
||||
return source[:matches[4]] + newInstruction + source[matches[5]:]
|
||||
}
|
||||
|
||||
func NormalizeA1RepeatedBenefits(source string) (string, int) {
|
||||
blocks := a1HeadingParagraph.FindAllStringIndex(source, -1)
|
||||
if len(blocks) < 2 {
|
||||
return source, 0
|
||||
}
|
||||
heads := map[string]bool{}
|
||||
paragraphs := map[string]bool{}
|
||||
var out strings.Builder
|
||||
out.Grow(len(source))
|
||||
cursor, rewritten := 0, 0
|
||||
for i, idx := range blocks {
|
||||
part := source[idx[0]:idx[1]]
|
||||
headingMatch := a1HeadingText.FindStringSubmatch(part)
|
||||
paragraphMatch := a1ParagraphText.FindStringSubmatch(part)
|
||||
out.WriteString(source[cursor:idx[0]])
|
||||
if len(headingMatch) == 4 && len(paragraphMatch) == 4 {
|
||||
heading, paragraph, changed := a1UniqBenefit(headingMatch[2], paragraphMatch[2], i+1, heads, paragraphs)
|
||||
if changed {
|
||||
part = a1ReplaceTagContent(part, a1ParagraphText, paragraph)
|
||||
part = a1ReplaceTagContent(part, a1HeadingText, heading)
|
||||
rewritten++
|
||||
}
|
||||
}
|
||||
out.WriteString(part)
|
||||
cursor = idx[1]
|
||||
}
|
||||
out.WriteString(source[cursor:])
|
||||
if rewritten == 0 {
|
||||
return source, 0
|
||||
}
|
||||
return out.String(), rewritten
|
||||
}
|
||||
|
||||
// NormalizeA1DescriptionTemplate applies the same non-repeating benefit rules
|
||||
// to the structured A1 description editor sections. No sections are deleted.
|
||||
func NormalizeA1DescriptionTemplate(raw string) (string, int, error) {
|
||||
if strings.TrimSpace(raw) == "" || strings.TrimSpace(raw) == "null" {
|
||||
return raw, 0, nil
|
||||
}
|
||||
var obj map[string]json.RawMessage
|
||||
if err := json.Unmarshal([]byte(raw), &obj); err != nil {
|
||||
return raw, 0, err
|
||||
}
|
||||
if len(obj) == 0 {
|
||||
return raw, 0, nil
|
||||
}
|
||||
var sections []map[string]json.RawMessage
|
||||
if err := json.Unmarshal(obj["sections"], §ions); err != nil {
|
||||
return raw, 0, nil
|
||||
}
|
||||
heads := map[string]bool{}
|
||||
paragraphs := map[string]bool{}
|
||||
normalized := 0
|
||||
pairNumber := 0
|
||||
for i := 0; i+1 < len(sections); i++ {
|
||||
var headingType, paragraphType, heading, paragraph string
|
||||
_ = json.Unmarshal(sections[i]["type"], &headingType)
|
||||
_ = json.Unmarshal(sections[i+1]["type"], ¶graphType)
|
||||
if !strings.EqualFold(headingType, "h2") || !strings.EqualFold(paragraphType, "p") {
|
||||
continue
|
||||
}
|
||||
_ = json.Unmarshal(sections[i]["instructions"], &heading)
|
||||
_ = json.Unmarshal(sections[i+1]["instructions"], ¶graph)
|
||||
pairNumber++
|
||||
newHeading, newParagraph, changed := a1UniqBenefit(heading, paragraph, pairNumber, heads, paragraphs)
|
||||
if !changed {
|
||||
i++
|
||||
continue
|
||||
}
|
||||
hb, err := json.Marshal(newHeading)
|
||||
if err != nil {
|
||||
return raw, 0, err
|
||||
}
|
||||
pb, err := json.Marshal(newParagraph)
|
||||
if err != nil {
|
||||
return raw, 0, err
|
||||
}
|
||||
sections[i]["instructions"] = hb
|
||||
sections[i+1]["instructions"] = pb
|
||||
normalized++
|
||||
i++
|
||||
}
|
||||
if normalized == 0 {
|
||||
return raw, 0, nil
|
||||
}
|
||||
encoded, err := json.Marshal(sections)
|
||||
if err != nil {
|
||||
return raw, 0, err
|
||||
}
|
||||
obj["sections"] = encoded
|
||||
result, err := json.Marshal(obj)
|
||||
if err != nil {
|
||||
return raw, 0, err
|
||||
}
|
||||
return string(result), normalized, nil
|
||||
}
|
||||
Reference in new issue
Block a user