Files
audita/internal/core/glossary/matching.go

215 lines
6.5 KiB
Go

package glossary
import (
"fmt"
"regexp"
"slices"
"sort"
"strings"
"unicode"
"unicode/utf8"
)
type protectedTermDef struct {
identities []Identity
canonical string
}
type protectedOccurrence struct {
text string
canonical string
}
// ProtectedVocabulary is a deterministic glossary-derived protected term set.
type ProtectedVocabulary struct {
termsByFolded map[string]protectedTermDef
pattern *regexp.Regexp
}
// NewProtectedVocabulary prepares the independently selected terms for matching.
func NewProtectedVocabulary(terms []Term) ProtectedVocabulary {
termsByFolded := make(map[string]protectedTermDef, len(terms))
ordered := slices.Clone(terms)
sort.SliceStable(ordered, func(i, j int) bool {
return len(ordered[i].Spelling) > len(ordered[j].Spelling)
})
var groups, alternatives []string
var left, right string
flush := func() {
if len(alternatives) > 0 {
groups = append(groups, left+`(?:`+strings.Join(alternatives, "|")+`)`+right)
alternatives = alternatives[:0]
}
}
for _, term := range ordered {
if term.Spelling == "" {
continue
}
if !term.Exempt {
termsByFolded[strings.ToLower(term.Spelling)] = protectedTermDef{identities: slices.Clone(term.Identities), canonical: term.Spelling}
}
first, _ := utf8.DecodeRuneInString(term.Spelling)
last, _ := utf8.DecodeLastRuneInString(term.Spelling)
// Share boundary checks across adjacent alternatives without
// reordering competing spellings.
a, b := literalEdge(first), literalEdge(last)
if a != left || b != right {
flush()
left, right = a, b
}
alternatives = append(alternatives, regexp.QuoteMeta(term.Spelling))
}
if len(termsByFolded) == 0 {
return ProtectedVocabulary{}
}
flush()
pattern := regexp.MustCompile(`(?i)(?:` + strings.Join(groups, "|") + `)`)
return ProtectedVocabulary{termsByFolded: termsByFolded, pattern: pattern}
}
// A punctuation or symbol edge needs the same ASCII nonword state both
// inside and outside the spelling. Letter and number edges retain Go's
// regexp word-boundary behavior. Underscore is an ASCII word character.
func literalEdge(r rune) string {
if r != '_' && (unicode.IsPunct(r) || unicode.IsSymbol(r)) {
return `\B`
}
return `\b`
}
func (v ProtectedVocabulary) Empty() bool {
return v.pattern == nil
}
// AllowsChange checks the full text of a segment against the protection view.
func (v ProtectedVocabulary) AllowsChange(before, after string, allowRepetitionCleanup bool) bool {
return v.Violation(before, after, allowRepetitionCleanup) == ""
}
// Violation compares usable full-segment text before and after a proposed edit.
// Spoken-word cleanup may reduce repeated mentions while preserving each identity.
func (v ProtectedVocabulary) Violation(before, after string, allowRepetitionCleanup bool) string {
if v.Empty() || before == after {
return ""
}
beforeByID := v.occurrencesByIdentity(before)
afterByID := v.occurrencesByIdentity(after)
if reason := validateIdentityPreservation(beforeByID, afterByID); reason != "" {
if allowRepetitionCleanup && preservesRepeatedIdentities(beforeByID, afterByID) {
return ""
}
return reason
}
return validateCapitalizationTransitions(beforeByID, afterByID)
}
func (v ProtectedVocabulary) occurrencesByIdentity(text string) map[Identity][]protectedOccurrence {
byID := make(map[Identity][]protectedOccurrence)
v.scan(text, func(term protectedTermDef, matched string) {
occurrence := protectedOccurrence{text: matched, canonical: term.canonical}
for _, identity := range term.identities {
byID[identity] = append(byID[identity], occurrence)
}
})
return byID
}
// MentionedIdentities expands each matched spelling's contributors only once.
func (v ProtectedVocabulary) MentionedIdentities(texts ...string) map[Identity]struct{} {
identities := make(map[Identity]struct{})
seen := make(map[string]struct{})
for _, text := range texts {
v.scan(text, func(term protectedTermDef, _ string) {
if _, found := seen[term.canonical]; found {
return
}
seen[term.canonical] = struct{}{}
for _, id := range term.identities {
identities[id] = struct{}{}
}
})
}
return identities
}
func (v ProtectedVocabulary) scan(text string, visit func(protectedTermDef, string)) {
if v.Empty() {
return
}
for _, span := range v.pattern.FindAllStringIndex(text, -1) {
matched := text[span[0]:span[1]]
if term, ok := v.termsByFolded[strings.ToLower(matched)]; ok {
visit(term, matched)
}
}
}
func validateIdentityPreservation(beforeByID, afterByID map[Identity][]protectedOccurrence) string {
var selected string
found := false
for identity, beforeItems := range beforeByID {
afterItems := afterByID[identity]
if len(afterItems) >= len(beforeItems) {
continue
}
// Protect identity counts. Within an identity that lost a mention,
// use spelling counts to identify which alias was affected.
remaining := make(map[string]int)
for _, item := range afterItems {
remaining[item.canonical]++
}
for _, item := range beforeItems {
spelling := item.canonical
if remaining[spelling] > 0 {
remaining[spelling]--
continue
}
if !found || spelling < selected {
selected, found = spelling, true
}
}
}
return protectedSpellingMessage(selected, found)
}
func validateCapitalizationTransitions(beforeByID, afterByID map[Identity][]protectedOccurrence) string {
var selected string
found := false
for identity, afterItems := range afterByID {
beforeItems := beforeByID[identity]
for i, afterItem := range afterItems {
if i >= len(beforeItems) || afterItem.text == beforeItems[i].text || afterItem.text == afterItem.canonical {
continue
}
if !found || afterItem.canonical < selected {
selected, found = afterItem.canonical, true
}
}
}
return protectedSpellingMessage(selected, found)
}
func protectedSpellingMessage(spelling string, found bool) string {
if !found {
return ""
}
return fmt.Sprintf("proposal may alter protected glossary spelling %q", spelling)
}
// preservesRepeatedIdentities permits a spoken-word proposal to reduce repeated
// mentions, but not remove an identity or replace its surviving spelling.
// Editorial and meaning validators still decide whether the cleanup is warranted.
func preservesRepeatedIdentities(original, result map[Identity][]protectedOccurrence) bool {
reduced := false
for identity, occurrences := range original {
remaining := result[identity]
if len(remaining) == 0 {
return false
}
if len(remaining) < len(occurrences) {
reduced = true
}
}
return reduced && validateCapitalizationTransitions(original, result) == ""
}