Files
xinmi/internal/application/service/chat_pipeline/extract_entity_coverage.go
T

42 lines
1.6 KiB
Go
Raw Normal View History

package chatpipeline
import (
"fmt"
"regexp"
"strings"
"github.com/Tencent/WeKnora/internal/types"
)
var graphStorageReachInstructionPattern = regexp.MustCompile(`^(?:请\s*)?(?:放\s*置\s*于|置\s*于|放\s*在).+不\s*易\s*触\s*及(?:\s*之)?\s*处$`)
// This bounded coverage check recognizes explicit placement instructions only.
// Unclear OCR stays verbatim; detecting a missing rule must never create an edge.
func validateGraphStorageCoverage(graph *types.GraphData, content string) error {
for _, clause := range strings.FieldsFunc(content, func(r rune) bool {
return strings.ContainsRune(",,。!?!?;;::\n\r", r)
}) {
clause = strings.TrimSpace(clause)
if heading := graphSectionHeadingPattern.FindStringIndex(clause); heading != nil && heading[0] == 0 {
clause = strings.TrimSpace(clause[heading[1]:])
}
if !graphStorageReachInstructionPattern.MatchString(clause) {
continue
}
covered := false
for _, relation := range graph.Relation {
if relation != nil && relation.Type == "约束" &&
strings.Contains(relation.Node2, clause) &&
strings.Contains(relation.Evidence, clause) &&
strings.Contains(content, relation.Evidence) {
covered = true
break
}
}
if !covered {
return fmt.Errorf("%w: explicit storage restriction is not covered by a complete source-grounded constraint: %q; preserve the original clause verbatim without repairing OCR if its relationship is supported, otherwise leave this source for review; never invent an edge to satisfy coverage", ErrGraphNeedsReview, clause)
}
}
return nil
}